Import Data

babynames <- read_excel("../00_data/mydata2.xlsx")

Introduction

Questions

Variation

ggplot(data = babynames) +
    geom_bar(mapping = aes(x = Name))

Visualizing distributions

# Top 15 male names
top15_male <- babynames %>%
  filter(sex == "M") %>%
  group_by(Name) %>%
  summarize(Proportion = sum(Proportion)) %>%
  arrange(desc(Proportion)) %>%
  slice_head(n = 15)

# Top 15 female names
top15_female <- babynames %>%
  filter(sex == "F") %>%
  group_by(Name) %>%
  summarize(Proportion = sum(Proportion)) %>%
  arrange(desc(Proportion)) %>%
  slice_head(n = 15)
ggplot(top15_male,
       aes(x = reorder(Name, Proportion),
           y = Proportion)) +
  geom_col(fill = "steelblue") +
  coord_flip() +
  labs(
    title = "Top 15 Male Names",
    x = "Name",
    y = "Proportion"
  ) +
  theme_minimal()

ggplot(top15_female,
       aes(x = reorder(Name, Proportion),
           y = Proportion)) +
  geom_col(fill = "pink") +
  coord_flip() +
  labs(
    title = "Top 15 Female Names",
    x = "Name",
    y = "Proportion"
  ) +
  theme_minimal()

Typical values

ggplot(babynames, aes(x = Count)) +
  geom_histogram(fill = "steelblue", bins = 30) +
  theme_minimal()

Unusual values

ggplot(babynames) +
  geom_histogram(aes(x = Count), binwidth = 500) +
  coord_cartesian(ylim = c(0, 100))

unusual_count <- babynames %>%
  filter(Count > 50000) %>%
  select(Year, sex, Name, Count) %>%
  arrange(desc(Count))

unusual_count
## # A tibble: 364 × 4
##     Year sex   Name    Count
##    <dbl> <chr> <chr>   <dbl>
##  1  1947 F     Linda   99686
##  2  1948 F     Linda   96209
##  3  1947 M     James   94756
##  4  1957 M     Michael 92695
##  5  1947 M     Robert  91642
##  6  1949 F     Linda   91016
##  7  1956 M     Michael 90620
##  8  1958 M     Michael 90520
##  9  1948 M     James   88588
## 10  1954 M     Michael 88514
## # ℹ 354 more rows
unusual_prop <- babynames %>%
  filter(Proportion > 0.05) %>%
  select(Year, sex, Name, Proportion) %>%
  arrange(desc(Proportion))

unusual_prop
## # A tibble: 168 × 4
##     Year sex   Name    Proportion
##    <dbl> <chr> <chr>        <dbl>
##  1  1880 M     John        0.0815
##  2  1881 M     John        0.0810
##  3  1880 M     William     0.0805
##  4  1883 M     John        0.0791
##  5  1881 M     William     0.0787
##  6  1882 M     John        0.0783
##  7  1884 M     John        0.0765
##  8  1882 M     William     0.0762
##  9  1886 M     John        0.0758
## 10  1885 M     John        0.0755
## # ℹ 158 more rows

Missing Values

babynames %>%

  # filter(Count > 50000) %>%

  mutate(Count = ifelse(Count > 50000, NA, Count)) %>%

  # Plot
  ggplot(aes(x = Count)) +
  geom_histogram(binwidth = 500)
## Warning: Removed 364 rows containing non-finite outside the scale range
## (`stat_bin()`).

Covariation

A categorical and continuous variable

babynames %>%
  ggplot(aes(x = sex, y = Count)) +
  geom_boxplot()

Two categorical variables

babynames %>%

  mutate(year_group = cut(
    Year,
    breaks = c(1880, 1920, 1960, 2000, 2020),
    labels = c("1880-1920", "1921-1960", "1961-2000", "2001-2020")
  )) %>%

  count(sex, year_group) %>%

  ggplot(aes(x = sex, y = year_group, fill = n)) +
  geom_tile()

Two continuous variables

library(hexbin)

babynames %>%
  ggplot(aes(x = Count, y = Proportion)) +
  geom_hex()

Patterns and models

library(modelr)

mod <- lm(log(Proportion) ~ log(Count), data = babynames)

babynames4 <- babynames %>%
  modelr::add_residuals(mod) %>%
  mutate(resid = exp(resid))

babynames4 %>%
  ggplot(aes(Count, resid)) +
  geom_point(alpha = 0.1) +
    geom_boxplot()
## Warning: Orientation is not uniquely specified when both the x and y aesthetics are
## continuous. Picking default orientation 'x'.
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?