Skip to content
gaiatek edited this page Jul 11, 2012 · 11 revisions

_* This is my first attempt using this wiki and I didn't have more than 5 minutes to spare so please forgive ugliness* _

Given data.frame df :

df <- data.frame('variable'=rep(c('a','b','c','d','e'),2), value=c(1:5,3,5:2), group=c(rep('one',5),rep('two',5)))

or the one I used:
variable value group

  1. a 4 one
  2. b 7 one
  3. c 5 one
  4. d 1 one
  5. e 5 one
  6. a 3 two
  7. b 1 two
  8. c 9 two
  9. d 5 two
  10. e 5 two

Running the following command will generate the following figure :

library(ggplot2) ggplot(df, aes(variable, value, fill=group)) + geom_bar(position='stack') barplot.

Inversely, we could use the group as a x axis and stacked the values of variables a,b,c,d,e :

ggplot(df, aes(group, value, fill=variable)) + geom_bar(position='stack')

ggplot(df, aes(group, value, fill=variable)) + geom_bar(position='stack')

Hope this is what you were looking for. If your data variables are in separate columns, check out function reshape::melt() or as I suggested for summaries of variables, check out plyr::ddply() and all other **ply() magic functions!

Clone this wiki locally