plotly.R icon indicating copy to clipboard operation
plotly.R copied to clipboard

`facet_wrap()` with `scales = "free_x"` squishes middle facets

Open khusmann opened this issue 3 months ago • 1 comments

Summary

When using facet_wrap() with scales = "free_x", the middle facets rows appear squished.

MWE

This bug is most apparent when working in Shiny contexts:

library(ggplot2)
library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("p", height = "800px")
)

server <- function(input, output, session) {
  output$p <- renderPlotly({
    plt <- palmerpenguins::penguins |>
      ggplot(
        aes(x = sex)
      ) +
      geom_bar() +
      facet_wrap(
        ggplot2::vars(species, island, year),
        ncol = 2,
        axes = "all",
        scales = "free_x" # or "free"
      )

    plotly::ggplotly(plt)
  })
}

shinyApp(ui, server)

The result is an output where the first and last row of facets look correct, but all the middle facet rows are squished and there's a bunch of whitespace inbetween:

Image

Interestingly, if you use scales = "free_y" or scales = "fixed", the bug is not triggered:

Image

Potential Cause

I believe this is an issue with the responsivity / css of the resulting plot. You can get this to trigger outside of shiny, by doing the following:

  1. Size your plot output window to result in a squished output, and run the following:
plt <- palmerpenguins::penguins |>
  ggplot(
    aes(x = sex)
  ) +
  geom_bar() +
  facet_wrap(
    ggplot2::vars(species, island, year),
    ncol = 2,
    axes = "all",
    scales = "free_x"
  )

plotly::ggplotly(plt)

Result:

Image
  1. Resize the plot window by increasing the height, and you get the weird result:
Image
  1. Key observation: if you now re-run the code from step 1, with the window height already tall, it renders correctly:
Image

This is why I think there's responsivity / css thing going on here -- I imagine Shiny is rendering to a smaller height, then resizing to fill the space of the div. Responsivity is broken, triggering the weird result.

Thanks for looking in to this!

khusmann avatar Sep 24 '25 22:09 khusmann

Note: in Shiny this can be worked around by specifying plot height in the ggplotly() render call instead of plotlyOutput()

library(ggplot2)
library(shiny)
library(plotly)

ui <- fluidPage(
  plotlyOutput("p")
)

server <- function(input, output, session) {
  output$p <- renderPlotly({
    plt <- palmerpenguins::penguins |>
      ggplot(
        aes(x = sex)
      ) +
      geom_bar() +
      facet_wrap(
        ggplot2::vars(species, island, year),
        ncol = 2,
        axes = "all",
        scales = "free_x"
      )

    plotly::ggplotly(plt, height = 800)
  })
}

shinyApp(ui, server)

Still would be nice to have the responsivity fixed though...

khusmann avatar Sep 25 '25 17:09 khusmann