简体   繁体   English

带或不带ggplot2的多面分组箱线图r

[英]Faceted grouped boxplot r with or without ggplot2

I have a set of experiments done over 10 phases or months. 我有一组超过10个阶段或几个月的实验。 I'm growing bacteria of 3 different TYPES and counting the growth (ACC). 我正在培养3种不同类型的细菌,并计算其生长(ACC)。

I'm trying to get a facet_wrap grouped boxplot of growth over different phases for the three bacteria types (A,E and H). 我正在尝试获取三种细菌类型(A,E和H)在不同阶段生长的facet_wrap分组箱线图。 My data: 我的资料:

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

Some representative data that doesn't work for some reason: 由于某些原因而无法使用的一些代表性数据:

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

I'm trying ggplot2 although if it can be done without that's ok too. 我正在尝试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))

Here's what I've managed so far but can't get it to facet: 到目前为止,这是我已经设法完成的工作,但无法理解:

在此处输入图片说明

Something like the 3rd graph down on this post looks good: ggplot: arranging boxplots of multiple y-variables for each group of a continuous x 类似于该帖子上的第3张图,看起来不错: ggplot:为连续x的每组安排多个y变量的箱形图

EDIT 编辑

New code is close but I've had to change ACC to numeric. 新代码即将结束,但我不得不将ACC更改为数字。 Can I get the labels back as NG, SG, LG, MG, HG on the y axis? 我可以在y轴上将标签取回为NG,SG,LG,MG,HG吗?

ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) + 
  geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ phase)

在此处输入图片说明

Final code: 最终代码:

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

And Result: 结果: 在此处输入图片说明

ggplot2 uses non-standard evaluation. ggplot2使用非标准评估。 It looks for variables in the data.frame passed to its data parameter. 它在传递到其data参数的data.frame中寻找变量。 So you can simply do this: 因此,您可以简单地执行以下操作:

ggplot(EAH, aes(x=Phase, y=ACC)) + 
  geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ Phase)

And of course, R is case sensitive. 当然,R是区分大小写的。

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

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