ggplot2,facet_grid 的 x 轴问题

Posted

技术标签:

【中文标题】ggplot2,facet_grid 的 x 轴问题【英文标题】:ggplot2, x-axis issues with facet_grid 【发布时间】:2021-07-28 09:42:12 【问题描述】:

我对 R 非常陌生,并试图为我的数据创建一个小提琴图网格。我能够得到我想要的网格布局,但是,当我制作网格时,我的图在 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 重新创建它,但没有成功。

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

【问题讨论】:

欢迎来到 SO!为了让其他人更容易地帮助您,创建一个最小的可重现示例会很棒! ***.com/questions/5963269/… 【参考方案1】:

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

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"))

【讨论】:

以上是关于ggplot2,facet_grid 的 x 轴问题的主要内容,如果未能解决你的问题,请参考以下文章

ggplot2:从图中删除未使用的因子水平组合的方面(facet_grid)

ggplot2 facet_grid 带有构面标题

如何使用不同的几何图形将边际总计添加到 ggplot2 facet_grid 图

在 ggplot2 facet_grid 中旋转切换的构面标签

R语言ggplot2可视化分面图(facet_grid):ggplot2可视化为分面图的每个组添加平均值线条

带有 facet_grid 的 ggplot2 中具有多个分类变量的堆积条形图