Search code examples
rdateggplot2widthbins

ggplot2: Group histogram data by year


I have a bunch of data with a YYYY-MM-DD date attached to it, and I'm having trouble getting a single bar for each year. In other words, all data from 2014 showing up under a bar for 2014.

The dates were converted to the YYYY-MM-DD format using

df1$Close.Date <- as.Date(df1$Close.Date, "%m/%d/%Y")

Here's my histogram formula that doesn't group correctly

ggplot(df1, aes(Close.Date, fill=Stage)) + geom_bar()

I've tried messing around with breaks and binwidth with no success


Solution

  • I fixed it by: adding this line df1$yr <- strftime(df1$Close.Date, "%Y") and then replacing Close.Date with yr in the ggplot line.