knitr::opts_chunk$set(echo = TRUE)

R Templates and Themes for the University of St. Thomas IDAR team.

Templates

There are two templates provided in this package that will knit report output:

  1. UST Word template
  2. UST PowerPoint template

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 theme

There are a number of functions developed to render UST-themed plots with ggplot2.

Set theme

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):

  1. Download this free version of Avenir Next here. (The reason we can’t use the version installed on our machine is that it is a .otf file which currently the extrafont package doesn’t support. The link provided is a traditional .ttf file.)
  2. Install this file by just double-clicking; it installs the font here: C:/Users/<your_username>/AppData/Local/Microsoft/Windows/Fonts/.
  3. Run the following id you don't have extrafont already installed: install.packages("extrafont).
  4. Then run the following code (you will only need to do this once): extrafont::font_import("C:/Users/<your_username>/AppData/Local/Microsoft/Windows/Fonts/").
  5. Restart your R session (In RStudio, Session > Restart R).
  6. Now use one of the templates above, and load all the packages at top of the .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.

Color palette

scale_color_ust(): color ggplot2 with UST colors. Arguments:

scale_fill_ust(): fill ggplot2 with UST colors. Same arguments as above.

Examples

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."
  )


UniversityOfSaintThomas/ustr documentation built on April 23, 2020, 12:44 a.m.