【问题标题】:how to change the background color of padding in Shiny如何在 Shiny 中更改填充的背景颜色
【发布时间】:2021-09-19 03:51:33
【问题描述】:

我的下图在子图之间有填充或填充到顶部/底部或左侧/右侧,具体取决于应用程序窗口的大小。我对这里发生的间距和布局管理非常满意,并且希望随着绘图数量的增长或缩小(或应用程序窗口调整大小)保持灵活性以动态调整大小。但是,我希望此填充的背景颜色与应用程序的灰色背景相同,而不是白色。

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)

忽略给出无意义相关性的愚蠢数据,这只是一个简单的占位符,可以解决问题的核心。拥有可变数量的子图是关键......尝试多次运行应用程序以查看对填充的影响。谢谢!

【问题讨论】:

    标签: r shiny colors background


    【解决方案1】:

    这个问题可以在绘图方法上解决,而不是从闪亮的一面。使用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)
    

    【讨论】:

    • 谢谢!!! NA 仍然留下一个白色框,但通过将颜色也设置为我匹配的灰色来轻松修复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 2021-06-29
    • 1970-01-01
    • 2019-02-11
    • 2021-09-16
    • 2018-01-04
    • 2023-03-04
    相关资源
    最近更新 更多