I am trying to create a barplot using ggplot2
where I am stacking by one variable and dodging by another.
Here is an example data set:
df=data.frame(
year=rep(c("2010","2011"),each=4),
treatment=rep(c("Impact","Control")),
type=rep(c("Phylum1","Phylum2"),each=2),
total=sample(1:100,8))
I would like to create a barplot where x=treatment
, y=total
, the stacked variable is type
and the dodged variable is year
. Of course I can do one or the other:
ggplot(df,aes(y=total,x=treatment,fill=type))+geom_bar(position="dodge",stat="identity")
ggplot(df,aes(y=total,x=treatment,fill=year))+geom_bar(position="dodge",stat="identity")
But not both! Thanks to anyone who can provide advice.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…