R Tutorial 4

One of the biggest benefits of R is the ability to make publication-ready plots. However, R is notoriously finnicky about making plots, hence the joke on my homepage about promising your firstborn to the ggplot overlords. The package ‘ggplot2’ is the primary package used for making figures, but in a more advanced tutorial later, we’ll use a lot more packages too! All these packages add on to ggplot though, so it’s best to start here. One quick sidenote - if you loaded the ‘tidyverse’ package, you’ll actually already have ggplot2 loaded. Tidyverse is a cool package because it actually contains probably 85% of the “general use” type packages you’ll need to get around in R. All of these “extra” packages that tidyverse loads when it gets loaded are called dependencies. This is just a general coding term for things that depend on other things (get it?). Alright, let’s jump in to a few figures.


Let’s start with a scatterplot. The basic ggplot code always involves some form of the first line I have below, but then if you want a scatterplot versus a line plot versus a bar chart, etc., depends on the next line of code. ‘geom_point()’ indicates that I want a scatterplot (points).

ggplot(dataset, aes(x = xvar, y = yvar)) +

This will get you something that looks better than something out of SPSS, but definitely not publication ready. If I want to clean up the plot a little because I don’t like the grey background that ggplot uses as a base background, I can change that with one of the many themes I can choose from using the “theme_XYZ()” command. Explore a few of them!

ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point() + theme_classic()

One thing that I think might be missing from a plot like this is the regression line and/or the confidence interval around that line. To add this, I can just throw this piece of code in there:

ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point() + theme_classic() + geom_smooth(method='lm')

Because R keeps it real, the default is to also plot the confidence interval around the regression line. But if you don’t want this, you can add in a command within geom_smooth of ‘se = FALSE’.

So now we’re getting pretty close to a publication ready figure! Let’s customize this a little bit further - how about changing some of the things about the scattplot points? We can make them hollow, we can change their color, and we can change their size.

#hollow circles
ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point(shape = 1) + theme_classic() + geom_smooth(method='lm')
#colored circles
ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point(color = "blue") + theme_classic() + geom_smooth(method='lm')
#big cirlces
ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point(size = 4) + theme_classic() + geom_smooth(method='lm')

We can also change the type of regression line we have! You can do a solid line as we have it now, or you could also do a dotted line, a dashed line, etc., and you can also change the thickness of whatever line type you chose.

ggplot(dataset, aes(x = xvar, y = yvar)) +
  geom_point(size = 4, color = "blue", shape = 1) + 
  theme_classic() + geom_smooth(method='lm', linetype = "dashed", size = 1.5)

Visualizing Categorical Interaction Effects

We can also extend this scatterplot to an interaction visualization really easily, as long as it is a categorical moderator. We can also do it for continuous moderators, but it’s a bit more complicated and that’s for another tutorial.

The main way to specify a moderator is to use the “group” statement in your aes() command. You can additionally then assign any number of figure options to that same group variable, and the different groups will have different values.

ggplot(dataset, aes(x = xvar, y = yvar, group = modvar)) +
  geom_point(size = 4, color = modvar, shape = modvar) + 
  theme_classic() + geom_smooth(method='lm', linetype = modvar, size = 1.5)

Play with these options a bit! Customize them to fit your liking. You’ll be off and running in no time!

Saving out your Figures

Some journals require crazy-high dpi for your figures, but lucky for us, we can save out our figures at whatever resolution we want in R! All we’re going to do is use the “ggsave” command. We can do it one of two ways: 1) we can save the whole plot to an object, the call that object when we use the ggsave command, or 2) we can simply record whatever is currently showing on the “plots” panel in our current R session.

#saving as object before using ggsave
fig1 <- ggplot(dataset, aes(x = xvar, y = yvar, group = modvar)) +
  geom_point(size = 4, color = modvar, shape = modvar) + 
  theme_classic() + geom_smooth(method='lm', linetype = modvar, size = 1.5)
#recording whatever is currently plotted
fig1 <- recordPlot()

#saving at 500 dpi
ggsave(fig1, "Figure 1.jpeg", dpi = 500)

Another really cool R trick is that you can export your R figures as powerpoint files! I wish I had of known about this when I was first learning R because this means you can do 90% of the heavy lifting in making a figure in R, but it takes foreverrrr to do that last 10% because ggplot is a fickle beast. If you can’t remember or figure out how to change an axis title, labels, size of text, etc., you can simply export it to powerpoint and then just put the finishing touches on it there. This also is great for collaborations if you’re the methods person and are doing the figures; instead of the first author asking you to tweak this or that a bunch of times, you can send them a powerpoint file they know how to tweak themselves. Everyone wins! So to write to powerpoint, we need to install and/or load the ‘officer’ and ‘rvg’ packages.


read_pptx() %>%
  add_slide(layout = "Title and Content", master = "Office Theme") %>%
  ph_with_vg(code = print(fig1), type = "body") %>% 
  print(target = "fig1.pptx")

Alright, with these skeletons, you should be able to get some initial plotting off the ground. In the next R plotting tutorial I do, I’ll get into more group/categorical variable plotting. May the ggplot odds be ever in your favor!