更改 ggpubr::ggarrange 中组合图的背景颜色

And*_*ard 2 r ggplot2

我的图

\n

我不明白为什么底角背景是白色的,而绘图的其余部分是灰色的。无论如何,我可以将底角颜色更改为与绘图其余部分相同的背景吗?

\n

我对每个图使用的代码是:

\n
HP_specifikationer_model1 <- ggplot(Svar_spec_data)+geom_hline(yintercept=0)\ngeom_line(aes(y=HP1, x=kvartaler, color = "HP-BNP-KRE-REN")) +\ngeom_line(aes(y=HP2, x=kvartaler, color = "REN-KRE-HP-BNP")) +\ngeom_line(aes(y=HP3, x=kvartaler, color = "BNP-KRE-REN-HP")) +\ngeom_line(aes(y=HP4, x=kvartaler, color = "KRE-HP-REN-BNP")) +\ngeom_line(aes(y=HP5, x=kvartaler, color = "BNP-KRE-HP-REN")) +\ngeom_line(aes(y=HP6, x=kvartaler, color = "BNP-REN-HP-KRE")) +\ngeom_line(aes(y=HP7, x=kvartaler, color = "REN-HP-BNP-KRE")) +\nscale_color_manual(name=\'Specifikation\',\n                 breaks=c(\'HP-BNP-KRE-REN\', \'REN-KRE-HP-BNP\', \'BNP-KRE-REN-HP\',\n                          \'KRE-HP-REN-BNP\', \'BNP-KRE-HP-REN\', \'BNP-REN-HP-KRE\',\n                          \'REN-HP-BNP-KRE\'),\n                 values=c(\'HP-BNP-KRE-REN\'=\'red\', \'REN-KRE-HP-BNP\'=\'blue\', \'BNP-KRE-REN-HP\'=\'green\',\n                          \'KRE-HP-REN-BNP\'=\'cyan\', \'BNP-KRE-HP-REN\'=\'lightpink\', \'BNP-REN-HP-KRE\'=\'orange\',\n                          \'REN-HP-BNP-KRE\'=\'black\'))+\nlabs(title = "Varrierende SVAR matricer ved st\xc3\xb8d til Boligpriserne", x="Lag (M\xc3\xa5neder)", y="Respons fra boligprisen", caption = "Egne beregninger", color= T)+\ntheme(legend.position="right") + theme(panel.grid.minor.x = element_blank()) + th\n
Run Code Online (Sandbox Code Playgroud)\n

代码末尾的 th 是用以下代码定义的:

\n
th <- theme(title = element_text(colour = "#404040"),\n        plot.background=element_rect(fill="#f3f3f3"),  \n        panel.background = element_rect(fill="#f3f3f3"), \n        legend.background = element_rect(fill="#f3f3f3"),\n        plot.subtitle = element_text(color="#666666"),\n        plot.caption = element_text(color="#616161", size=9),\n        legend.key = element_rect(fill = "#f3f3f3", colour = "#f3f3f3"),\n        plot.margin = unit(c(0.5, 0.7, 0.5, 0.7), "cm"), \n        panel.border = element_rect(color = "black",\n                                fill = NA,\n                                size = 0.15))\n
Run Code Online (Sandbox Code Playgroud)\n

我使用的代码是安排我的图:

\n
ggarrange(BNP_specifikationer_model1,\nRENTE_specifikationer_model1,\nKREDIT_specifikationer_model1,\nHP_specifikationer_model1,\nncol=2, nrow=2, common.legend = TRUE, legend="bottom")\n
Run Code Online (Sandbox Code Playgroud)\n

Tje*_*ebo 5

I think in ggpubr, you can use bgcolor

You can also use cowplot, such as in this thread

Or patchwork, as per below, with the advantage that you avoid repeated calls of your theme!

## Given that your data is not reproducible, here a made up example.

library(ggpubr)
#> Loading required package: ggplot2
# Create some plots - based on the example ?ggpubr::ggarrange
data("ToothGrowth")
df <- ToothGrowth
df$dose <- as.factor(df$dose)
bxp <- ggboxplot(df, x = "dose", y = "len",
                 color = "dose", palette = "jco")
dp <- ggdotplot(df, x = "dose", y = "len",
                color = "dose", palette = "jco")
dens <- ggdensity(df, x = "len", fill = "dose", palette = "jco")

# ::::::::::::::::::::::::::::::::::::::::::::::::::
ggarrange(bxp, dp, dens, ncol = 2, nrow = 2) + bgcolor("Grey")         
#> Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.
Run Code Online (Sandbox Code Playgroud)

Another option, using {patchwork}, changing the general theme with & theme()

library(patchwork)
bxp + dp + dens + plot_layout(ncol = 2) &
  theme(plot.background = element_rect(fill = "Grey"))
#> Bin width defaults to 1/30 of the range of the data. Pick better value with `binwidth`.
Run Code Online (Sandbox Code Playgroud)

Created on 2022-05-26 by the reprex package (v2.0.1)