Week 2 Homework

Themes, colors, and saving a graph

1 Introduction

This week you are going to work exclusively in RStudio. You will work in the project you worked in for your Week 1 Homework. You will become familiar with our tools for applying themes, applying custom color palettes, and saving graphs for use in a presentation or report.

Follow the instructions below to complete this homework.

2 Step-by-step instructions

2.1 Preparation

  1. Start RStudio and open the project you created for your Week 1 Homework.
  2. At the Console prompt, do the following in order to install the themefurmanu package on your computer.
devtools::install_github("scottamoore/themefurmanu")
  1. Create a new R file in the project directory. Name it week2-homework.R.
  2. At the top of this file, put the following code:
library(tidyverse)
library(themefurmanu)
  1. Save the file.
  2. Execute those two lines of R code in the Console to load the libraries.
  3. To verify that you have the themefurmanu package installed, run the following code in the Console:
show_furmanu_palettes()

The result should show in the Plots pain in the bottom right corner of the RStudio window. If you see a list of color palettes, you are good to go. If you see an error message, you need to install the themefurmanu package.

  1. Now run the following in the Console:
open_furmanu_demo()
  1. This will open a new qmd (Quarto Markdown) file in the RStudio editor called demo.qmd. This file contains a demo of the themefurmanu package, showing how to use both the theme and the color palettes. You need to compile this document to see the results. To do this, click on the Render button in the top center of the RStudio editor window containing the demo file. This will create a new HTML file called demo.html in the same directory as the demo.qmd file. You can open this file in your web browser to see the results of the demo (if it is not automatically shown for you).
    • You should become very familiar with the contents of this file as it provides detailed instructions on how to use the color palettes in the themefurmanu package.
  2. Now, copy the following code and put it into the file that you created above. Put this code below what you previously added.
library(tidylog)
library(skimr)
library(hexbin)
library(RColorBrewer)
library(ggthemes)
library(scales)
source("source/read_univ_data_simple.R")
source("source/save_graphs.R")

grade_palette <- div_gradient_pal(
    low = "#2166AC", 
    mid = "#f7f7f7", 
    high = "#B2182B")(seq(0, 
                          1,
                          length.out = length(grade_levels)
                          )
                     )
names(grade_palette) <- grade_levels  # map to grades
  1. Save the file.
  2. Now run all of the code in the week2-homework.R file. If you get the following error:
> source("source/read_univ_data_simple.R")
Error in file(filename, "r", encoding = encoding) : 
  cannot open the connection
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
  cannot open file 'source/read_univ_data_simple.R': No such file or directory

then, this means that you need to change the working directory in RStudio to the project directory. You can do this by doing the following:

  1. Be sure that you are currently editing the week2-homework.R file.
  2. Go to the Session menu and selecting Set Working Directory -> To Source File Location.
  3. You should now be able to run the code without any errors.
  1. If you don’t already have one, then create a folder called output in the project directory. This is where you will save the graphs that you create in this homework.

2.2 General instructions for each exercise

  1. For each exercise in Homework #1, you will start with the code that you generated for the answer to that exercise.
  2. Copy that code into the week2-homework.R file.
  3. Make the following structural change to the code. Each answer from the previous week will look something like this:
some_data_file |>
  ...some R code...

You will change it to something like this:

exer1 <- some_data_file |>
  ...some R code...

This will create a new variable called exer1 (or exer2, etc.) that contains the result of the code. You will use this variable in the next step.

  1. Now, you will edit the code so that it 1) uses the theme_furmanu() theme, 2) has an appropriate set of values in labs(), and 3) uses some color palette from that package as well.
  2. After you have done that, you will add a line of code to save the graph (to the image folder) as a PNG file suitable for either a slide deck or a report. Choose whichever one you want.
  3. Repeat the above for each of the exercises from Homework #1. You will end up with a file that contains the code for each exercise and the code to save the graph as a PNG file as well as a folder with the 10 PNG files.
  4. Copy the images into a slide deck or report.

2.3 How to do Exercise #1

Here is an example of how to do the first exercise. You will need to do the same for each of the exercises in Homework #1.

  1. Your answer to Exercise 1 from Homework #1 was something like this:
admit_data |>
  filter(!is.na(HSGPA)) |>
  ggplot(aes(HSGPA)) +
  geom_histogram(bins = 20, 
                 color = "black", 
                 linewidth = 0.3,
                 fill = "lightgrey") +
  theme_minimal() +
  scale_x_continuous(
    breaks = c(2.0, 2.5, 3.0, 3.5, 4.0)
  )
  1. Copy this code into the week2-homework.R file.
  2. Run the code in this file. The graph should appear in the Plots tab of RStudio. If not, then you have a problem.
  3. Change the code to look like this (i.e., add exer1 <- before the admit_data and add exer1 at the end of the code):
exer1 <- admit_data |>
  filter(!is.na(HSGPA)) |>
  ...
exer1
  1. To make sure that we know how to save the graph, we will add a line of code to save the graph as a PNG file. Add one of the following lines of code to the end of the other code for exer1:
save_ggplot_word(exer1, "output/exer1-word.png")
save_ggplot_slide(exer1, "output/exer1-slide.png")
  1. Verify that there is a graph in the output folder. Open it to ensure it looks as it should.
  2. Change the definition of exer1 so that it uses theme_furmanu() instead of theme_minimal().
  3. Add a labs() statement such as the following to the definition of exer1:
  ... +
  labs(
    title = "Distribution of High School GPA",
    subtitle = "For all graduates 2012-24",
    x = "High School GPA",
    y = "Number of Students",
    caption = "For Homework #2, Data Visualization, May 2025"
  ) +
  1. Now we need to add a color palette to the graph. This will take a couple steps since a histogram is kind of a special case.
  2. Add the following line of code to the definition of exer1:
  ... +
  scale_fill_furmanu(palette = "sequential3", 
                      discrete = FALSE)

We are using this palette because the x axis is continuous (not discrete), we want a sequential palette to emphasize the increasing values, and we are setting the fill. (Moreover, we’re big fans of the sequential3 palette here at Furman because it’s purple, don’t ya’ know.) If you forget your choices, you can always run show_furmanu_palettes() at the Console prompt.

  1. Now change the ggplot() and geom_histogram() lines to look like this:
  ggplot(aes(HSGPA,
             fill = after_stat(x))) +
  geom_histogram(bins = 20, 
                 color = "black", 
                 linewidth = 0.3) +

This basically is how you tell ggplot2 to assign the fill color based on the x location of the bar on the x = HSGPA axis.

  1. Complete any miscellaneous clean-up. For this graph, we want to remove the legend, so we will add the following line of code to the end of the definition of exer1:
  theme(
    legend.position = "none"
  )
  1. The final code for exer1 should look like this:
exer1 <- admit_data |>
  filter(!is.na(HSGPA)) |>
  ggplot(aes(HSGPA,
             fill = after_stat(x))) +
  geom_histogram(bins = 20, 
                 color = "black", 
                 linewidth = 0.3) +
  theme_furmanu() +
  theme(
    legend.position = "none"
  ) +
  labs(
    title = "Distribution of High School GPA",
    subtitle = "For all graduates 2012-24",
    x = "High School GPA",
    y = "Number of Students",
    caption = "For Homework #2, Data Visualization, May 2025"
  ) +
  scale_x_continuous(
    breaks = c(2.0, 2.5, 3.0, 3.5, 4.0)
  ) +
  scale_fill_furmanu(palette = "sequential3", 
                     discrete = FALSE)
exer1
save_ggplot_word(exer1, "output/exer1-word.png")
save_ggplot_slide(exer1, "output/exer1-slide.png")