简体   繁体   English

ggplot2,facet_grid 的 x 轴问题

[英]ggplot2, x-axis issues with facet_grid

I'm very new to R and trying to create a grid of violin plots for my data.我对 R 非常陌生,并试图为我的数据创建一个小提琴图网格。 I was able to get the grid layout I want it, however, when I make the grid, my plots are on the x-axis where they would be if they were all plotted together.我能够得到我想要的网格布局,但是,当我制作网格时,我的图在 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"))

Because the x-axis is "Treatment", which is also my fill, I tried freeing the scale with the following codes, but that didn't change the plots.因为 x 轴是“治疗”,这也是我的填充,所以我尝试使用以下代码释放比例,但这并没有改变绘图。

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

I've also tried to recreate it using facet_wrap, but was unsuccessful.我也尝试使用 facet_wrap 重新创建它,但没有成功。

I'm happy to include any other information that may be helpful.我很乐意提供任何其他可能有用的信息。 Thank you in advance for any suggestions!提前感谢您的任何建议!

Please provide the dataset next time, this is something that would look like your dataset:请下次提供数据集,这看起来像您的数据集:

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

If you do facet_grid(..scales="free") on a basic plot, it works:如果你在基本的 plot 上执行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

在此处输入图像描述

Then:然后:

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

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM