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