Week 4

Data Wrangling and Visualization

Author

Prof. Jack Reilly

Published

Fall 2026

Problem Set 4 Solutions

Download answers to Problem Set 4 Here

dwv_ps4_solutions.R
#Title: DWV Assignment 4 Solutions.R
#Author: Jack Reilly
#Purpose: Solve Assignment 4
#Requires: 
#Output: Plots to plot window; nothing to disk

#Assignment 4
library(tidyverse)
setwd("~/desktop/working/dwv/")

#Part I. Lines

#Draw a y=4x+3 on a plot that ranges from -20 to +20 on both x and y.
#Label the line appropriately next to itself.
#Color the line in an interesting color! And make it dashed.
plot(x=0, 
     type='n', 
     xlim=c(-20,20), 
     ylim=c(-20,20),
     xlab="This is the X axis", 
     ylab="This is the Y axis", 
     main="My Line")

abline(h=0, v=0, col = "gray")

abline(a=3, 
       b=4, 
       col = "forestgreen", 
       lwd=3, 
       lty = "dashed")

text(5,12, "y=4x+3", col = "forestgreen")

#Bonus: Use a combination of the above options (plus others, if you feel like looking them up) to create the wildest looking series of lines on a graph that you can. (Remember, you can overplot lines!)

#Here's some lines
plot(x=0, type='n', xlim=c(-10,10), ylim=c(-10,10), xlab="x", ylab="y", main="Drawing Some Lines")
abline(h=0, v=0, col = "gray")

abline(a=0, b=1, col = "purple", lwd=110)
abline(a=0, b=-1, col = "purple", lwd=110)

abline(a=0, b=1, col = "blue", lwd=100)
abline(a=0, b=-1, col = "blue", lwd=100)

abline(a=0, b=1, col = "green", lwd=90)
abline(a=0, b=-1, col = "green", lwd=90)

abline(a=0, b=1, col = "yellow", lwd=80)
abline(a=0, b=-1, col = "yellow", lwd=80)

abline(a=0, b=1, col = "orange", lwd=70)
abline(a=0, b=-1, col = "orange", lwd=70)

abline(a=0, b=1, col = "red", lwd=60)
abline(a=0, b=-1, col = "red", lwd=60)

abline(a=0, b=1, col = "white", lwd=50)
abline(a=0, b=-1, col = "white", lwd=50)

abline(a=0, b=1, col = "orange", lwd=50)
abline(a=0, b=-1, col = "orange", lwd=50)

abline(a=0, b=1, col = "yellow", lwd=40)
abline(a=0, b=-1, col = "yellow", lwd=40)

abline(a=0, b=1, col = "green", lwd=30)
abline(a=0, b=-1, col = "green", lwd=30)

abline(a=0, b=1, col = "blue", lwd=20)
abline(a=0, b=-1, col = "blue", lwd=20)

abline(a=0, b=1, col = "purple", lwd=10)
abline(a=0, b=-1, col = "purple", lwd=10)

abline(a=0, b=1, col = "black", lwd=5, lty="dotted")
abline(a=0, b=-1, col = "black", lwd=5, lty="dotted")

#Part 2. Protected Lands

lands<-read_csv("datasets/protected_lands.csv")

mean(lands$protected_lands)
mean(lands$gdp_percap)
mean(lands$tot_gdp_bilions)

sd(lands$protected_lands)
sd(lands$gdp_percap)
sd(lands$tot_gdp_bilions)

range(lands$protected_lands)
range(lands$gdp_percap)
range(lands$tot_gdp_bilions)

plot(x=0, type='n', xlim=c(0,12000), ylim=c(0,50), xlab="Total GDP", ylab="Protected Lands", main="Protected Lands by Country GDP")
points(y=lands$protected_lands, x=lands$tot_gdp_bilions, pch=20)

model_tot<-lm(lands$protected_lands~lands$tot_gdp_bilions)

abline(model_tot, col="red", lwd=2)

plot(x=0, type='n', xlim=c(3000,60000), ylim=c(0,50), xlab="GDP Per Capita", ylab="Protected Lands", main="Protected Lands by Country GDP Per Capita")
points(y=lands$protected_lands, x=lands$gdp_percap, pch=20)

model_pc<-lm(lands$protected_lands~lands$gdp_percap)

abline(model_pc, col="red", lwd=2)




Download an example quarto notebook for the solutions here — the .qmd, its rendered .html and .docx, and the two support folders the .html needs to display its figures. Unzip the whole thing and keep the folders next to the .html.

Week 4 Workshop

Download the lecture 4 recoding code here

dwv_w4_lecture.R
#Title: DWV Lecture 4 Material
#Author: Jack Reilly
#Purpose: Contains code for lecture 4 slides
#Requires: none
#Output: nothing to disk

#Start with our data
library(tidyverse)

pid<-c(1, 6, 3, 7, 8, 9, 2, 2, 3, 4, 4, 7, 1, 5)
data<-tibble(pid)

#Start with NAs
data$pid_recode <- NA

#Recode PID (see slides)
data$pid_recode[data$pid < 3] <- -1
data$pid_recode[data$pid > 5 & data$pid < 8] <- 1
data$pid_recode[data$pid > 2 & data$pid < 6] <- 0

#Check our work
table(data$pid, data$pid_recode)

#Or in tidyverse: do it all together!
data <- data |>
  mutate(pid_recode2 = case_when(
    pid %in% 1:2 ~ -1,
    pid %in% 3:5 ~ 0,
    pid %in% 6:7 ~ 1,
    pid == 8 | pid == 9     ~ NA
  ))

table(data$pid, data$pid_recode2)

data |> count(pid, pid_recode2)

Download the line graphing comparison here — the same line graph drawn three ways: base R, lattice, and ggplot2.

dwv_w4_linegraphs.R
#Line graphing

# Your data
years  <- c(2000, 2002, 2004, 2006, 2008, 2010)
values <- c(10, 9, 11, 14, 15, 20)

df <- data.frame(years, values)

# Base R
#We can just plot it
plot(years, values, type = "o", col = "blue",
     xlab = "Year", ylab = "Value",
     main = "Line Graph (Base R)")

#Alternately, we can build our plot in layers
# Step 1: create empty plot
plot(x = years, y = values,
     type = "n",               # "n" means no points or lines yet
     xlim = c(2000, 2010),     # set axis range
     ylim = c(0, 25),          # set y range
     xlab = "Year",
     ylab = "Value",
     main = "Base R Layered Plot")

# Step 2: add line
lines(years, values, col = "blue", lwd = 2)

# Step 3: add points
points(years, values, pch = 19, col = "red", cex = 1.5)

# Step 4: add text labels, if desired
text(years, values, labels = values, pos = 3, cex = 0.8)

#Lattice
#Lattice is a more opinionated library, based upon Cleveland (1993)
#Constrains some choices about visuals, and is less flexible than the other two
#But is more likely to result in nice default graphs

library(lattice)

#Standard usage in lattice:
# graph_type(formula, data =)
# Some types: 
# barchart, bwplot, cloud, contourplot  , densityplot, dotplot  
# histogram, levelplot, parallel, splom, stripplot, xyplot, wireframe

#Remember:
?lattice

xyplot(values ~ years, data = df,
       type = "o",
       xlab = "Year", ylab = "Value",
       main = "Line Graph (Lattice)")

# GGplot2 - the main tidyverse package
#(Yes, there was a ggplot, but it was a student project)

#GGplot is also a more opinionated sort of package
# This time, based around Leland Wilkinson's "Grammar of Graphics", coded by Wickham
# Grammar of graphics: all layered graphics can be described with seven things
# two most relevant: data, and then a system of mapping that data to geometry

#So first, we identify the data, and then, we identify the geometries:

library(ggplot2)

ggplot(df, aes(x = years, y = values)) +
  geom_line(color = "blue", linewidth = 1) +
  geom_point(color = "red", size = 3) +
  labs(title = "Line Graph (ggplot2)",
       x = "Year", y = "Value") +
  theme_minimal()