Removing white spaces between facets when using ggplotly()

Viewed 632

I wish to make a ggplot object interactive using plotly. When I use ggplotly(), huge white spaces are introduced between my facets, making the plot small and weird looking:

plotly facets

Compared to how big and neat it looks when using ggplot only:

ggplot object

My reproducible code is:

library(plotly)
library(ggplot2)
library(reshape2)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  facet_wrap( ~ day, ncol=2)

ggplotly(p)

I don't know if its my computer settings or something is wrong with my code. Am using R version 4.0.2, ggplot2 version 3.3.2 and plotly version 4.9.2.1. Someone help me retain the size and add interactivity to my plot.

I have tried the answer given here by Cam McMains but nothing changes.

Update

library(plotly)
library(ggplot2)
library(reshape2)

p <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + 
  geom_point(shape=1) + 
  theme(panel.spacing=unit(0,'npc'))+
  facet_wrap( ~ day, ncol=2)
  
ggplotly(p)

Gives update

0 Answers
Related