Search code examples
rdateggplot2histogramfrequency

Frequency count of dates


I have a data frame with a column of dates. I am trying to get a frequency count of each date. I was thinking that a histogram would visualize the data nicely, but maybe there is a better way? I was able to created a histogram of the data but it is not exactly what I was looking for. I was hoping to get each individual date on the x-axis and the frequency count on the y-axis.

I have done some programming in R but I have not done much visualizations in R. Any help would be greatly appreciated.

RawDates<- c("11/8/2017","12/6/2017","10/6/2017","12/6/2017","1/24/2018","9/5/2017","1/24/2018","2/21/2018","10/12/2017","1/22/2018","5/2/2018","1/24/2018","10/12/2017","1/22/2018","2/21/2018","5/2/2018","3/12/2018","5/3/2018","11/7/2017","12/5/2017","9/8/2017","10/6/2017","10/5/2017","11/3/2017","12/6/2017","2/21/2018","11/2/2017","12/5/2017","5/2/2018","1/24/2018","9/6/2017","11/2/2017","2/21/2018","5/2/2018","1/24/2018","11/8/2017","3/12/2018","5/3/2018","1/24/2018")
FormattedDates <- as.Date(RawDates, format = "%m/%d/%Y")

df <- data.frame(FormattedDates)

##This is whatI have already tried
hist(df$FormattedDates, "days", format = "%m/%d/%Y")

Solution

  • Here a simple ggplot2 solution:

    library(ggplot2)
    library(scales)
    
    ggplot(df) +
     geom_histogram(aes(x = FormattedDates)) +
     scale_x_date(labels = date_format("%m %d %Y"), date_breaks = "30 days") +
     theme(legend.position = "bottom",
           axis.text.x = element_text(angle = 45, hjust = 1))
    

    enter image description here