Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the same plot without the intermediate step of the amount aggregation?

How can I get the same plot without the intermediate compute of the aggregate column.

I have this data :

set.seed(1234)
dat <- data.frame(month = gl(3,1,20),
                  family= gl(5,1,20),
                  amount= sample(1:3,20,rep=TRUE))

Using this code , I get a barplot . Where each bar , is the sum of amount by family and by month. first I create a new aggegate column V1.

## I am using data.table , you can get it by ddply also
library(data.table)
dd <- data.table(dat)
hh <- dd[,sum(amount),by=list(month,family)]

Then I plot using this code :

ggplot(data=hh,aes(x=month,y=V1,fill=family))+
  geom_bar(stat = "identity")

To get this plot:

enter image description here

This works but I want simpler method. I think using stat_sum or other ggplot2 techniques I can do this without the intermediate aggregation step. something like this :

 ## don't run this doesn't work
 ggplot(data=dat,aes(x=month,y=amount,fill=family))+
  geom_bar(stat = "sum")
like image 362
agstudy Avatar asked Apr 24 '13 17:04

agstudy


2 Answers

ggplot(data=dat,aes(x=month,y=amount,fill=family,group=family))+
  geom_bar(stat = "summary",fun.y=sum)

enter image description here

like image 126
Roland Avatar answered Oct 12 '22 14:10

Roland


I found on R help mailing list that there is a an argument weight= than can be used to get sum of particular values instead of count in producing stacked bar charts. You have to supply x values, fill= values and weight=amount ensures that sum of amount values is used to make bar heights. This also automatically ensures that fill= values are arranged in the same order.

ggplot(dat,aes(month,fill=family,weight=amount))+geom_bar()

enter image description here

like image 34
Didzis Elferts Avatar answered Oct 12 '22 14:10

Didzis Elferts