Stat451_Final

Author

Karl Castillo

B (i)

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.3
Warning: package 'purrr' was built under R version 4.3.3
Warning: package 'lubridate' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.2.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.0.4     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
ti <- tibble(
  country = c("United Kingdom", "Germany", "Ireland"),
  ancestry = c(54000000, 45000000, 34000000)
)

ti |>
  ggplot(aes(x = country, y = ancestry, fill = country)) +
  geom_col() +
  scale_fill_manual(values = c(
    "United Kingdom" = "red",
    "Germany"        = "blue",
    "Ireland"        = "green"
  )) +
  labs(
    title = "US Americans w/ European Ancestry (Top 3 Countries)",
    x = "Country",
    y = "Number of US Americans",
    fill = "Country"
  ) +
  scale_y_continuous(labels = scales::comma)

B(ii)

library(tidyverse)

poll <- tibble(
  candidate = c("Biden", "Trump"),
  percent = c(46, 47)
)

poll |>
  ggplot(aes(x = candidate, y = percent, fill = candidate)) +
  geom_col() +
  scale_fill_manual(values = c("Biden" = "blue", "Trump" = "red")) +
  labs(
    title = "Poll Results in Pennsylvania",
    x = "Candidate",
    y = "Percentange"
  ) +
  ylim(0, 50)

B(iii)

library(tidyverse)

ti <- tibble(
  age_group = c("below 23", "23–25", "25–27", "27–29", "29–31", "over 31"),
  max_age   = c(22.7,       24.7,     26.9,     28.9,     30.8,     33.4)
)

ti |>
  ggplot(aes(x = age_group, y = max_age)) +
  geom_col(fill = "forestgreen") +
  labs(
    title = "Maximum Average Age in Each Age Group",
    x = "Age group (years)",
    y = "Maximum average age"
  )

B(v)

library(tidyverse)

ti <- tibble(
  country = c("United States", "United Kingdom"),
  serial_killers = c(3615, 180),
  population = c(333000000, 67000000)
) |>
  mutate(proportion = serial_killers / population)

ti |>
  ggplot(aes(x = country, y = proportion, fill = country)) +
  geom_col() +
  scale_fill_manual(values = c("United States" = "red", "United Kingdom" = "blue")) +
  labs(
    title = "Proportion of Serial Killers per Country",
    x = "Country",
    y = "Proportion (Serial Killers per Person)"
  ) +
  scale_y_continuous(labels = scales::comma)