Introduction

Questions

Variation

Visualizing distributions

1. Which generation added more Pokemon with its release?

ggplot(pokemon) +
    geom_histogram(aes(x = generation_id))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: Removed 147 rows containing non-finite outside the scale range
## (`stat_bin()`).

Typical values

Unusual values

3. What number of Pokemon have a certain Total Attack Stat?

ggplot(pokemon) + 
  geom_histogram(mapping = aes(x = attack), binwidth = 0.5)

# Missing Values

# Filter for missing secondary types and count them
pokemon %>%
  filter(is.na(type_2)) %>%
  count(type_1)
## # A tibble: 0 × 2
## # ℹ 2 variables: type_1 <chr>, n <int>

A Categorical and Continuous

# Boxplot ordered by median
ggplot(data = pokemon) +
  geom_boxplot(mapping = aes(x = reorder(type_1, attack, FUN = median, na.rm = TRUE), y = attack)) +
  coord_flip()

Two Categorical Variables:

ggplot(data = pokemon) +
  geom_count(mapping = aes(x = type_1, y = egg_group_1))

Two Continuous Variables:

ggplot(data = pokemon) +
  geom_point(aes(x = defense, y = attack), alpha = 1/5)

Patterns and Models

# Fit a model to isolate the attack/defense relationship and plot the residuals
mod <- lm(attack ~ defense, data = pokemon)

pokemon %>%
  add_residuals(mod) %>%
  ggplot() +
  geom_point(mapping = aes(x = defense, y = resid))