When plotting time series data, you might want to bin the values so that each data point corresponds to the sum for a given month or week. This post will show an easy way to use cut and ggplot2‘s stat_summary to plot month totals in R without needing to reorganize the data into a second data frame.
Let’s start with a simple sample data set with a series of dates and quantities:
# convert date variable from factor to date format: log$Date <- as.Date(log$Date, "%Y/%m/%d") # tabulate all the options here str(log)
> str(log) 'data.frame': 7 obs. of 2 variables: $ Date : Date, format: "2013-05-25""2013-05-28"... $ Quantity: num 9115451718
Next we need to create variables stating the week and month of each observation. For week, cut has an option that allows you to break weeks as you’d like, beginning weeks on either Sunday or Monday.
# create variables of the week and month of each observation: log$Month <- as.Date(cut(log$Date, breaks ="month")) log$Week <- as.Date(cut(log$Date, breaks ="week", start.on.monday =FALSE)) # changes weekly break point to Sunday log