Visualizing distributions

ggplot(data = colony) +
  geom_bar(mapping = aes(x = months))

ggplot(data = colony) +
  geom_histogram(mapping = aes(x = colony_lost_pct))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: Removed 54 rows containing non-finite outside the scale range
## (`stat_bin()`).

Typical values

ggplot(data = colony, mapping = aes(x = colony_lost_pct)) +
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: Removed 54 rows containing non-finite outside the scale range
## (`stat_bin()`).

Unusual values

ggplot(data = colony, mapping = aes(x = colony_lost_pct)) +
  geom_histogram() +
  coord_cartesian(ylim = c(0, 40))
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.
## Warning: Removed 54 rows containing non-finite outside the scale range
## (`stat_bin()`).

Missing Values

colony %>%
  mutate(colony_lost_pct = ifelse(
    colony_lost_pct < 0 | colony_lost_pct > 40,
    NA,
    colony_lost_pct
  )) %>%
  ggplot(aes(x = colony_lost_pct)) +
  geom_histogram(na.rm = TRUE)
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

colony %>%
  mutate(colony_lost_pct = ifelse(
    colony_lost_pct < 0 | colony_lost_pct > 40,
    NA,
    colony_lost_pct
  )) %>%
  ggplot(aes(x = colony_lost_pct)) +
  geom_histogram(na.rm = TRUE)
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

Covariation

A categorical and continuous variable

ggplot(data = colony,
       mapping = aes(x = colony_lost_pct)) +
  geom_freqpoly(
    mapping = aes(colour = months),
    binwidth = 1
  )
## Warning: Removed 54 rows containing non-finite outside the scale range
## (`stat_bin()`).

### Two categorical variables

colony %>%
  count(months, year) %>%
  ggplot(aes(x = months, y = year, fill = n)) +
  geom_tile()

Two continous variables

ggplot(data = colony) +
  geom_bin2d(mapping = aes(
    x = colony_max,
    y = colony_lost
  ))
## `stat_bin2d()` using `bins = 30`. Pick better value `binwidth`.
## Warning: Removed 72 rows containing non-finite outside the scale range
## (`stat_bin2d()`).

Two continous variables

library(hexbin)

colony %>%
  ggplot(aes(
    x = colony_added,
    y = colony_lost
  )) +
  geom_hex()
## Warning: Removed 83 rows containing non-finite outside the scale range
## (`stat_binhex()`).

colony %>%
  filter(colony_added < 20) %>%
  ggplot(aes(
    x = colony_added,
    y = colony_lost
  )) +
  geom_boxplot(
    aes(group = cut_width(colony_added, 0.5))
  )

Patterns and models

ggplot(data = colony) +
  geom_boxplot(
    mapping = aes(
      x = as.factor(state),
      y = colony_lost_pct
    )
  )
## Warning: Removed 54 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

ggplot(data = colony) +
  geom_point(
    mapping = aes(
      x = year,
      y = colony_lost_pct
    )
  )
## Warning: Removed 54 rows containing missing values or values outside the scale range
## (`geom_point()`).