calendR包—私人定制专属日历

前言

前两天给大家派送了小编自己定制的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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kLHPeK4N-1607942382402)(https://imgkr2.cn-bj.ufileos.com/2a0d3258-eac2-43a3-a7ba-45f513feb48c.jpg?UCloudPublicKey=TOKEN_8d8b72be-579a-4e83-bfd0-5f6ce1546f13&Signature=H2lVusnh33GCFRMU7hq7RFsg9iI%253D&Expires=1608013868)]

为了添加多个事件,您将需要创建一个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

对应的github

calendar详细介绍

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

对应代码与相关数据,请在我的github中获取(文末原文)。喜欢请一键三连,创作不易,感恩不尽😄。

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值