1

I have a set of experiments done over 10 phases or months. I'm growing bacteria of 3 different TYPES and counting the growth (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). 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.

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:

enter image description here

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

EDIT

New code is close but I've had to change ACC to numeric. Can I get the labels back as NG, SG, LG, MG, HG on the y axis?

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

enter image description here

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: enter image description here

Community
  • 1
  • 1
HCAI
  • 2,213
  • 8
  • 33
  • 65
  • This is a faq. You already told ggplot2 where to look for the variables. Do not use EAH$! – Roland Apr 15 '16 at 10:27
  • Thank you for looking at this. I've tried at least 10 different ggplot codes, mostly from stackoverflow but none work for me. I can't understand what this error means: Error in layout_base(data, cols, drop = drop) : At least one layer must contain all variables used for facetting. Also why shouldn't I use EAH$!? – HCAI Apr 15 '16 at 11:13

1 Answers1

1

ggplot2 uses non-standard evaluation. It looks for variables in the data.frame passed to its data parameter. 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.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Thank you very much for the example, I really appreciate it. I've changed it to: ggplot(EAH, aes(x=TYPE, y=as.numeric(ACC))) + geom_boxplot(aes(fill=TYPE))+ facet_grid(. ~ phase) , which gives the correct split but as I had to make ACC numeric, I can't see the levels on the yaxis (see my edit in my post). What do you think? – HCAI Apr 15 '16 at 11:44
  • A boxplot must have a numeric y-axis (and must be based on numeric values). What you show there is not a boxplot. In fact, I'm not able to understand your plot. – Roland Apr 15 '16 at 12:20
  • R converted the growth levels (No growth, scanty growth, etc) to numeric values 1,2,3 etc to plot them. Is a boxplot the wrong way to display this type of data? Essentially I have an ordinal categorical y axis here... – HCAI Apr 15 '16 at 12:39
  • A boxplot is for continuous data. You don't have that and consequently the boxplot is the wrong tool for you. – Roland Apr 15 '16 at 12:45