问题描述
以下代码显示了 shinydashboard
中的 2 个 ggplot2
-plots.情节背景应始终透明,即使在调整大小后也是如此.
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 ;)
这发生在 RStudio 和浏览器(Chrome、Firefox)中.
This occurs in RStudio and browser (Chrome, Firefox).
我知道一个选项是将 ggplots 的背景颜色更改为 ShinyApp 的背景颜色.但我希望它不是唯一的.
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)
推荐答案
似乎当您使用 renderPlot 以闪亮的方式运行绘图时,它会将绘图保存为变量,以便在调整页面大小时不会重新渲染绘图,它只是再次显示图像.这似乎与透明背景有关(可能是由于在将其另存为变量时设置了背景?我不确定这一点).为了防止这种情况,在 renderPlot 中将 execOnResize 选项设置为 TRUE,这将重绘绘图而不是调整保存图像的大小.例如:
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)
这篇关于ShinyApp 调整大小后 ggplot 的透明背景会发生变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!