I'm very new to R and trying to create a grid of violin plots for my data. 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.
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.
+ 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.
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:
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"))