gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:05:48 26 4
gpt4 key购买 nike

My ggplot

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

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

HP_specifikationer_model1 <- ggplot(Svar_spec_data)+geom_hline(yintercept=0)
geom_line(aes(y=HP1, x=kvartaler, color = "HP-BNP-KRE-REN")) +
geom_line(aes(y=HP2, x=kvartaler, color = "REN-KRE-HP-BNP")) +
geom_line(aes(y=HP3, x=kvartaler, color = "BNP-KRE-REN-HP")) +
geom_line(aes(y=HP4, x=kvartaler, color = "KRE-HP-REN-BNP")) +
geom_line(aes(y=HP5, x=kvartaler, color = "BNP-KRE-HP-REN")) +
geom_line(aes(y=HP6, x=kvartaler, color = "BNP-REN-HP-KRE")) +
geom_line(aes(y=HP7, x=kvartaler, color = "REN-HP-BNP-KRE")) +
scale_color_manual(name='Specifikation',
breaks=c('HP-BNP-KRE-REN', 'REN-KRE-HP-BNP', 'BNP-KRE-REN-HP',
'KRE-HP-REN-BNP', 'BNP-KRE-HP-REN', 'BNP-REN-HP-KRE',
'REN-HP-BNP-KRE'),
values=c('HP-BNP-KRE-REN'='red', 'REN-KRE-HP-BNP'='blue', 'BNP-KRE-REN-HP'='green',
'KRE-HP-REN-BNP'='cyan', 'BNP-KRE-HP-REN'='lightpink', 'BNP-REN-HP-KRE'='orange',
'REN-HP-BNP-KRE'='black'))+
labs(title = "Varrierende SVAR matricer ved stød til Boligpriserne", x="Lag (Måneder)", y="Respons fra boligprisen", caption = "Egne beregninger", color= T)+
theme(legend.position="right") + theme(panel.grid.minor.x = element_blank()) + th

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

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

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

ggarrange(BNP_specifikationer_model1,
RENTE_specifikationer_model1,
KREDIT_specifikationer_model1,
HP_specifikationer_model1,
ncol=2, nrow=2, common.legend = TRUE, legend="bottom")

最佳答案

我认为在ggpubr中,你可以使用bgcolor

您还可以使用cowplot,such as in this thread

或者拼凑,如下所示,其优点是可以避免重复调用主题!

## 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`.

另一个选项,使用 {patchwork},通过 & 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`.

reprex package 于 2022 年 5 月 26 日创建(v2.0.1)

关于r - 更改 ggpubr::ggarrange 中组合图的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72389809/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com