r-如何更改Shiny中填充的背景颜色



我下面的图在子图之间有填充,或者根据应用程序窗口的大小向上/下或左/右填充。我对这里的间距和布局管理非常满意,并希望随着绘图数量的增长或缩小(或应用程序窗口的大小调整(保持灵活性,以动态调整大小。然而,我希望这个填充的背景颜色是与应用程序相同的灰色背景,而不是白色。

library(dplyr)
library(ggplot2)
library(ggcorrplot)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- dashboardPage(

header = dashboardHeader(),

sidebar = dashboardSidebar(),

body = dashboardBody(

fluidRow(
plotOutput("test",  width = '100%'), align="center"
)
)
)

server <- function(input, output) {
#generate some dummy data with a random number of plots to show
set.seed(NULL)
numPlots <- sample(1:5, 1)
plot <- sort(rep(seq(1:numPlots), 10))
d <- data.frame(A = runif(10*numPlots, 0, 10), B = runif(10*numPlots, 0, 10), C = plot)



matchingGray <- rgb(236/255, 240/255, 245/255)  #best guess using screenshot and then dropper in another tool

g <- list()
for (p in 1:numPlots)
{
g[[p]] <- ggcorrplot(corr = cor(d %>% filter(C == p) %>% select(A,B))) +
theme(plot.background = element_rect(fill = matchingGray, linetype = "blank"))
}



output$test <- renderPlot({

gridExtra::grid.arrange(grobs = g, ncol = length(g))

})
}

shinyApp(ui = ui, server = server)

忽略那些给出无意义相关性的愚蠢数据,这只是一个简单的切入问题核心的占位符。拥有可变数量的子画面是关键。。。尝试多次运行该应用程序以查看填充的含义。谢谢

这个问题可以在绘图方法上解决,而不是从亮面解决。使用cowplot::ggdraw解决问题:

library(dplyr)
library(ggplot2)
library(ggcorrplot)
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(cowplot)
ui <- dashboardPage(
header = dashboardHeader(),
sidebar = dashboardSidebar(),
body = dashboardBody(
fluidRow(
plotOutput("test",  width = '100%'), align="center"
)
)
)
server <- function(input, output) {
#generate some dummy data with a random number of plots to show
set.seed(NULL)
numPlots <- sample(1:5, 1)
plot <- sort(rep(seq(1:numPlots), 10))
d <- data.frame(A = runif(10*numPlots, 0, 10), B = runif(10*numPlots, 0, 10), C = plot)
matchingGray <- rgb(236/255, 240/255, 245/255)  #best guess using screenshot and then dropper in another tool

g <- list()
for (p in 1:numPlots) {
g[[p]] <- ggcorrplot(corr = cor(d %>% filter(C == p) %>% select(A,B))) +
theme(plot.background = element_rect(fill = matchingGray, linetype = "blank"))
}
output$test <- renderPlot({
cowplot::ggdraw(gridExtra::grid.arrange(grobs = g, ncol = length(g), bg = "wheat1")) +
theme(plot.background = element_rect(fill="#ecf0f5", color = NA))
})
}
shinyApp(ui = ui, server = server)

最新更新