Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
947 views
in Technique[技术] by (71.8m points)

r - ggplot's transparent background changes after ShinyApp resizing

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...