首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R- calendR包 - 私人定制专属日历

R- calendR包 - 私人定制专属日历

作者头像
DataCharm
发布2021-02-22 15:33:19
1.2K0
发布2021-02-22 15:33:19
举报

前言

前两天给大家派送了小编自己定制的2021年日历和月历,看到好多读者下载了,小编表示很欣慰?。上期推送可见:R可视乎|2021年日历大派送

今天来说说这个包吧,非常简单,比起ggplot2包绘制日历要简单的多。

R中的年历图

该软件包非常易容易使用,因为它仅包含一个命名函数calendR。默认情况下,如果未指定任何参数,则该函数将以横向形式创建当年的日历,并且所有文本将使用系统的语言,如下所示:

# install.packages("calendR") #直接install安装包
library(calendR)
calendR() # 默认为当前年份

如果不希望日历使用操作系统的语言,则可以对其进行修改。例如,使用英语:

Sys.setlocale("LC_ALL","English")

如果想创建其他年份的年度日历图,则可以在year参数上设定,如下所示:

calendR(year = 2021)

日历周开始的设定

默认情况下,日历的周数将从星期日开始。设定参数start="M"可以获得从星期一开始日历图。

calendR(year = 2021, start = "M")

为日子增添色彩

special.days参数可以为指定的日期添加颜色,special.col设置颜色,low.col设置其他日期的颜色。

比如下面就是将第9,19等日子设置为特定时间,其颜色为淡蓝色,其他颜色设置为白色。

calendR(year = 2021,
        start = "M",
        special.days = c(9, 19, 56, 79, 102,  # Days to color
                         126, 257, 300, 342),
        special.col = "lightblue",            # Color of the specified days
        low.col = "white")                    # Background color of the rest of the days

技巧:如果要突出日历中所有的周末,可以将special.days参数设置为"weekend",此快捷方式将立即为它们全部着色。

calendR(year = 2025,
        start = "M",
        special.days = "weekend") # Color all weekends

为了添加多个事件,您将需要创建一个NA值向量,该向量的长度与相应年份的天数相同。然后将事件添加到相应的日期,你需要在special.days参数中指定。

events <- rep(NA, 365)

# Set the corresponding events
events[40:45] <- "Trip"
events[213:240] <- "Holidays"
events[252] <- "Birthday"
events[359] <- "Christmas" 

# Creating the calendar with a legend
calendR(year = 2025,
        special.days = events,
        special.col = c("pink", "lightblue", # Colors
                        "lightgreen", "lightsalmon"),
        legend.pos = "right") # Legend to the right

R中的月历图

年度日历前面描述的功能也可用于月度日历中。但是月度日历还可以将文本添加到每月的某几天中。

为了创建月度日历,你需要指定年份和月份。

calendR(year = 2021, month = 8)

为日子增添色彩

与前面相同,使用special.days参数给指定日期加上颜色。

calendR(year = 2021, month = 8,
        special.days = c(1, 9, 12, 23, 28),
        special.col = "#bfe2f2",
        low.col = "white")

在日子里添加文字

使用月度日历图时,可以使用text参数向日期添加一些文本,并使用参数text.pos指定其位置。使用text.sizetext.col参数修改文本的大小和颜色。

calendR(year = 2021, month = 8,        # Year and month
        start = "M",                   # Start the week on Monday
        text = c("Running", "Running", # Add text (only for monthly calendars)
                 "Class"), 
        text.pos = c(5, 16, 25),       # Days of the month where to put the texts 
        text.size = 4.5,               # Font size of the text
        text.col = 4)                  # Color of the texts

添加月相

设置lunar = TRUE可以将月相添加到其日期中。lunar.col参数设置隐藏区域的颜色,lunar.size设置大小。

calendR(month = 2,  
        lunar = TRUE,         # Add moons to the calendar
        lunar.col = "gray60", # Color of the non-visible area of the moons
        lunar.size = 7)       # Size of the moons

学术日历

使用start_dateend_date创建学术日历。如果你想设置某个时间段(下面是2020年9月-2021年5月31日)的日历,非常使用科研人员,学生。请参阅以下示例:

calendR(start_date = "2020-09-01", # Custom start date
        end_date = "2021-05-31",   # Custom end date
        start = "M",               # Start the weeks on Monday
        mbg.col = 4,               # Color of the background of the names of the months
        months.col = "white",      # Color text of the names of the months
        special.days = "weekend",  # Color the weekends
        special.col = "lightblue", # Color of the special.days
        lty = 0,                   # Line type
        bg.col = "#f4f4f4",        # Background color
        title = "Academic calendar 2020-2021", # Title
        title.size = 30,                       # Title size
        orientation = "p")         # Vertical orientation

当然,我觉得打印出来可能效果更好用吧。这里只是给出一个简单的例子,你可以在这个基础上加上背景以及你喜欢的颜色,可以继续往下看。

私人定制

接下来,就是给日历加了背景以及根据直男审美把其他颜色进行了调整。下面给出上次大家说还不错的日历的源代码(具体pdf版本可在公众号中回复“日历”免费获得)。

可以使用pdf = TRUE将日历进行导出(默认为A4格式)。可以在doc_name参数中指定生成的PDF文件的名称。此外,你可以在几种纸张尺寸之间进行选择以保存日历,从"A6"到,"A0"。但是注意,可能需要微调一些字体尺寸来获得所需的输出。

如果想制作自己的日历,只需修改img的图片,存储的路径(默认在我的文档里)。

日历

img <- "C:/Users/ZLL/Desktop/5.jpg"

calendR(year = 2021,
        start = "M", 
        title.col = "white",
        # Weeks start on Monday
        mbg.col = "#cd853f",               # Background color of the month names
        months.col = "white",      # Color of the text of the month names
        weeknames.col = "white",
        
        special.days = "weekend",  # Color the weekends
        special.col = "#a9a9a9", # Color of the special.days
        lty = 0,                   # Line type (no line)
        weeknames = c("Mo", "Tu",  # Week names
                      "We", "Th",
                      "Fr", "Sa",
                      "Su"),
        title.size = 40,   # Title size
        orientation = "p",
        bg.img = img,
        pdf = TRUE,
        doc_name = "My_calendar_brown"
)

月历

这里和日历区别最大的点就是:月历要把每个月都输出,可以使用sapply函数,把所有月份输出。前面的invisible指的是:不显示图片(显示的话太费事了!保存再看更快)。

最后使用paste0()将字符串进行粘合(这个方法非常好用!),而这里的i是变化的,所以最后生成的pdf文件名不一样但很有规律(除了i不一样)。

img <- "C:/Users/ZLL/Desktop/5.jpg"
invisible(sapply(1:12 , function(i) calendR(year = 2021,month = i, pdf = TRUE,                            title.col = "white",
                                            # Weeks start on Monday
                                            mbg.col = "#cd853f",  
                                            # Background color of the month names
                                            months.col = "white",      # Color of the text of the month names
                                            weeknames.col = "white",
                                            special.days = "weekend",  # Color the weekends
                                            special.col = "gray60", # Color of the special.days
                                            lty = 0,                   # Line type (no line)
                                            bg.img = img,
                                            doc_name = file.path("C:\\Users\\ZLL\\Documents\\calendar", paste0("Calendar(gray)_2021_", i))))
                                            )

拓展阅读

其他相关资料,小编收集了一些,想继续研究的可见:

Calendar plot in R using ggplot2[1]

对应的github[2]

calendar详细介绍[3]

各位可视化爱好者可以根据教程设置私人化日历,也欢迎后台和小编联系,分享你制作的日历,我们可以制作一期私人日历展示活动!

对应代码与相关数据,请在我的github中获取(文末原文)。最近降温,大家注意保暖。来回杭州的动车上完成的这篇推文,喜欢请一键三连,创作不易,感恩不尽?。

参考资料

[1]

Calendar plot in R using ggplot2: https://r-coder.com/calendar-plot-r/

[2]

对应的github: https://rdrr.io/github/houyunhuang/ggcor/man/

[3]

calendar详细介绍: https://cran.r-project.org/web/packages/calendar/calendar.pdf

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-12-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 DataCharm 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • R中的年历图
    • 日历周开始的设定
      • 为日子增添色彩
      • R中的月历图
        • 为日子增添色彩
          • 在日子里添加文字
            • 添加月相
            • 学术日历
            • 私人定制
              • 日历
                • 月历
                  • 拓展阅读
                    • 参考资料
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档