Fourth session: 2022-10-14

session
visualization
r
git
latex
On the fourth session we talked about plot texts in ggplot and turning R projects into git repositories.
Author

Mariana Montes

Published

October 14, 2022

In this fourth session we looked at the following tips:

Showtext package

With the {showtext} package it is possible to load a variety of fonts and use them in the texts of your plot. This is particularly useful when writing to PDF from Quarto/Rmarkdown, since the default Latex fonts are serif fonts and the default plot fonts are sans serif (totally different!).

Our goal was to use Cambria in all plots of a document. The workflow we set on, after installing the package, was the following:

library(showtext)
filter(font_files(), str_starts(family, "Cam"))
font_add("cambria", "cambria.ttc", bold = "cambriab.ttf")
showtext_auto()

In order to find the names of that should go in the font_add() call, we also wrote dplyr::filter(font_files(), stringr::str_starts(family, "Cam")) to check the names of font files starting with “Cam”.

The showtext_auto() call makes sure that it’s available for the whole document, but we also set a general theme for the plots with ggplot2::theme_set(). The code below makes ggplot::theme_minimal() the default theme for all following plots, with a base font size of 14 and Cambria as the font, thanks to {showtext}.

library(ggplot2)
theme_set(theme_minimal(base_size = 14, base_family = "cambria"))

Chinese characters

Whereas HTML doesn’t normally give us problems, if we want PDF output from our Quarto/Rmarkdown file that prints Chinese characters, we need to add some latex packages. We can easily solve that by editing the YAML section as follows:

format:
  pdf:
    include-in-header: 
      text: |
        \usepackage{fontspec}
        \usepackage{xeCJK}
output:
  bookdown::pdf_document2:
    extra_dependencies:
        - fontspec
        - xeCJK

Defining the citation style in a Quarto file

As the Quarto docs state, the default citation style in a Quarto document is the Chicago Manual Style. If you want to use a different one, you may simply download a citation stylesheet (for example, from the Zotero repository), add it in your project and call it as the csl element in your YAML:

csl: unified-stylesheet-linguistics.csl

Creating an R project from an existing directory

We were met with the situation where we already had a folder with an R script and some data, but that it wasn’t an R project. It is always possible to create an empty R project and then fill it with your documents, but we can also do it on top of the existing data!

When we create an R project, we have the option to do it “on an existing directory”. We just select that option and choose the path to that directory, and it’s done! The directory has now the appropriate files for an R project.

However, this option does not include the checkbox to turn the project into a git repository. For that purpose (assuming your directory isn’t a repository already), you can use {usethis} functions to set up the repository.

usethis::use_git() will turn the project into a git repository and populate “.gitignore” with the appropriate file extensions. You will need to restart RStudio to let the Git tab appear. It will also give you the option of immediately staging and committing the existing files. You may do so at that moment or after restarting RStudio via the normal means. Once you have committed some files, you can link the repository to a GitHub remote and push it. One option is to create an empty repository in GitHub itself and follow the instructions to connect them; alternatively you can use usethis::use_github() (recommended: start with usethis::use_github(private = TRUE) if you want your repository to be initially private). If Git already knows you, a repository will be created with the name of your project and your files will be pushed. You’re set to go!