This line of your code is incorrect:
levels(mydf$dow) <- c('Mon','Tue','Wed','Thu','Fri','Sat','Sun')
This is because mydf$dow
already a factor, and with levels
you simply rename the current levels. In this case, you simply rename "Fri" "Mon" "Sat" "Sun" "Thu" "Tue" "Wed"
to 'Mon','Tue','Wed','Thu','Fri','Sat','Sun'
and that will create the mess that you get at the end.
If you look at mydf
in your example, you will see:
mydate month week day dow 1 2013-09-01 9 35 1 Thu 2 2013-09-02 9 36 2 Tue 3 2013-09-03 9 36 3 Sat 4 2013-09-04 9 36 4 Sun 5 2013-09-05 9 36 5 Fri 6 2013-09-06 9 36 6 Mon 7 2013-09-07 9 36 7 Wed
Due to renaming, day names no longer match dates.
After the fix, everything works as expected:
require(ggplot2) require(scales) require(lubridate) date.start <- as.Date('2013-09-01') date.end <- date.start + months(1) mydf <- data.frame(mydate = seq(as.Date(date.start), as.Date(date.end) - days(1), by = 'day')) mydf$month <- month(mydf$mydate) mydf$week <- week(mydf$mydate) mydf$day <- day(mydf$mydate) mydf$dow <- as.factor(format(mydf$mydate, format = "%a")) mydf$dow <- factor(mydf$dow, levels=c('Mon','Tue','Wed','Thu','Fri','Sat','Sun')) ggplot(mydf, aes(x = dow, y = as.factor(week))) + geom_tile(colour = "black", fill = "white", label = mydf$day) + geom_text(label = mydf$day, size = 4, colour = "black") + scale_x_discrete(expand = c(0,0)) + theme(axis.ticks = element_blank()) + theme(axis.title.y = element_blank()) + theme(axis.title.x = element_blank()) + theme(panel.background = element_rect(fill = "transparent"))+ theme(legend.position = "none") + theme()
![enter image description here](https://fooobar.com/undefined)