저는 ggpubr::ggarrange
y축과 x축을 공유하는 다중 플롯을 만들곤 했습니다. 내가 겪고있는 유일한 문제는 y 축이있는 첫 번째 플롯이 전체 그림의 비율을 벗어난 다른 3 개의 플롯보다 작다는 것입니다.
따라서 첫 번째 플롯을 다른 3개의 플롯에 비례하지 않게 하지 않고 하나의 y 랩만 표시하는 솔루션을 찾고 있습니다. 이미 한동안 잠잠했던 이후로 이 문제에 대한 해결책을 찾고 있었기 때문에 도움을 주시면 감사하겠습니다.
지금까지의 접근 방식은 플롯(p) 2,3,4에서 y 랩을 제거하고 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)
출력 이미지: 1
또한 p2,p3,p4에 있는 y lab과 text를 제거하지 않고 ggarrange 함수에서 풀려고 했는데 결과가 같습니다.
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)
다른 패키지가 옵션인 경우 을 사용하는 것이 좋습니다 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
의 ggpubr:ggarrange
스퀴즈 패싯 을 사용하여 각 플롯에서 모든 패싯이 동일한 너비인 경우와 비교 y
:
ggpubr::ggarrange(plotlist = p, nrow = 1, common.legend = TRUE)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다