plotly / plotly.R

An interactive graphing library for R
https://plotly-r.com
Other
2.57k stars 627 forks source link

Panels are different sizes when converting ggplot plot with facets #1099

Open natbprice opened 7 years ago

natbprice commented 7 years ago

When converting a ggplot plot with facets to a plotly object the panels are different sizes. Margins are not subtracted from top and bottom rows so these panels end up being taller. This makes it hard to compare among panels. Margins for top and bottom of panels are equal so longer x-axis labels result in large margins on tops of panels with large spacing between panels and a low data-ink ratio.

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

p <- ggplot(data = tips,
            aes(x = sex, y = total_bill)) +
  geom_jitter(width = 0.1, height = 0) +
  facet_wrap(day ~ time, scales = "free_x", ncol = 2) +
  theme(axis.text.x = element_text(angle = 45),
        strip.text.x = element_text(size = 8, 
                                    margin = margin(t = 1, r = 0, b = 1, l = 0, unit = "pt")))

q <- ggplotly(p) %>% 
  plotly::layout(margin = list(l = 80, r = 0, t = 100, b = 100, pad = 0))

d <- rbind(q$x$layout$yaxis4$domain, q$x$layout$yaxis3$domain, q$x$layout$yaxis2$domain, q$x$layout$yaxis$domain)
df <- data_frame(lb = d[,1], ub = d[,2]) %>% 
  mutate(range = ub - lb,
         panelSpacing = lb - lag(ub))
robsalasco commented 4 years ago

hi! any news about this bug?

Dennishi0925 commented 4 years ago

Are there any solutions to the bug?

zykezero commented 3 years ago

Found this from 2017 regarding this issue. https://stackoverflow.com/questions/46955032/ggplotly-with-facet-wrapscales-free-y-creates-uneven-panel-widths

Someone has a solution here. https://stackoverflow.com/questions/61580973/first-and-last-facets-using-facet-wrap-with-ggplotly-are-larger-than-middle-face