Thursday 15 April 2010

ggplot2 - combine facet_grid and boxplot -



ggplot2 - combine facet_grid and boxplot -

i want create facet-grid combined boxplots. facets should variable experiment boxplot should show value against fc every variable.the variable , experiment has multiple levels. need utilize gridextra? tried this, 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)

you can simulate info if there's much post (and, knowing levels of various factors helpful), on right track plot, needed add together interaction variable , value. did next facet_wrap utilize facet_grid:

class="lang-r prettyprint-override">library(ggplot2) # create reproducible simulated info 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 have created column combined variable , value factor instead of using interaction explicitly in ggplot call, create easier change/have more command x-axis labeling.

ggplot2 reshape2

No comments:

Post a Comment