How to control space between stack bars in ggplot2?

Viewed 3771

I'm plotting a stack barplot in ggplot2. My dataset is like,

var1         var2   var3       value
treatment1  group_1 C8.0    0.010056478
treatment2  group_1 C8.0    0.009382918
treatment3  group_2 C8.0    0.003014983
treatment4  group_2 C8.0    0.005349631
treatment5  group_2 C8.0    0.005349631

var1 contains 5 treatments, these five treatments belong to two groups in var2, and each treatment has 14 measurements in var3, their value stored in value.

I want to make a plot to compare these five treatments, and their measurements. so I plot with stack bar plot like this figure: enter image description here

My code:

library(ggplot2)
colourCount = length(unique(mydata$var3))
getPalette = colorRampPalette(brewer.pal(14, "YlGnBu")) #get more color from palette

ggplot(data=mydata, aes(x=var1, y=value, fill=var3))+
  geom_bar(stat="identity", position="stack", colour="black", width=.2)+
  *#geom_errorbar(aes(ymax=var3+se, ymin=var3-se, width=.1))+*
  scale_fill_manual(values = getPalette(colourCount))+

  scale_y_continuous(expand = c(0, 0))+
  mytheme

How could I group the first two stacked columns together, and the other three columns together? Because they belong to two groups in var2.

2 Answers
Related