BuildeR

Simon Wallace

6 minute read

With the new year upon us it is a time to reflect on the past twelve months and think about how we are going to melt away all those holiday calories. This time last year I set a goal which was to get 52 running days in a year with a minimum of a 5k run, whilst I didn’t complete this goal in 2017 it motivated me to see how my performance fluctuated over the course of a year.

Simon Wallace

2 minute read

I was working with Tableau one day and saw that they had a calendar heatmap, thinking I might get use of that someday I built it in ggplot2. Required functions library(tidyverse) getMonthWeek <- function(dates,weekStart=c('Monday','Sunday')){ match.arg(weekStart) out <- list() if(weekStart=='Monday'){ dowNumber <- data.frame('Day'=c('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),'DayNumber'=1:7) }else{ dowNumber <- data.frame('Day'=c('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),'DayNumber'=1:7) } for(month in unique(dates$Month)){ temp <- filter(dates,Month==month) temp$MonthWeek <- 0 temp$MonthWeek[temp$Day==weekStart] <- 1 temp$MonthWeek <- cumsum(temp$MonthWeek)+1 out[[length(out)+1]] <- temp } final <- merge(do.call(rbind,out),dowNumber) final <- final[order(final$Date),] final$Day <- factor(final$Day,levels=dowNumber$Day) final$Month <- factor(final$Month,levels=c('January','February','March', 'April','May','June','July','August', 'September','October','November','December')) final$YearWeek <- 0 final$YearWeek[final$Day==weekStart] <- 1 final$YearWeek <- cumsum(final$YearWeek)+1 return(final) } getCalendar <- function(start,end,weekStart='Monday'){ # Only works with dates in the format %Y-%m-%d dates <- seq(start, end, by="+1 day") days <- weekdays(dates) month <- months(dates) year <- unlist(lapply(strsplit(as.