knitr::opts_chunk$set(echo = TRUE)
R Templates and Themes for the University of St. Thomas IDAR team.
There are two templates provided in this package that will knit report output:
Once you've installed the package, you can create a new templated .Rmd file by running the following code:
#Word doc rmarkdown::draft(file = "YYYY-MM-DD_name-of-project.Rmd", template = "ust-template-doc", package = "ustr", create_dir = FALSE, edit = FALSE) #PowerPoint presentation rmarkdown::draft(file = "YYYY-MM-DD_name-of-project.Rmd", template = "ust-template-ppt", package = "ustr", create_dir = FALSE, edit = FALSE)
The reason it's better to run this code rather than doing File > New File > R Markdown, and choosing it in the dialog box in RStudio is because RStudio currently creates a subdirectory inside your project. We don't want this behavior. There is an issue on their Github repo to get this fixed, but for now the workaround is to just run the code in the console.
ggplot2 themeThere are a number of functions developed to render UST-themed plots with ggplot2.
Insert the following at the top of your scripts to set basic font and minimal theme to your plots:
theme_set_ust_doc() theme_set_ust_ppt()
Note: this requires an install of the Avenir Next font. You will need to follow these steps to get the font installed and loaded to use in R graphics (ggplot2):
.otf file which currently the extrafont package doesn’t support. The link provided is a traditional .ttf file.)C:/Users/<your_username>/AppData/Local/Microsoft/Windows/Fonts/.extrafont already installed: install.packages("extrafont).extrafont::font_import("C:/Users/<your_username>/AppData/Local/Microsoft/Windows/Fonts/")..Rmd template file; or run the following:library(tidyverse) library(scales) library(extrafont) #the three packages above are required for ustr to work. library(ustr)
An alternative to using Avenir Next is the following functions which uses Calibri, a font installed on virtually any Windows machine:
theme_set_ust_calibri_doc() theme_set_ust_calibri_ppt()
There are doc and ppt versions of each theme; the are identical except ppt has the text sized bumped up by 2 for PowerPoints.
scale_color_ust(): color ggplot2 with UST colors. Arguments:
palette = "main", "purples", "tropical" or "cool" (default = "main")discrete = TRUE or FALSE (default = TRUE)reverse = TRUE or FALSE (default = FALSE)scale_fill_ust(): fill ggplot2 with UST colors. Same arguments as above.
library(tidyverse) library(extrafont) library(scales) library(ustr) ustr::theme_set_ust_ppt() ggplot(mtcars, aes(x = hp, y = mpg, color = factor(gear))) + geom_point(size = 4) + scale_color_ust(palette = "main", discrete = TRUE) + #these are the defaults. labs( title = "This is a title", subtitle = "This is a subtitle", caption = "This is a caption." )
Here, I am specifying discrete = FALSE to get a gradient scale.
ggplot(mtcars, aes(x = hp, y = mpg, color = wt)) + geom_point(size = 4) + scale_color_ust(palette = "cool", discrete = FALSE) + #using the cool theme and a continuous scale which interpolates between colors by using discrete = FALSE. labs( title = "This is a title", subtitle = "This is a subtitle", caption = "This is a caption." )
Here, I am reversing the palette.
mtcars %>% count(gear) %>% mutate(gear = fct_reorder(as.factor(gear), n)) %>% ggplot(aes(x = gear, y = n, fill = gear)) + geom_col() + coord_flip() + scale_fill_ust(palette = "tropical", reverse = TRUE) + #tropical theme and reversing. theme(legend.position = "none") + labs( title = "This is a title", y = element_blank(), subtitle = "This is a subtitle", caption = "This is a caption." )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.