`facet_wrap()` with `scales = "free_x"` squishes middle facets
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- R
- Star
- 2.7k
- Fork
- 641
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Mô tả
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!
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu bằng cách chạy MWE R/Shiny được cung cấp với ggplot2, plotly và facet_wrap(scales = "free_x"), sau đó tái hiện chuỗi thao tác thay đổi kích thước bên ngoài Shiny. Kiểm tra bố cục plot được tạo ra và hành vi responsive trong quá trình thay đổi kích thước; được xem là hoàn thành khi các hàng facet ở giữa vẫn giữ nguyên chiều cao như mong đợi và không xuất hiện khoảng trắng sau khi thay đổi kích thước.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- javascript, r
- Lĩnh vực
- data-visualization, frontend
- Loại issue
- Lỗi
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức độ hoạt động
- Ít trao đổi
- Độ rõ ràng
- Khá rõ ràng
- Mức phù hợp với người mới
- 45/100