Last active
December 20, 2015 02:29
-
-
Save walkerjeffd/6056406 to your computer and use it in GitHub Desktop.
Calendar Plot using ggplot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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