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

how to change the background color of padding in Shiny

我下面的情节在子情节之间有填充或 top/bottom 或 left/right 的填充取决于应用程序的大小 window。我对这里发生的间距和布局管理非常满意,并且希望随着绘图数量的增加或缩小(或应用程序 window 调整大小)保持灵活性以动态调整大小。但是,我希望此填充的背景颜色与应用程序的背景颜色相同,而不是白色。

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)