3

When I facet the plots using plotly, it causes the panel names to overlap with the next plot below it. I want the panel names to fit in the box and not overlap with the other plots below it. I have tried to change the spacing between panels but that did not seem to work. I would rather panel name fit on one line rather than on two lines. Here is my code:

library(ggplot2)
library(tidyverse)
library(plotly)

mtcars$cyl = sample(letters[1:5], 32, TRUE)
mtcars$cyl <- as.factor(mtcars$cyl)
mtcars$gear <- as.factor(mtcars$gear)


library(magrittr)
gg_facet_nrow <- function(p){
  num_panels <- length(unique(ggplot_build(p)$data[[1]]$PANEL)) # get number of panels
  num_cols <- ggplot_build(p)$layout$facet$params$ncol # get number of columns set by user
  num_rows <- wrap_dims(num_panels, ncol=num_cols)[1] # determine number of rows
}

p1 <- ggplot(mtcars, aes(mpg, wt)) +geom_line()+ facet_wrap( . ~ gear,  scale = "free_y", ncol = 1 ) + theme(panel.spacing = unit(1,"cm"))
p2 <- ggplot(mtcars, aes(mpg, wt)) + geom_line() + facet_wrap(vars(cyl, gear), scale = "free_y", ncol = 1) + theme(panel.spacing = unit(1,"cm")) 

he <- gg_facet_nrow(p1)
he1 <- gg_facet_nrow(p2)

ggplotly(p1, height = he *300)

ggplotly(p2, height = he1 * 300)

Here is a picture of what I mean: enter image description here

Does anyone know why this is happening and how to fix it?

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
QMan5
  • 713
  • 1
  • 4
  • 20
  • Does the issue have anything to do with shiny? If you make the same plot *not* in Shiny, does it still have the issue? – Gregor Thomas Oct 25 '21 at 19:16
  • It still happens even when I'm not using shiny. I believe it might be because I'm using facet_wrap instead of facet_grid. Although, I feel like there should be some way to fix this problem. Basically, the 4 is the gear and a is the cyl in the photo. I'm not sure how these two things go be on one line on the panel name. – QMan5 Oct 25 '21 at 19:20
  • I assume then it's a plotly issue - if you make a plain `ggplot` does it work? Just trying to figure out how to make your problem **minimal**, which usually makes it easier to debug. – Gregor Thomas Oct 25 '21 at 19:22
  • It's not a problem when I just use ggplot. It seems to only happen when the plot is converted to plotly. – QMan5 Oct 25 '21 at 19:24
  • Would you mind extracting your code from shiny to make a **minimal** reproducible example? – Gregor Thomas Oct 25 '21 at 19:42
  • It should be reproducible. I included the code above the pictures and gif. The dataset is mtcar which comes from base r. I just took a sample just to make it easier to load. – QMan5 Oct 25 '21 at 20:01
  • Yes, it's nicely reproducible, but it's far from **minimal**. If I were to work on it, the first thing I would do would be to make it more minimal, but since you've built it it would probably be much faster for you to do it--and also make the question more attractive for others to work on it - right now people probably won't look at this question unless they're experts at Shiny **and** Plotly. I'm pretty good at Shiny, but not great at Plotly. If you strip the Shiny out since it's irrelevant, maybe someone good at Plotly will come and solve it real quick. – Gregor Thomas Oct 25 '21 at 20:31
  • I understand and have made changes to the question. – QMan5 Oct 25 '21 at 20:54

0 Answers0