Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group by month, quarter or year using ggplot2

Tags:

r

ggplot2

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)

1

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?


1 Answers

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:

enter image description here

like image 174
Rentrop Avatar answered Sep 14 '25 06:09

Rentrop