我使用ggpubr::ggarrange
创建了一个具有共享y轴和x轴的多重图。我唯一的问题是,第一个曲线图,它确实有y轴,比其他3个曲线图要小,这使得整个图形不成比例。
因此,我正在寻找一种解决方案,只显示一个y实验室,而不会使第一个图与其他3个图不成比例。非常感谢你的帮助,因为我一直在寻找解决这个问题的方法,因为我已经安静了一段时间。
到目前为止,我的方法是从plot (p) 2,3,4中删除y lab,并将其保留在p1上。
这是我的代码:
library(ggplot2)
library(ggpubr)
library(dplyr)
p1 <- ggplot(arrange(ploughed1, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels=c("4","3","2","1"))))+
geom_bar(stat = "identity", position = "dodge")+
scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
guides(fill = guide_legend(reverse = TRUE))+
labs(fill="Horizon")+
ylim(0,200)+
theme_bw()+
facet_wrap(~compost)+
theme(strip.text = element_text(size = 7),
panel.spacing = unit(0.2, "lines"))+
geom_col(position = position_stack(reverse = TRUE))+
labs(x="Ferment", y = "RAI_II=Rooting*Scheme*Active", title = "P- ")
p2 <- ggplot(arrange(ploughed2, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels=c("4","3","2","1"))))+
geom_bar(stat = "identity", position = "dodge")+
scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
guides(fill = guide_legend(reverse = TRUE))+
labs(fill="Horizon")+
ylim(0,200)+
theme_bw()+
theme(axis.text.y = element_blank(),
panel.spacing = unit(0.2, "lines"),
strip.text = element_text(size = 7))+
facet_wrap(~compost)+
geom_col(position = position_stack(reverse = TRUE))+
labs(x="Ferment", title = "P+ ")+
rremove("ylab")
p3 <- ggplot(arrange(reduced1, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels=c("4","3","2","1"))))+
geom_bar(stat = "identity", position = "dodge")+
scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
guides(fill = guide_legend(reverse = TRUE))+
labs(fill="Horizon")+
ylim(0,200)+
theme_bw()+
theme(axis.text.y = element_blank(),
panel.spacing = unit(0.2, "lines"),
strip.text = element_text(size = 7))+
facet_wrap(~compost)+
geom_col(position = position_stack(reverse = TRUE))+
labs(x="Ferment", title = "RT- ")+
rremove("ylab")
p4 <- ggplot(arrange(reduced2, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels=c("4","3","2","1"))))+
geom_bar(stat = "identity", position = "dodge")+
scale_fill_manual(values = c("#FF9933", "#CC6600","#663300","#000000"))+
guides(fill = guide_legend(reverse = TRUE))+
labs(fill="Horizon")+
ylim(0,200)+
theme_bw()+
theme(axis.text.y = element_blank(),
panel.spacing = unit(0.2, "lines"),
strip.text = element_text(size = 7))+
facet_wrap(~compost)+
geom_col(position = position_stack(reverse = TRUE))+
labs(x="Ferment", title = "RT+ ")+
rremove("ylab")
ggarrange(p1, p2, p3, p4, nrow=1, common.legend = TRUE)
输出.png
输出图像:1
我也试着在ggarrange函数中解决这个问题,没有删除p2、p3和p4中的y lab和文本,结果相同。
ggarrange(p1, p2+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ), p3+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ), p4+
theme(axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank() ) , nrow=1, common.legend = TRUE)
发布于 2021-07-18 23:55:29
如果您可以选择其他包,我建议您使用patchwork
。使用一些方便的函数来减少重复的代码和一些随机的示例数据来模拟您的真实数据:
library(ggplot2)
library(patchwork)
library(dplyr)
ploughed1 <- data.frame(
Horizont = rep(1:4, 4),
RAI_II = runif(16, 10, 50),
Ferment = rep(c("-", "+"), each = 8),
compost = rep(c("- Compost", "+ Compost"), each = 4)
)
plot_fun <- function(x, title) {
ggplot(arrange(x, Horizont), aes(Ferment, RAI_II, fill = factor(Horizont, levels = c("4", "3", "2", "1")))) +
geom_bar(stat = "identity", position = "dodge") +
scale_fill_manual(values = c("#FF9933", "#CC6600", "#663300", "#000000")) +
guides(fill = guide_legend(reverse = TRUE)) +
ylim(0, 200) +
theme_bw() +
facet_wrap(~compost) +
theme(
strip.text = element_text(size = 7),
panel.spacing = unit(0.2, "lines")
) +
geom_col(position = position_stack(reverse = TRUE)) +
labs(x = "Ferment", y = "RAI_II=Rooting*Scheme*Active", fill = "Horizon", title = title)
}
remove_y <- theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.title.y = element_blank()
)
p <- list(
plot_fun(ploughed1, "P-"),
plot_fun(ploughed1, "P+") + remove_y,
plot_fun(ploughed1, "RT-") + remove_y,
plot_fun(ploughed1, "RT+") + remove_y
)
wrap_plots(p, nrow = 1) + plot_layout(guides = "collect")
与每个图中所有面的宽度相同的patchwork
相比,由于y
比例,利用ggpubr:ggarrange
挤压第一个图中的面:
ggpubr::ggarrange(plotlist = p, nrow = 1, common.legend = TRUE)
https://stackoverflow.com/questions/68428896
复制相似问题