Given a list of dates
dates <- data.frame(foo = c(
"2009-03-15",
"2010-04-15",
"2011-06-16",
"2011-06-17",
"2011-06-17",
"2011-06-17",
"2011-06-17"))
I can make a histogram easily with the following command:
histo <- hist(as.Date(dates$foo), breaks = "months", freq=TRUE, plot=TRUE)
I can also make a barplot
barplot(histo$counts)
Note: I want a monthly histogram, so the proposed solution if possible must preserve the breaks = months
or equivalent.
I'm too lazy to figure out how to do this using base graphics. It's pretty easy in ggplot2
, though:
library(ggplot2)
library(zoo)
ggplot(data = dates,aes(x = as.Date(as.yearmon(foo)))) +
geom_bar() +
xlim(as.Date(c('2001-01-01','2011-07-20')))
Note that what you describe is by no means a histogram, but a bar chart. Converting back and forth from yearmon and then back to Date gives you the binning by month, but makes keeping the date scale easy.