::install_github("scottamoore/themefurmanu") devtools
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
RStudio
and open the project you created for your Week 1 Homework. - At the
Console
prompt, do the following in order to install thethemefurmanu
package on your computer.
- Create a new
R
file 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
R
code in theConsole
to load the libraries. - To verify that you have the
themefurmanu
package 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 theRStudio
editor calleddemo.qmd
. This file contains a demo of thethemefurmanu
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 theRender
button in the top center of theRStudio
editor window containing the demo file. This will create a new HTML file calleddemo.html
in the same directory as thedemo.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.
- 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")
<- div_gradient_pal(
grade_palette 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.R
file. If you get the following error:
> source("source/read_univ_data_simple.R")
in file(filename, "r", encoding = encoding) :
Error
cannot open the connection: Warning message:
In additionfile(filename, "r", encoding = encoding) :
In 'source/read_univ_data_simple.R': No such file or directory cannot open file
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:
- Be sure that you are currently editing the
week2-homework.R
file. - Go to the
Session
menu 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
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
- 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.R
file. - 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:
<- some_data_file |>
exer1 ...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
image
folder) 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.R
file. - Run the code in this file. The graph should appear in the
Plots
tab ofRStudio
. If not, then you have a problem. - Change the code to look like this (i.e., add
exer1 <-
before theadmit_data
and addexer1
at the end of the code):
<- admit_data |>
exer1 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
output
folder. Open it to ensure it looks as it should. - Change the definition of
exer1
so 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
histogram
is 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
exer1
should look like this:
<- admit_data |>
exer1 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)
exer1save_ggplot_word(exer1, "output/exer1-word.png")
save_ggplot_slide(exer1, "output/exer1-slide.png")