Skip to content

Support for plots with absolute dimensions #4194

@jbengler

Description

@jbengler

Dear shiny team,

sometimes it makes sense to choose absolute dimensions for a ggplot to get more control over the aspect ratio and the visual proportions of the elements in the plot.

One way to do this is patchwork::plot_layout(), but also the new ggplot2 release will acquire this capability in ggplot2::theme(). See here:
tidyverse/ggplot2#6094

Currently, shiny is already respecting the absolute dimensions of the plot. However, with big window sizes we get a lot of white space around the plot and with small window sizes the plot gets cut off. See reprex below.

What would be the ideal behavior?
It would be great if the plot image would still be scaled to fill all available space, however more in the sense of a responsive web image. See https://www.w3schools.com/howto/howto_css_image_responsive.asp

Do you see a way to implement this?
Or is there a workaround that already exists in current shiny?

Thanks for you thoughts & Best wishes
Jan

library(shiny)
library(bslib)
library(tidyverse)
library(patchwork)

ui <- page_sidebar(
  title = "Absolute dimensions",
  sidebar = sidebar(),
  plotOutput(outputId = "distPlot")
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    ggplot(faithful, aes(eruptions, waiting)) +
      geom_point() +
      patchwork::plot_layout(heights = unit(50, "mm"), widths = unit(100, "mm"))
  })
}

shinyApp(ui = ui, server = server)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions