Skip to content

Instantly share code, notes, and snippets.

@walkerjeffd
Last active December 20, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerjeffd/6056406 to your computer and use it in GitHub Desktop.
Save walkerjeffd/6056406 to your computer and use it in GitHub Desktop.
Calendar Plot using ggplot2
require(lubridate)
require(plyr)
require(ggplot2)
theme_set(theme_bw())
# create random dataset
df <- data.frame(DATETIME = ymd("2000-01-01") + ddays(runif(100)*365*5))
# compute day/month columns
df <- mutate(df,
YEAR=factor(year(DATETIME)),
MONTH=factor(month(DATETIME, label=TRUE, abbr=TRUE)),
WEEKDAY=factor(wday(DATETIME, label=TRUE, abbr=TRUE), levels=c('Sun', 'Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat')),
MONTHDAY=mday(DATETIME),
MONTH_FIRST_WDAY=wday(ymd(paste(YEAR,MONTH,1,sep='-'))),
MONTHWEEK=factor(floor((mday(DATETIME)+MONTH_FIRST_WDAY-2)/7)+1, levels=c(1:6))
)
# plot
ggplot(df, aes(MONTHWEEK, WEEKDAY)) +
geom_tile(fill='black', color='white') +
facet_grid(YEAR~MONTH, drop=FALSE) +
labs(x="Week of the Month", y="Day of the Week") +
scale_y_discrete(drop=FALSE, limits = rev(levels(df$WEEKDAY))) +
scale_x_discrete(drop=FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment