Relationships

Materials for class on Tuesday, September 26, 2017

Contents

Slides

Download the slides from today’s lecture.

First slide

Miscellaneous R stuff

Download Comic Relief data and place it in a folder named data in your project: Comic Relief 2015–2016 grants

Searching for code on GitHub

GitHub is an excellent source of real-world code, and it’s easy to search for examples of things other people have written. Use the advanced search to limit the search language to R.

Installing packages from GitHub

Not all R packages are hosted on CRAN—many are repositories that live on GitHub. It’s possible to install packages from GitHub, but you can’t use RStudio’s “Packages” panel. Most non-CRAN packages will include instructions of how to install (such as this package for ridge plots), but the general syntax is this:

Pipes (%>%)

Pipes let you concatenate nested functions in a human-readable form and can make it a lot easier to understand your code:

Pipes are even more helpful when manipulating data frames. For instance, typically it’s best to load the raw data as a data frame, clean it with a chain of functions, and then save it as a separate data frame:

Working with ggplot objects

You can save the output of a ggplot plot to an object (p1 here):

When you save ggplot plots as objects, you can do fun things with them, like add additional layers without repeating all the code from the base plot:

… or saving the plot to your computer:

… or arranging multiple plots:

If you want to save one of these combined plots to an object, you need to use arrangeGrob() instead of grid.arrange():

If you want to plot a saved arrangeGrob object, you have to use these two functions:

Fonts

It’s relatively easy to use custom fonts in ggplot plots, but there are some tricks. Read this post for more details and for instructions for how to do it on macOS and Windows.

Colors

You can specify colors in R in a few different ways:

Theme options

You can make adjustments to plots with the theme() function, but there are a billion different options to play with. To simplify life, you can install a GUI interface for manipulating themes with the ggThemeAssist package. Once it’s installed, you can access it in RStudio with the “Addins” menu at the top of the window. You must have a ggplot object selected for it to work.

ggThemeAssist will generate a bunch of settings inside a theme() function. If you want to reuse those settings in other plots, you can assign them to a variable and then add that variable as a layer to other plots:

Feedback for today

Go to this form and answer these three questions (anonymously if you want):

  1. What new thing did you learn today?
  2. What was the most unclear thing about today?
  3. What was the most exciting thing you learned today?