r - How can I make plotly subplots the same size when converting from facets with ggplotly? -


in ggplot2 believe facets (subplots) same size. think important making comparisons between subplots. however, when convert plots plotly objects using ggplotly() subplots not same size. in particular, rows seem different heights. not sure if intended functionality of ggplotly(), bug, or user error on part. using ggplot2 version 2.2.1.9000 , plotly 4.7.1.

how can convert ggplots plotly objects while ensuring subplots same size?

note: posted question in context of using shiny , plotly. however, issue seems independent of shiny deleting original question , reposting.

in example below, when use facet_wrap(sex ~ day) second row shorter top , bottom rows. when use facet_grid(sex ~ day) rows equal in height. when use facet_grid(sex ~ day) 2 middle rows shorter.

library(plotly) library(reshape2)  # example plot myplot <- ggplot(tips, aes(x = total_bill, y = tip / total_bill)) +   geom_point(shape = 1)   # facet wrap sex ~ day pwrap <- myplot + facet_wrap(sex ~ day) qwrap <- ggplotly(pwrap)  q <- qwrap d <- rbind(q$x$layout$yaxis4$domain, q$x$layout$yaxis3$domain, q$x$layout$yaxis2$domain, q$x$layout$yaxis$domain) dfwrap <- data.frame(lb = d[,1], ub = d[,2]) dfwrap["range"] <- dfwrap["ub"] - dfwrap["lb"] dfwrap["facettype"] <- "facet_wrap(sex ~ day)"  # facet grid sex ~ day pgrid1 <- myplot + facet_grid(sex ~ day) qgrid1 <- ggplotly(pgrid1)  q <- qgrid1 d <- rbind(q$x$layout$yaxis4$domain, q$x$layout$yaxis3$domain, q$x$layout$yaxis2$domain, q$x$layout$yaxis$domain) dfgrid1 <- data.frame(lb = d[,1], ub = d[,2]) dfgrid1["range"] <- dfgrid1["ub"] - dfgrid1["lb"] dfgrid1["facettype"] <- "facet_grid(sex ~ day)"  # facet grid day ~ sex pgrid2 <- myplot + facet_grid(day ~ sex) qgrid2 <- ggplotly(pgrid2)  q <- qgrid2 d <- rbind(q$x$layout$yaxis4$domain, q$x$layout$yaxis3$domain, q$x$layout$yaxis2$domain, q$x$layout$yaxis$domain)  dfgrid2 <- data.frame(lb = d[,1], ub = d[,2]) dfgrid2["range"] <- dfgrid2["ub"] - dfgrid2["lb"] dfgrid2["facettype"] <- "facet_grid(day ~ sex)" 

edit

after more digging found plotly uses get_domains() function define layout. inside function there line defining heights of plots

ys[[i]] <- c(ystart = 1 - (heights[j]) - if (j == 1) 0 else margins[3],              yend = 1 - (heights[j + 1]) + if (j == nrows) 0 else margins[4]) 

such top , bottom rows higher width of margins. appears margins include not spacing between panels (panel.spacing) room axis labels, ticks, panel titles, etc. therefore, can find test case works reasonably ggplot() totally fails ggplotly()

p <- ggplot(data = tips %>%                group_by(time, sex, day) %>%               summarize(total_bill = mean(total_bill)),             aes(x = time, y = total_bill)) +   geom_bar(stat = "identity") +   facet_wrap(sex ~ day, scales = "free_x") +   theme(axis.text.x = element_text(angle = 90))  ggplotly(p) 


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -