Using R and ggplot to compare months across different years

Previously, I had used R to plot daily totals for a years worth of my walking and cycling activity. As I accumulated a second year of data, I wanted to see a month-to-month comparison of the 2 years. Once the data is prepared, this is very straight-forward using ggplot.

Using the biking data to illustrate, let’s assume we have a dataframe named bike.all.df, which contains two columns, a date column (exerdt) and a mileage (miles) for each day’s mileage for the two years in question (note, we don’t need really “zero days” in this dataframe as we did in the original post, as we are summing over days and they get washed out).

First we want to create a month and a year column for each row. Then using ggplot is straightforward

library(ggplot2)
library(scales)
bike.all.df$MO<-format(bike.all.df$exerdt,"%m")
bike.all.df$YR<-format(bike.all.df$exerdt,"%Y")
ggplot(bike.all.df, aes(MO, fill=YR)) + geom_bar(aes(weight=miles), position="dodge") + xlab("MONTH") +
  ylab("Miles") +  
  labs(title = "Bike Miles By Month for 2014 and 2015",fill = "")

Here are the plots produced for both biking and walking. As can be seen, it’s hard (for me) to cycle in winter weather, and my walking takes a back seat when I can cycle.

Bike_2014V2015_By_Month

Walk_2014V2015_By_Month