ggplot's transparent background changes after ShinyApp resizing

蓝咒 提交于 2020-02-05 03:31:07

问题


The following code shows 2 ggplot2-plots in a shinydashboard. The plot backgrounds should always be transparent, even after resizing.

The plots show correctly when starting the app, but as soon as the screen is resized, or the siderbar closed, the background changes to white again. Why is that and how can I prevent that?

When closing the sidebar, the background changes to white and after reopening the sidebar, the plots switch to transparent again. But when resizing the window, its not changing back to transparent no matter what. Except maybe you resize exactly to the default window dimensions. I didnt manage to test that ;)

This occurs in RStudio and browser (Chrome, Firefox).

I know that an option would be to change the background color of the ggplots to the background color of the ShinyApp. But I hope its not the only.

library(shiny)
library(shinydashboard)
library(ggplot2)

df <- data.frame(
  id = rep(1:5, each=5),
  a = runif(25, 2, 50)
)

ui = {dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    splitLayout(cellWidths = c("50%", "50%"), 
                plotOutput("boxplot"),
                plotOutput("vioplot")
    )
  )
)}

server <- function(input, output) {
  output$boxplot <- renderPlot({
    ggplot(df, aes(x=id, y=a, group=id)) + 
      geom_boxplot(aes(fill=id)) +
      facet_grid(~id, margins = T) +
      theme(rect=element_blank(),
            panel.grid = element_blank(),
            panel.background= element_blank(),
            plot.background = element_blank()
      )
  }, bg="transparent")

  output$vioplot <- renderPlot({
    ggplot(df, aes(x=id, y=a, group=id)) + 
      geom_violin(aes(fill=factor(id))) +
      facet_grid(~id, margins = T) +
      theme(rect=element_blank(),
            panel.grid = element_blank(),
            panel.background= element_blank(),
            plot.background = element_blank()
      )
  }, bg="transparent")
}

shinyApp(ui, server)

回答1:


It seems as though when you run a plot in shiny with renderPlot it saves the plot as a variable so that when you resize the page the plot is not rerendered, it just shows the image again. This seems to be having issues with the transparent background (maybe due to a background being set when it is saved as a variable? I'm not sure on this point). To prevent this, set the execOnResize option to TRUE in renderPlot, this will redraw the plot instead of resize the saved image. For example:

output$boxplot <- renderPlot({
ggplot(df, aes(x=id, y=a, group=id)) + 
  geom_boxplot(aes(fill=id)) +
  facet_grid(~id, margins = T) +
  theme(rect=element_blank(),
        panel.grid = element_blank(),
        panel.background= element_blank(),
        plot.background = element_blank()
  )
}, bg="transparent", execOnResize = TRUE)


来源:https://stackoverflow.com/questions/50690093/ggplots-transparent-background-changes-after-shinyapp-resizing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!