Data Visualization in R

Week 8, Part 1

Jeff Jacobs

jj1088@georgetown.edu
DSAN Summer Bootcamp 2024
Georgetown University

Why Are We Starting with Tidyverse Here?

Base R vs. Tidyverse

Code
source("../_globals.r")
lexp_df <- read.csv("assets/lexp_by_ward.csv")
plot(
  lexp_df$ward, lexp_df$life_exp,
  main="DC Life Expectancy by Ward (2015)",
  xlab="Ward", ylab="Life Expectancy",
  pch=19, cex.lab=2, cex.main=2, cex.axis=2
)

Code
library(tidyverse)
us_mean <- 78.69
lexp_df <- read_csv("assets/lexp_by_ward.csv")
lexp_df <- lexp_df |> mutate(
  above_avg = ifelse(life_exp > us_mean, "Above Mean", "Below Mean")
)
lexp_df |> ggplot(aes(x=as.factor(ward), y=life_exp, color=above_avg)) +
  geom_point(size=5) +
  geom_segment(
    aes(xend=ward, yend=us_mean),
    linewidth=1.5
  ) +
  geom_hline(
    aes(
      yintercept=us_mean,
      linetype="National Mean"
    ),
    linewidth=1
  ) +
  dsan_theme("half") +
  theme(
    plot.title = element_text(hjust = 0.5)
  ) +
  labs(
    x = "Ward",
    y = "Life Expectancy",
    title = "DC Life Expectancy by Ward (2015)"
  ) +
  scale_linetype_manual("test", values=c("dashed")) +
  remove_legend_title()

Data from DC Dept. of Health

Plus Way Wider Range of Plot Types!

(All using the same syntax)

Code
library(sf)
my_sf <- read_sf("assets/Wards_from_2022.geojson")
my_sf_merged <- my_sf |> left_join(
  lexp_df, by=c("WARD"="ward")
)
my_sf_merged |> ggplot(aes(fill=life_exp)) +
  geom_sf() +
  dsan_theme() +
  theme(
    axis.line = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank()
  ) +
  labs(
    title = "DC Life Expectancy by Ward (2015)",
    fill = "Life Exp."
  ) +
  scale_fill_viridis_c()

Figure 1: Life expectancy by ward; Shapefile from OpenData.DC.gov

Why Do Data Scientists Have To Know Visualization?

…Can’t We Just Use Fancy Statistical Methods?

Code
library(tidyverse)
library(datasets)
anscombe_df <- datasets::anscombe |>
  #mutate(id = row_number()) |>
  pivot_longer(
    everything(),
    cols_vary = "slowest",
    names_to = c(".value", "Dataset"),
    names_pattern = "(.)(.)"
  )
anscombe_plot_base <- anscombe_df |>
  ggplot(aes(x=x, y=y, color=Dataset)) +
  geom_point(size=g_pointsize / 2) +
  geom_smooth(method="lm", se=FALSE, fullrange=TRUE) +
  dsan_theme("full") +
  xlim(c(4, 20)) + ylim(c(2, 14))
anscombe_plot_base +
  facet_wrap(vars(Dataset), nrow=2, scales="free")

The Scariest Dataset of All Time

Means
Code
anscombe_df |> group_by(Dataset) |>
  summarize(
    x_mean = round(mean(x), 2),
    y_mean = round(mean(y), 2)
  )
Dataset x_mean y_mean
1 9 7.5
2 9 7.5
3 9 7.5
4 9 7.5
Figure 2: Column means for each dataset
Standard Deviations
Code
anscombe_df |> group_by(Dataset) |>
  summarize(
    x_sd = round(sd(x), 2),
    y_sd = round(sd(y), 2)
  )
Dataset x_sd y_sd
1 3.32 2.03
2 3.32 2.03
3 3.32 2.03
4 3.32 2.03
Figure 3: Standard deviations for each dataset
Correlations
Code
library(corrr)
corr_df <- anscombe_df |>
  group_by(Dataset) |>
  summarize(
    r = round(cor(x, y), 2)
  )
corr_df
Dataset r
1 0.82
2 0.82
3 0.82
4 0.82
Figure 4: Correlation between \(x\) and \(y\) for each dataset

It Gets Worse…

Code
library(broom)
lm_df <- anscombe_df |> group_by(Dataset) |>
  do(tidy(lm(y ~ x, data = .))) |>
  mutate(across(where(is.numeric), round, 2)) |>
  ungroup() |>
  mutate(
    term = gsub("Intercept","Int", term)
  )
lm_df |> filter(Dataset == 1) |> select(!Dataset)
term estimate std.error statistic p.value
(Int) 3.0 1.12 2.67 0.03
x 0.5 0.12 4.24 0.00
lm_df |> filter(Dataset == 2) |> select(!Dataset)
term estimate std.error statistic p.value
(Int) 3.0 1.13 2.67 0.03
x 0.5 0.12 4.24 0.00
lm_df |> filter(Dataset == 3) |> select(!Dataset)
term estimate std.error statistic p.value
(Int) 3.0 1.12 2.67 0.03
x 0.5 0.12 4.24 0.00
lm_df |> filter(Dataset == 4) |> select(!Dataset)
term estimate std.error statistic p.value
(Int) 3.0 1.12 2.67 0.03
x 0.5 0.12 4.24 0.00

Grammar of Graphics

Intuition

  • Sentences are made up of subjects, verbs, objects
G cluster_0 Jeff cluster_1 hugged cluster_2 Biko data Subject aes Verb data->aes geom Object aes->geom
  • Visualizations are made up of encodings, coordinate systems, aesthetic mappings
G cluster_0 my_data |> cluster_1 ggplot(aes(x=year, y=height)) |> cluster_2 geom_point() data Data aes Aesthetics data->aes geom Geometry aes->geom

Graphical Primitives

Split the visualization task into discrete steps, joined by |>!

Layer Function Explanation
Data ggplot(df) Raw data you want to visualize
Aesthetics aes(x=xcol,y=ycol) Mapping of df columns onto geometries
Geometries geom_*() Which geometric objects to use
Facets facet_*() Arrangement of plots into a grid
Theme theme(), labs() Overall visual defaults, titles, axis labels

(See the official ggplot2 book for more!)

ggplot2 In Practice

(In practice, a few central graphical primitives you’ll use over and over again)

geom_bar()
bar_data <- tribble(
  ~Category, ~Value,
  "A", 100,
  "B", 200
)
ggplot(bar_data, aes(x=Category, y=Value)) +
  geom_bar(stat="identity") +
  dsan_theme("quarter")

stat_function()
ggplot(data.frame(x=c(-4,4)), aes(x=x)) +
  stat_function(fun=dnorm, linewidth=g_linewidth) +
  dsan_theme("quarter")

Common Geometries

geom_point() for scatterplots

N_scatter <- 50
random_points <- data.frame(x=runif(N_scatter,0,1),y=runif(N_scatter,0,1))
ggplot(random_points, aes(x=x, y=y)) +
  geom_point(size = g_pointsize) +
  dsan_theme("quarter")

and geom_line() for line graphs

N_line <- 15
rand_line <- data.frame(x=seq(1,N_line),y=runif(N_line,0,10))
ggplot(rand_line, aes(x=x, y=y)) +
  geom_line(linewidth = g_linewidth) +
  dsan_theme("quarter")

Combining Geometries

…Which can be used together in a single plot:

ggplot(rand_line, aes(x=x, y=y)) +
  geom_line(linewidth=g_linewidth) +
  geom_point(size=g_pointsize/2) +
  dsan_theme("full")

Titles + Axis Labels

Can all be accomplished using the labs() function!

rand_plot <- rand_line |>
  ggplot(aes(x=x, y=y)) +
  geom_line(linewidth=g_linewidth) +
  geom_point(size=g_pointsize/2) +
  dsan_theme("quarter") +
  labs(
    title = "My Cool Plot",
    x = "The x value",
    y = "The y value"
  )
rand_plot

Categorical Variables

Use the color aesthetic with factor() to differentiate categories

(Clustered data generation code)
set.seed(2024)
centroids <- list(c(0,0),c(0,1),c(1,0),c(1,1))
cluster_df <- tibble()
N <- 50
Sigma <- matrix(c(0.05, 0, 0, 0.05), nrow=2, ncol=2, byrow=TRUE)
for (i in 1:length(centroids)) {
  cur_centroid <- centroids[[i]]
  # Generate N points from this centroid
  cur_data <- MASS::mvrnorm(N, cur_centroid, Sigma)
  colnames(cur_data) <- c("x", "y")
  cur_df <- as_tibble(cur_data)
  cur_df <- cur_df |> mutate(
    cluster = i
  )
  cluster_df <- rbind(cluster_df, cur_df)
}

Without factor()

Code
cluster_df |> ggplot(aes(x=x, y=y, color=cluster)) +
  geom_point(size=g_pointsize / 2) +
  dsan_theme("half") +
  labs(color="Cluster")

With factor() 💯

Code
cluster_df |> ggplot(aes(x=x, y=y, color=factor(cluster))) +
  geom_point(size=g_pointsize / 2) +
  dsan_theme("half") +
  labs(color="Cluster")

Facets

If individual plot gets too busy, facets provide an easy way to split data into multiple (automatically-labeled) plots!

Code
facet_plot <- cluster_df |>
  ggplot(aes(
    x=x, y=y,
    color=factor(cluster)
  )) +
  geom_point(size=g_pointsize / 2) +
  dsan_theme("half") +
  labs(color="Cluster") +
  facet_wrap(vars(cluster),nrow=2)
facet_plot

(Much more on this in DSAN 5200: Advanced Data Visualization 😉)

Visualizing Text Data

Loading Text Data

library(tidyverse)
library(tidytext)
text_url <- "https://gist.githubusercontent.com/jpowerj/493e395c2688ea72eabd0277e8e7a392/raw/6f882d95f3a1dc21c3b7e0b18b68d64b165c6367/eggs.txt"
result_df <- as_tibble(read_lines(text_url))
result_df <- result_df |> filter(value != "")
result_df |> head(5)
value
I am Sam
Sam I am
That Sam-I-am!
That Sam-I-am!
I do not like that Sam-I-am!

Tokenization

token_df <- result_df |>
  unnest_tokens(word, value)
token_df |> head()
word
i
am
sam
sam
i
am

Removing Stopwords

library(stopwords)
cleaned_df <- token_df |>
  anti_join(get_stopwords())
cleaned_df |> head()
word
sam
sam
sam
sam
like
sam

Computing Token Counts

count_df <- cleaned_df |>
  count(word, sort = TRUE)
count_df |> head()
word n
like 45
eat 24
sam 18
eggs 11
green 11
ham 10

Basic Wordcloud

set.seed(2024)
library(ggwordcloud)
count_df |>
  ggplot(aes(label = word)) +
  geom_text_wordcloud(size = 20) +
  theme_minimal()

With Color!

# The wordclouds
set.seed(2024)
library(ggwordcloud)
count_df |>
  ggplot(aes(label = word, color = n)) +
  geom_text_wordcloud(size = 20) +
  theme_minimal()

The Size Aesthetic

count_df |>
  ggplot(aes(label = word, size = n, color = n)) +
  geom_text_wordcloud() +
  scale_size_area(max_size = 50) +
  theme_minimal()