Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stacked bars within grouped bar chart

Tags:

I have the following graph

test <- expand.grid('cat' = LETTERS[1:5], 'cond'= c(F,T), 'year' = 2001:2005)
test$value <- floor((rnorm(nrow(test)))*100)
test$value[test$value < 0] <- 0

ggplot() +
  geom_bar(data=test, aes(y = value, x = year, fill = cat), stat="identity",position='dodge') +
  theme_bw()

test and I need to divide each 'cat' by 'cond'(true or false). How do I do that?

like image 822
dmvianna Avatar asked Nov 21 '12 04:11

dmvianna


1 Answers

You can put cat on the x-axis and use facet_grid with year:

ggplot() +
  geom_bar(data=test, aes(y = value, x = cat, fill = cond), stat="identity",
           position='stack') +
  theme_bw() + 
  facet_grid( ~ year)

enter image description here

like image 123
Sven Hohenstein Avatar answered Oct 05 '22 02:10

Sven Hohenstein