Given the following case:
a <- c(rep("A", 25), rep("B", 25))
b <- rep(as.Date(c("2007-01-01")) + seq(60,1500,60),2)
c <- runif(50, 0, 1000)
d <- data.frame(a,b,c)
If I visualize this using ggplot2 I get a nice bar chart
ggplot(data = d, aes(x=b, y=c, fill=a)) +
geom_bar(stat = "identity") +
scale_x_date(breaks = as.Date(c("2007-01-01", "2008-01-01", "2009-01-01", "2010-01-01", "2011-01-01")),
minor_breaks = NULL)
Note: One bar represents one observation.
However, I would like to be able to quickly group these observations together and have only 1 bar chart per year or 1 bar chart for every 2 years.
How is it possible to do this using ggplot2?
ggplot2
does not do aggregation by itself. You can use the base R aggregate
function for it or packages like dplyr
, plyr
or data.table
.
A data.table
solution would be:
require(data.table)
require(ggplot2)
setDT(d)[,b := as.IDate(b)]
ggplot(d[,sum(c), by=.(a, year(b))], aes(x=year, y=V1, fill=a)) +
geom_bar(stat = "identity")
Obviously you can take whatever statistic you like instead of sum
and also group by something else then year in the d[,sum(c), by=.(a, year(b))]
call.
The Result would be:
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