How can I get the same plot without the intermediate step of the amount aggregation?
ggplot2, histogram, r
Solution
ggplot(data=dat,aes(x=month,y=amount,fill=family,group=family))+
geom_bar(stat = "summary",fun.y=sum)
Problem
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: 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") ```