0

In R, I have created a histogram of data points per month. I would like the tick marks centered under the bars and to keep the month/year labels. Any help is much appreciated!

My code is:

hist(d$date, "months", freq=TRUE, main="Number of locations by month",  ylim=c(0,8000), xlab="Month", format = "%b %Y", xaxt="n") axis(side=1,at=d$mids, labels=???????????)

1 Answers1

2

It sounds like you are expecting hist to automatically break at every month. That's not necessarily the case. hist tries to do a reasonable job of estimating an underlying density function. It's not necessarily for visualizing counts. You could explicitly set the breaks= argument if you like. Although this sounds more like a barplot to me. Here's how I might generate such a plot

#sample data
d<-data.frame(date=sample(seq.Date(as.Date("2001-01-01"), 
    as.Date("2001-12-31"), length.out=20), 30, replace=T))

#summarize data by months
mybreaks=seq.Date(as.Date("2001-01-01"), as.Date("2002-01-01"), by="month")
tt<-table(cut(d$date, breaks=mybreaks))
names(tt)<-strftime(mybreaks[-length(mybreaks)], format="%b %Y")

#plot results
barplot(tt)

monthly bar plots

MrFlick
  • 195,160
  • 17
  • 277
  • 295