使用ggplot2创建每月/每年的日历图像 [英] Creating a monthly/yearly calendar image with ggplot2

查看:545
本文介绍了使用ggplot2创建每月/每年的日历图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我认为这将是一个15分钟的工作(创建所有月份的数据,使用 reshape 格式化一下,然后使用 facet_wrap ),但几乎立即遇到问题,如下图所示 - 尽管一周中的日子看起来不错,但月份的日子并不合适。在R中订购数据是我的克星;我真的应该得到这个处理。无论如何,下面的图片显示了我到目前为止的代码是低于那个。这只是一个有趣的项目,不是紧急的,但任何帮助和/或点缀欢迎。

  require(ggplot2)
require(scales)
需要(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_text(color =black,fill =white,label = mydf $ day)+
geom_text(label = mydf $ day,size = 4,颜色= 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()


解决方案

用相反的顺序创建 week.f 列。我们已经使用了%U(假设美国惯例,但是如果您希望英国惯例使用%W和还要更改 dow 的因子级别的顺序)。我们还允许任意日期输入计算月份的开始,并简化了 ggplot 调用。



<$ p $ (ggplot2)

输入< - as.Date(2013-09-11)#输入日期

st< - as.Date(cut(input,month))#计算开始月份
dates31 <-st + 0:30#st和接下来的30个日期(共31个)
日期< - dates31 [months(dates31)== months(st)]#仅在st的月份保留日期

week.ch< - 格式(日期,%U)#周数

mydf< - data.frame(
day = as.numeric(format(dates,%d)),
week.f = factor(week.ch,rev (格式(日期,%a),c(太阳报,星期一,星期二,星期三,星期四,星期五,星期六))


ggplot(mydf,aes(x = dow,y = week.f))+
geom_tile(color =black ,fill =white)+
geom_text(label = mydf $ day,size = 4)+
scale_x_discrete(expand = c(0,0))+
theme(axis.ticks = elem主题(panel.background =元素正确(填充=透明))+
主题(legend.position =元素_bnk())+
主题(axis.title = element_blank())+
主题none)


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


I started to make a calendar in Excel, but thought it might be easy to do in R instead. (Note: I'm aware of various heatmap calendar packages but I prefer to use ggplot2.) Ultimately I want to make a calendar that shows 12 months starting from a given date, with each month as a small box showing 4 or 5 weeks on the y-axis (week 1 at the top, week 4 or 5 at the bottom) and the days of the week (starting from Monday) along the x-axis.

I thought this would be a 15-minute job (create data for all months, format a bit using reshape then use facet_wrap) but ran into problems almost immediately as the plot below shows - the days of the month are not in order, although the days of the week seem okay. Ordering data in R is my nemesis; I really should get a handle on this. Anyway, the image below shows what I have so far and the code is below that. This is just a fun project, not urgent, but any help and/or embellishments welcome.

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()

解决方案

Create the week.f column with levels in reverse order. We have used "%U" (assuming US convention but if you want the UK convention use "%W" and also change the order of the factor levels of dow). Also we have allowed arbitrary date input calculating the start of the month and have simplified the ggplot call slightly.

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")

By the way, there is a cal function in the TeachingDemos package that can construct a one month calendar using classic graphics.

这篇关于使用ggplot2创建每月/每年的日历图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆