[英]Faceted grouped boxplot r with or without ggplot2
我有一组超过10个阶段或几个月的实验。 我正在培养3种不同类型的细菌,并计算其生长(ACC)。
我正在尝试获取三种细菌类型(A,E和H)在不同阶段生长的facet_wrap
分组箱线图。 我的资料:
head(EAH)
ACC sample site bed phase X.M.SA TYPE
1 SG A 1 0 1 NO E
2 SG A 2 0 1 NO A
3 MG A 3 0 1 NO H
4 SG A 4 0 1 NO A
5 LG A 1 0 2 NO E
6 LG A 2 0 2 NO H
由于某些原因而无法使用的一些代表性数据:
EAH<- data.frame(ACC=factor(sample(1:5,10,replace=T), label=c("NG","SG","LG","MG","GH")),
Phase=factor(seq(1,10,1)),
TYPE=factor(sample(1:3,10,replace=T), label=c("A","E","S"),replace=T))
我正在尝试ggplot2,尽管如果没有它也可以完成。
ggplot(EAH, aes(x=as.factor(EAH$phase), y=EAH$ACC, group=EAH$TYPE)) +
geom_boxplot(aes(fill=factor(EAH$TYPE)))+ facet_grid(. ~ as.factor(EAH$phase))
到目前为止,这是我已经设法完成的工作,但无法理解:
类似于该帖子上的第3张图,看起来不错: ggplot:为连续x的每组安排多个y变量的箱形图
编辑
新代码即将结束,但我不得不将ACC更改为数字。 我可以在y轴上将标签取回为NG,SG,LG,MG,HG吗?
ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) +
geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ phase)
最终代码:
library(RColorBrewer)
library(ggplot2)
ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) +
geom_boxplot(aes(fill=TYPE))+
facet_grid(. ~ phase) +
labs(x = "Phase", y = "Growth",color="Type" )+
scale_fill_brewer(palette="Blues")+
theme_bw()+
theme(strip.background=element_rect(fill="black"))+
theme(strip.text=element_text(color="white", face="bold"))+
scale_y_discrete(breaks=c("1", "2", "3","4","5"),
labels=c("NG", "SG", "LG","MG","HG"))
ggplot2使用非标准评估。 它在传递到其data
参数的data.frame中寻找变量。 因此,您可以简单地执行以下操作:
ggplot(EAH, aes(x=Phase, y=ACC)) +
geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ Phase)
当然,R是区分大小写的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.