Reputation: 1709
Is it possible to plot two bar facets, based on places "A" and "B", where in each facet I show "year" on the x-axis and the yearly sums on the y-axis?
Here is the toy example I've built:
DF<-data.frame(place=c("A", "A", "A", "B", "B"), amt=c(1, 1, 1, 5, 2), year = c(2000, 2000, 2002, 2001, 2002))
DF$year<-as.factor(DF$year)
I've tried using ddply to get my sums:
sums<-ddply(DF,.(year,place), summarise, sumAmt=sum(amt))
But i don't know how to use that in qplot
qplot(??? , data=sums, facets=.~place)
if i replace ??? = year then i'm simply showing counts. I've read that I should perhaps try using geom_bar(stat = "identity") and aes(x = factor(year), y = sumAmt)), but I don't understand how.
Upvotes: 2
Views: 238
Reputation: 887571
If you provide the 'x' and 'y' variables, it will plot accordingly.
qplot(year, sumAmt, data=sums, facets=.~place)
To get barplot, specify the geom
qplot(year, sumAmt, data=sums, facets=.~place, geom='bar', stat='identity')
You can also do this without creating a 'sums' dataset
qplot(year, amt, data=DF, stat='summary', fun.y='sum', facets=.~place)
If you are using ggplot
, the syntax is
ggplot(DF, aes(x=year, y=amt))+
stat_summary(fun.y='sum', geom='point')+
facet_wrap(~place)
Upvotes: 1