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")
ggplot(data=dat,aes(x=month,y=amount,fill=family,group=family))+
geom_bar(stat = "summary",fun.y=sum)
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With