ggplot2,x 轴问题 facet_grid

ggplot2, x-axis issues with facet_grid

我是 R 的新手,正在尝试为我的数据创建一个小提琴图网格。我能够得到我想要的网格布局,但是,当我制作网格时,我的绘图位于 x 轴上,如果它们全部绘制在一起,它们将位于 x 轴上。

p2 <- ggplot(data, aes(x=Treatment, y=ECM, fill=Treatment)) + 
      geom_violin(trim=FALSE) +   facet_grid (Time ~ Duff) +
      labs(title=" ",x=" ", y = "ECM Root Colonization (%)")
p2 + theme_classic() + theme(legend.position="right") + stat_summary(fun=mean, geom="point", size=2, color="black") + theme(axis.title.x=element_blank(), axis.text.x=element_blank(),axis.ticks.x=element_blank()) + scale_fill_discrete(name = "Treatment", labels = c("B-M- B/B", "B-M- B/M (B)", "B-M- B/M (M)", "B-M- M/M", "B-M+ B/B", "B-M+ B/M (B)", "B-M+ B/M (M)", "B-M+ M/M", "B+M- B/B", "B+M- B/M (B)", "B+M- B/M (M)", "B+M- M/M", "B+M+ B/B", "B+M+ B/M (B)","B+M+ B/M (M)", "B+M+ M/M")) +
      theme(strip.background = element_rect(colour="black", fill="white", 
                                            size=1.5, linetype="solid")) +
      theme(strip.text.x = element_text(size=15, color="black",
                                        face="bold"))

因为 x 轴是“治疗”,这也是我的填充,所以我尝试使用以下代码释放比例,但这并没有改变绘图。

+   facet_grid (Time ~ Duff, scales="free")
+   facet_grid (Time ~ Duff, scales="free_x")

我也尝试过使用 facet_wrap 重新创建它,但没有成功。

我很乐意提供任何其他可能有用的信息。预先感谢您的任何建议!

下次请提供数据集,这看起来像你的数据集:

lbl = paste0(rep(c("B-","B+"),each=8),
rep(c("M-","M+"),each=4)," ",
rep(c("B/B","B/M(B)","B/M(M)","M/M"),2))

duff = rep(c("BS_MS","BS_MU","BU_MS","BU_MU"),each=4)
names(duff) = lbl

set.seed(111)

data = data.frame(
        Treatment = rep(lbl,10),
        ECM = rnorm(160),
        Time = rep(c("Final","Treatment"),each=80)
        )

data$Duff = duff[as.character(data$Treatment)]

如果你在一个基本情节上facet_grid(..scales="free"),它会起作用:

p2 = ggplot(data, aes(x=Treatment, y=ECM, fill=Treatment)) + 
geom_violin(trim=FALSE) +   facet_grid (Time ~ Duff,scales="free") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
p2

然后:

p2 + theme_classic() + theme(legend.position="right") + 
stat_summary(fun=mean, geom="point", size=2, color="black") +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank(),axis.ticks.x=element_blank()) +
scale_fill_discrete(name = "Treatment", labels = c("B-M- B/B", "B-M- B/M (B)",
"B-M- B/M (M)", "B-M- M/M", "B-M+ B/B", "B-M+ B/M (B)", "B-M+ B/M (M)",
"B-M+ M/M", "B+M- B/B", "B+M- B/M (B)", "B+M- B/M (M)", "B+M- M/M",
"B+M+ B/B", "B+M+ B/M (B)","B+M+ B/M (M)", "B+M+ M/M")) +
theme(strip.background = element_rect(colour="black", fill="white", 
                                        size=1.5, linetype="solid")) +
theme(strip.text.x = element_text(size=15, color="black",
                                    face="bold"))