`facet_wrap()` with `scales = "free_x"` squishes middle facets
Personne n'a encore pris cette issue.
- Langage dominant
- R
- Étoiles
- 2.7k
- Forks
- 641
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Description
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:
Interestingly, if you use scales = "free_y" or scales = "fixed", the bug is not triggered:
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:
- 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:
- Resize the plot window by increasing the height, and you get the weird result:
- Key observation: if you now re-run the code from step 1, with the window height already tall, it renders correctly:
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!
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par exécuter le MWE R/Shiny fourni avec ggplot2, plotly et facet_wrap(scales = "free_x"), puis reproduisez la séquence de redimensionnement en dehors de Shiny. Examinez la mise en page du graphique généré et le comportement responsive pendant le redimensionnement ; le travail est terminé lorsque les lignes de facettes centrales conservent leurs hauteurs attendues et qu’aucun espace vide n’apparaît après le redimensionnement.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, r
- Domaine
- data-visualization, frontend
- Type d'issue
- Bug
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- Calme
- Clarté
- Plutôt claire
- Accessibilité débutants
- 45/100