使用ggplot2创建月度/年度日历图像

Slo*_*ner 8 r ggplot2

我开始在Excel中制作日历,但认为在R中可能很容易做到.(注:我知道各种热图日历包,但我更喜欢使用ggplot2.)最后,我想打一个日历,显示从指定日起12个月,每个月为显示在4或5周小盒子y轴(顶部的第1周,底部的第4周或第5周)和沿x轴的星期几(从星期一开始).

我认为这将是一个15分钟的工作(为所有个月的数据,使用一下格式reshape,然后使用facet_wrap),但遇到了问题,几乎立即如下所示的情节-这个月的日子不是为了,虽然天这一周似乎还可以.在R中订购数据是我的克星; 我真的应该对此有所了解.无论如何,下面的图片显示了我到目前为止的代码,代码低于该代码.这只是一个有趣的项目,并不紧急,但欢迎任何帮助和/或装饰.

日历

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"))
levels(mydf$dow) <- 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()
Run Code Online (Sandbox Code Playgroud)

G. *_*eck 6

week.f以相反顺序创建具有级别的列.我们已经使用过"%U"(假设美国惯例,但如果你想要使用英国惯例"%W",也要改变因子水平的顺序dow).此外,我们允许任意日期输入计算月初,并ggplot略微简化了呼叫.

library(ggplot2)

input <- as.Date("2013-09-11") # input date

st <- as.Date(cut(input, "month")) # calculate start of month
dates31 <- st + 0:30 # st and next 30 dates (31 in all)
dates <- dates31[months(dates31) == months(st)] # keep only dates in st's month

week.ch <- format(dates, "%U") # week numbers

mydf <- data.frame(
   day = as.numeric(format(dates, "%d")),
   week.f = factor(week.ch, rev(unique(week.ch))),
   dow = factor(format(dates, "%a"), c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"))
)

ggplot(mydf, aes(x = dow, y = week.f)) +
    geom_tile(colour = "black", fill = "white") +
    geom_text(label = mydf$day, size = 4) + 
    scale_x_discrete(expand = c(0,0)) +
    theme(axis.ticks = element_blank()) +
    theme(axis.title = element_blank()) +
    theme(panel.background = element_rect(fill = "transparent"))+
    theme(legend.position = "none")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

顺便说一句,cal在TeachingDemos包中有一个功能,可以使用经典图形构建一个月的日历.


Tom*_*eif 6

这行代码错误:

levels(mydf$dow) <- c('Mon','Tue','Wed','Thu','Fri','Sat','Sun')
Run Code Online (Sandbox Code Playgroud)

这是因为mydf$dow已经是一个因素,通过使用levels你只是重命名当前的水平.在这种情况下,你只是重命名"Fri" "Mon" "Sat" "Sun" "Thu" "Tue" "Wed"'Mon','Tue','Wed','Thu','Fri','Sat','Sun',这将造成你最后得到的混乱.

如果你看一下mydf你的例子,你会看到:

       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
Run Code Online (Sandbox Code Playgroud)

由于重命名,天数名称不再与日期匹配.

一旦纠正,一切都按预期工作:

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()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述