Reputation: 2545
I want to make a facet-grid combined with boxplots. The facets should be the variable Experiment
The boxplot should show value against FC for every variable.The variable and Experiment has multiple levels.
Do I need to use gridExtra?
I tried this, which is obviously wrong:
df.m
FC Experiment variable value
1 -1.36811678 Ago2 Knockout Pos1 no
2 -0.59630399 Ago2 Knockout Pos1 yes
3 -0.09747337 Ago2 Knockout Pos1 no
4 1.71652821 Ago2 Knockout Pos1 yes
5 -1.13473546 Ago2 Knockout Pos1 no
6 -0.44012950 Ago2 Knockout Pos1 yes
ggplot(df.m,aes(value,FC,fill=as.factor(value))) +
geom_boxplot(notch=T) + facet_grid(~Experiment)
Upvotes: 1
Views: 11826
Reputation: 78852
You can simulate some data if there's too much to post (and, knowing the levels of the various factors would also be helpful), but you were on the right track for the plot, you just needed to add an interaction for variable
and value
. I did the following with facet_wrap
but you could just as easily use facet_grid
:
library(ggplot2)
# create reproducible simulated data
set.seed(1492)
dat <- data.frame(FC=runif(100, min=-2, max=2),
Experiment=rep(c("Ago1", "Ago2", "Ago3", "Ago4"), 25),
variable=rep(c("Pos1", "Pos2", "Pos3", "Pos4", "Pos5"), 20),
value=sample(c("yes", "no"), 100, replace=TRUE),
stringsAsFactors=FALSE)
gg <- ggplot(dat, aes(x=interaction(variable, value), y=FC))
gg <- gg + geom_boxplot(aes(fill=value))
gg <- gg + facet_wrap(~Experiment)
gg <- gg + labs(x="")
gg <- gg + theme_bw()
gg <- gg + theme(strip.background=element_rect(fill="black"))
gg <- gg + theme(strip.text=element_text(color="white", face="bold"))
gg
You could have created another column with the combined variable
and value
factor instead of using interaction
explicitly in the ggplot
call, which would also make it easier to change/have more control the x-axis labeling.
Upvotes: 8