devtools::install_github("scottamoore/themefurmanu")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
- Start
RStudioand open the project you created for your Week 1 Homework. - At the
Consoleprompt, do the following in order to install thethemefurmanupackage on your computer.
- Create a new
Rfile in the project directory. Name itweek2-homework.R. - At the top of this file, put the following code:
library(tidyverse)
library(themefurmanu)- Save the file.
- Execute those two lines of
Rcode in theConsoleto load the libraries. - To verify that you have the
themefurmanupackage installed, run the following code in theConsole:
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.
- Now run the following in the
Console:
open_furmanu_demo()- This will open a new
qmd(Quarto Markdown) file in theRStudioeditor calleddemo.qmd. This file contains a demo of thethemefurmanupackage, 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 theRenderbutton in the top center of theRStudioeditor window containing the demo file. This will create a new HTML file calleddemo.htmlin the same directory as thedemo.qmdfile. 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
themefurmanupackage.
- 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
- 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- Save the file.
- Now run all of the code in the
week2-homework.Rfile. 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 directorythen, this means that you need to change the working directory in RStudio to the project directory. You can do this by doing the following:
- Be sure that you are currently editing the
week2-homework.Rfile. - Go to the
Sessionmenu and selectingSet Working Directory->To Source File Location. - You should now be able to run the code without any errors.
- If you don’t already have one, then create a folder called
outputin the project directory. This is where you will save the graphs that you create in this homework.
2.2 General instructions for each exercise
- For each exercise in Homework #1, you will start with the code that you generated for the answer to that exercise.
- Copy that code into the
week2-homework.Rfile. - 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.
- Now, you will edit the code so that it 1) uses the
theme_furmanu()theme, 2) has an appropriate set of values inlabs(), and 3) uses some color palette from that package as well. - After you have done that, you will add a line of code to save the graph (to the
imagefolder) as a PNG file suitable for either a slide deck or a report. Choose whichever one you want. - 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.
- 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.
- 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)
)- Copy this code into the
week2-homework.Rfile. - Run the code in this file. The graph should appear in the
Plotstab ofRStudio. If not, then you have a problem. - Change the code to look like this (i.e., add
exer1 <-before theadmit_dataand addexer1at the end of the code):
exer1 <- admit_data |>
filter(!is.na(HSGPA)) |>
...
exer1- 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")- Verify that there is a graph in the
outputfolder. Open it to ensure it looks as it should. - Change the definition of
exer1so that it usestheme_furmanu()instead oftheme_minimal(). - Add a
labs()statement such as the following to the definition ofexer1:
... +
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"
) +- Now we need to add a color palette to the graph. This will take a couple steps since a
histogramis kind of a special case. - 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.
- Now change the
ggplot()andgeom_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.
- 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"
)- The final code for
exer1should 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")