calendR包—私人定制专属日历

简介: 前两天给大家派送了小编自己定制的2021年日历和月历,看到好多读者下载了,小编表示很欣慰😁。上期推送可见:R可视乎|2021年日历大派送今天来说说这个包吧,非常简单,比起ggplot2包绘制日历要简单的多。

前言


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

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


R中的年历图

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

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

image.gif

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

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

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

calendR(year = 2021)


日历周开始的设定

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

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

1[`E0)00E}XWR733]V7]KCJ.png


为日子增添色彩

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

9YZ37T20]63COUDZ3YT{)L8.png

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

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

image.gif

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

IK~B]P{{%C8YKI2TPNMZ3[D.png


R中的月历图

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

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

calendR(year = 2021, month = 8)

image.gif

为日子增添色彩

与前面相同,使用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

]SZ1C5K2UBIV_R6%6C33@3I.png


添加月相

设置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

image.gif

学术日历


使用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

E$IX4MJ%L]_00ZY905ABKIF.png

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


私人定制


接下来,就是给日历加了背景以及根据直男审美把其他颜色进行了调整。下面给出上次大家说还不错的日历的源代码(具体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"
)

R)SWS~X6G648~}H)RL{CNM2.jpg


月历

这里和日历区别最大的点就是:月历要把每个月都输出,可以使用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))))
                                            )

VT6]BF0@67]2U498A2QO~DS.png


拓展阅读

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

Calendar plot in R using ggplot2[1]

对应的github[2]

calendar详细介绍[3]

目录
相关文章
|
小程序
QT日历制作
QT日历制作
81 0
|
5月前
|
Python
用Python实现指定日期的日历
用Python实现指定日期的日历
|
11月前
lunar="false",日历插件不显示农历
lunar="false",日历插件不显示农历
82 1
|
数据可视化 数据挖掘
使用 ggTimeSeries 包构建日历图
使用 ggTimeSeries 包构建日历图
102 0
|
Java 数据安全/隐私保护 容器
背记不如实战系列-javaGUI实例-日历制作
背记不如实战系列-javaGUI实例-日历制作
147 0
|
前端开发 JavaScript 容器
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
344 0
开源炫酷日历、网页日历模板、自适应大小日历、win10日历
|
程序员 Android开发 开发者
Android开发:获取当前系统时间和日期的方法
最近接手了公司的一个Android项目,一直在处理Android项目的App的开发,作为半路起家来说,总结了一些Android开发的心得和知识点,然后就写下来记录一下,分享给有需要的开发者查阅交流。那么本篇博文就来分享一下在Android开发过程中,涉及到获取系统当前日期和时间的方法,知识点虽然很常见,但是很实用。
2150 0
Android开发:获取当前系统时间和日期的方法
关于调起系统日历预填信息问题
最近开发遇到一个问题,需要调起系统日历,添加日历事件,但是会出现有些手机无法预填信息的情况;在这里对这个问题做个小记录;
122 0
关于调起系统日历预填信息问题
|
Windows
私人定制日历代码改进
2021年即将到来,小编学习了calendR这个包,并写了两篇推送。分别为: R可视乎|2021年日历大派送 calendR包—私人定制专属日历 并开源了自己的代码在github上,但是细心的读者发现代码还存在可以优化的地方。
158 0
私人定制日历代码改进
介绍一个好用的日期倒计时工具
介绍一个好用的日期倒计时工具
122 0
介绍一个好用的日期倒计时工具