02:00
Lecture 7
Cornell University
INFO 5001 - Fall 2023
2023-09-13
What is the difference between rendering and saving a document?
.qmd
file but are not reflected in your output HTML or PDF fileWhat does it mean to commit and push something?
What makes a dataset “tidy”?
02:00
Untidy/messy is not inherently bad, but it makes certain types of analyses more challenging.
ae-05
ae-05
(repo name will be suffixed with your GitHub name).pivot_longer()
function.Reveal below for code from the previous plot.
# load required packages
library(tidyverse)
library(scales)
# use custom font for plot
library(showtext)
font_add_google("Atkinson Hyperlegible")
# set seed for reproducibility
set.seed(123)
# simulate draws 100,000 times
draw_order_wide <- map(1:1e05, \(n) sample.int(n = 4)) |>
enframe(
name = ".id",
value = "person"
) |>
unnest_wider(col = person, names_sep = "_")
# how often each person picks in each spot
draw_order_wide |>
# pivot to long form for plotting
pivot_longer(
cols = starts_with("person"),
names_to = "person",
values_to = "order"
) |>
mutate(
# replace generic values with individual names
person = case_match(
.x = person,
"person_1" ~ "Jacob",
"person_2" ~ "Beverly",
"person_3" ~ "Rosemarie",
"person_4" ~ "Mama"
),
person = fct_relevel(.f = person, "Mama", "Jacob", "Beverly", "Rosemarie"),
# convert to factor for plotting purposes
order = factor(x = order,
levels = 1:4,
labels = c(
"First", "Second",
"Third", "Fourth"
))
) |>
# draw a relative frequency bar chart
ggplot(mapping = aes(x = person, fill = order)) +
geom_bar(position = "fill") +
# clean up scales and labels
scale_y_continuous(labels = label_percent()) +
scale_fill_viridis_d(end = 0.8) +
labs(
title = "Daddy draw for order is fair",
subtitle = "On average every person draws the same spots with the same frequency",
x = NULL,
y = "Percent of morning workday departures",
fill = "Good bye\norder",
caption = "Source: probability and simulations"
) +
# increase font size and use accessible font
theme_minimal(
base_size = 13,
base_family = "Atkinson Hyperlegible"
) +
# move title and subtitle to left side of plot window
theme(
plot.title.position = "plot"
)