This code-through is an introduction to the calendR package. The calendR package helps in making completely adjustable/customizable ggplot2 calendar plots with one function only. This package was created by José Carlos Soage González and Natalia Pérez Veiga. In this code-through, we will create ready to print yearly and monthly calendar plots in R and we will also view daily values of stock prices over the year using a calendar! You can read more about the package here
I found this package to be interesting because I always like to manage my schedule, time and activities, and after I found this package I can customize my own calendar in R!
You can use the below December Calendar to plan your month. Merry Christmas!
Installation of the calendR package
We started by calling the function calendR which will print for us the current yearly calendar. This package is simple as it only contains one function which is the calendR function. In the function we have many arguments that will help us customize our Calendar.However, If no argument is specified the function will return the current year in landscape orientation.
Here I will customize my yearly calendar .
I chose the below arguments:
mbg.col: Background color for month names(I chose 15 which represents the color yellow). The default is the color white.
months.col: Text color of the month names
start: This specifies on what day you would like your calendar to start . I chose M which specifies that I want my calendar to start on Monday.If not specified,the week starts on Sunday(Default).
special.days: This will help me choose the special days that I want highlighted in my calendar. In the example below I chose weekend however If you would like to color specific days then you have to specify the corresponding day of the year (from 1 to 365 or 366).
special.col: Color for the days indicated in special.days.
months.pos: specifies alignment of month names (eg. 0 will align month names to the left, 1 will align month names to the right, and 0.5 is the default which will align the months to the middle)
orientation: We have two orientations (Landscape and Portrait).The default orientation is landscape.
weeknames: Character vector with the names of the days of the week starting on Monday. By default they will be in the system locale.We can rename days of the week(I chose to abbreviate them).
bg.img:Character string containing the URL or the local directory of a image to be used as background.
If you are interested in saving this calendar , you can set the argument pdf=TRUE to save the calendars in a ready to print PDF format (A4 format by default). You can also set papersize = "A6" or choose any other papersize.
calendR(year = 2021,
mbg.col ="15", # Background color for the month names
months.col = "blue", # Color of text (Month names)
start = "M",
special.days = "weekend" , # Color days of the year
special.col = "#c0aee0", # Color of the special.days
months.pos = 0.5,
orientation = "l",
weeknames = c( "M" , "T" , "W" , "T" ,
"F" , "S" , "S" ),
bg.img = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS0W9mpmyUh550ONNzM19c3EyG4FHprELttwA&usqp=CAU") To add events or important dates, you will need to create a vector of NA values of the same length of the number of days of the year. Then, you can add the events to the corresponding days of the year on the vector, which you will need to specify inside the special.days argument.
Note : you will need to specify as many colors as events
imp.dates <- rep(NA, 365)
# Add the events to the desired days
imp.dates[12:62] <- "Session A-Spring Semester"
imp.dates[63:76]<-"Spring Break"
imp.dates[77:120]<-"Session B-Spring Semester"
imp.dates[c(140,180,185)]<-"Business Trip"
imp.dates[248]<-"Teacher's day"
imp.dates[333] <- "Birthday"
#Create a calendar with a legend
calendR(year = 2021, special.days = imp.dates,
special.col = c("lightcyan2", "tan",
"lightyellow", "rosybrown1","lightgreen","lightgrey"),
legend.pos = "right",
weeknames = c( "M" , "T" , "W" , "T" ,
"F" , "S" , "S" ),
mbg.col ="15",
months.col = "blue") Here I chose to assign certain colors to my important dates instead of having them randomly assigned. First, I created an object called desired_order, then I ordered the colors based on my desired order. The colors are displayed based on the levels of the factor of the categorical variable. Looking at the legend below , we see that the the important dates are ordered alphabetically.
imp.dates <- rep(NA, 365)
# Add the events to the desired days
imp.dates[12:62] <- "Session A-Spring Semester"
imp.dates[63:76]<-"Spring Break"
imp.dates[77:120]<-"Session B-Spring Semester"
imp.dates[c(140,180,185)]<-"Business Trip"
imp.dates[248]<-"Teacher's day"
imp.dates[333] <- "Birthday"
# Desired order and colors
desired_order <- c("Session A-Spring Semester",
"Spring Break","Session B-Spring Semester","Business Trip",
"Teacher's day","Birthday")
# Order the colors based on the desired order
ordered_colors <- c("lightcyan2", "tan",
"lightyellow", "rosybrown1","lightgreen","lightgrey")[order(desired_order)]
calendR(year = 2021, special.days = imp.dates,
special.col = ordered_colors,
legend.pos = "right",
weeknames = c( "M" , "T" , "W" , "T" ,
"F" , "S" , "S" ),
mbg.col ="15",
months.col = "blue") Here we specified the month of December.
You can find a description of the arguments in the below comments. note : You can only add text in monthly calendars.
calendR(year = 2020, # Year
month = 12, # Month
start = "M", # Start of the week ("M": Monday)
#Arguments for the title
title = "December", # Title name
title.size = 35, # Font size of the title
title.col = 2, # Color of the title
#Arguments for the subtitle
subtitle = "Merry Christmas!", # Subtitle Name
subtitle.size = 16, #Subtitle Size
subtitle.col = 9, #Color of the subtitle
#Customization
weeknames = c("S", "M", "T", "W", # Change week names
"T", "F", "S"),
special.days = "weekend", # color the weekends
special.col = rgb(0, 0, 1, 0.15), # Color of the special days
col = "#f2f2f2", # Color of the lines of the calendar
lwd = 2, # Width of the lines of the calendar
lty = 1, # Line type of the lines of the calendar
font.family = "sans", # Font family of all the texts
font.style = "bold", # Font style of the texts except the subtitle
weeknames.col = "red", # Color of the names of the days of the week
months.col = "red", # If month = NULL, is the color of the month names
days.col = 4, # Color of the number of the days
day.size = 3.5, # Size of the number of days
text ="Christmas Eve", # Add Text
text.pos = 24, # Days of the month where to put the texts
text.size=2,
low.col = "transparent",
bg.img="https://images.vexels.com/media/users/3/220089/list/39c7677421b580acfc0a1aa23ac0c0c8-frame-christmas-background-design.jpg")Our December Calender is ready!
Calendars are not only useful for personal use but are also useful to track changes over time. In the below code , I do not use the calendR function , instead I loaded the function calendarHeat. Let’s use the calendarHeat function to understand how stock prices change over the year!
The R code for calendarHeat() can be downloaded through Paul Bleicher’s Github page.
calendarHeat() makes the process much easier! We will call the function and specify the below arguments.
date : Dates for which the data needs to be plotted.
values: Values associated with those dates.
color: The color palette. Default is r2g (red to green). You can create your own palette by defining a vector as shown below.
ncolors: Number of colors for the heatmap
varname: Title for the chart
If we want to view daily values for the whole year, then Calendar Heat maps are especially useful. We can obtain the Moderna stock prices using the tidyquant package.
I chose Moderna Inc to view how the stock prices changed over time. Upon the release of the news in November that the mRNA-1273 met its primary efficacy endpoint in the first interim analysis of the Phase 3 COVE study with a vaccine efficacy of 94.5%, the stock prices went up!
MRNAStock <- as.data.frame(tidyquant::tq_get(c("MRNA"),get="stock.prices")) # get data using tidyquant
MRNAStock <- MRNAStock[year(MRNAStock$date) == 2020, ] # Using data for the current year
r2g <- c("#D61818", "#FFAE63", "#FFFFBD", "#B5E384")
calendarHeat(MRNAStock$date, MRNAStock$adjusted, ncolors = 99, color = "r2g", varname="MRNA Adjusted Close")I really enjoyed creating calendars and customising them . I hope you did too! In the above tutorial, we went through steps of creating a yearly calendar and adding events to certain dates , then we created a customized december calendar that you can use for this month . Finally , we created a calendar heat map that displays stock prices for Moderna Inc to show that calendars can be also useful if we are interested in visualising time-series data.