How to set your APA 7 theme in RStudio

Many researchers in the social sciences often need to create APA-style figures. Fortunately, there is a little trick that makes it easier to automatically set up an APA theme in ggplot2.

Resarchers in the social sciences produce figures in the APA style fairly often. Be it for conferences, presentations, or for publications. Most of these figures need to be created in the APA style. I bet many of you, myself included, have created a theme in ggplot2 and copied it to every APA figure every time. Maybe you have already automated your process and managed to set up a custom APA theme in ggplot2. And yet, you can make your life even easier.

So in this tutorial, I want to show you how to set up a custom APA theme that will load every time you open RStudio or start a new R-project. Every plot you create will use your APA theme. I will show you how to set this option globally and how it is done for R-Projects.

The .Rprofile file

I got the idea for this trick from Wei Lin who wrote a fantastic article on how to boost your productivity in R. In his article, he showed how the .Rprofile file can be used to load packages, set enviromental variables, and set up a custom theme when loading a new R-Session. The R-Startup can be quite complicated (as you can see in this article, but we need not concern ourselves too much with this process for now.

You should know that there can be multiple .Rprofile files on your computer. The global .Rprofile file is usually located on your computer's home directory. You can find its location with the following code:

# install.packages("usethis")
usethis::edit_r_profile()

Once you run this code, your .Rprofile file should open and you should be able to edit it.

Rprofile file empty

Then each project can contain its own .Rprofile file. This file is executed only for this particular project in RStudio. This .Rprofile file takes precedence over the global file. If you need APA figures only for specific projects, it makes sense to put your theme in the project's .Rprofile file. However, if you are creating APA figures only, put your theme into the global .Rprofile file.

How to add your APA theme

First, let's look at what a plot looks like when we don't set an APA 7 theme:

Default R plot

Once you have decided which of the two .Rprofile files you want to use, paste the following code into the file. We will go through the code in a second.

.First <- function() {
  # Load packages
  library(tidyverse)
  
  # Set theme
  apa_theme <- theme(
    plot.margin = unit(c(1, 1, 1, 1), "cm"),
    plot.background = element_rect(fill = "white", color = NA),
    plot.title = element_text(size = 22, face = "bold",
                              hjust = 0.5,
                              margin = margin(b = 15)),
    axis.line = element_line(color = "black", size = .5),
    axis.title = element_text(size = 18, color = "black",
                              face = "bold"),
    axis.text = element_text(size = 15, color = "black"),
    axis.text.x = element_text(margin = margin(t = 10)),
    axis.title.y = element_text(margin = margin(r = 10)),
    axis.ticks = element_line(size = .5),
    panel.grid = element_blank(),
    legend.position = c(0.20, 0.8),
    legend.background = element_rect(color = "black"),
    legend.text = element_text(size = 15),
    legend.margin = margin(t = 5, l = 5, r = 5, b = 5),
    legend.key = element_rect(color = NA, fill = NA)
  )
  
  theme_set(theme_minimal(base_size = 18) +
            apa_theme)
}

Let's find out what happened here. First we defined a function called .First. Strictly speaking, you don't need this function if you just set the theme and load some packages. If, however, you want to reference some data from the .Rdata file, you need to put your code into the .First function (more on this in this post).

Then, we load each package we need for every new R session. I chose to load the tidyerse package, but you could just as well only load the ggplot package.

Next, we define the APA 7 theme. This is my personal take on the APA 7 theme, based on the official documentation. For this theme, I have removed the grid lines, made the axis title bold, increased the text size in general, and added some margin around the plot. You may want to customize your theme to suit your needs (for reference, check out my theme app on this website). We then run the theme_set function and add our theme to it. That's it.

If you restart your RSession now each figure should have your APA 7 theme:

APA 7 theme

You can easily undo this change by commenting out all the code in your .Rprofile file.

Sign up to receive updates on new tutorials and features. If you cannot find the e-mail, check your spam folder.

ggplot2tor

Tutorials, educational apps, cheat sheets and courses for you to master ggplot2

Creator and author

I am an Instructional Designer and a former educational scientist with a curiosity for web development and data visualization. Find me on my personal homepage or LinkedIn.