Introduction

Questions

Variation

Visualizing distributions

cleaned_data %>%
    ggplot(aes(x = region)) + 
    geom_bar() +
    coord_flip() +
  labs(title = "Total Datapoints by Region")

Typical values

cleaned_data %>%
  filter(region == "Agder") %>%
  ggplot(aes(x = date, y = losses)) +
  geom_line(linewidth = 1) +
  geom_point() +
  labs(title = "Salmon Losses Over Time in Agder", x = "Date", y = "Total Losses")

Unusual values

cleaned_data %>%
    ggplot(aes(losses)) +
    geom_histogram() +
    labs(title = "Distribution of Losses")
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

cleaned_data %>%
    ggplot(aes(losses)) +
    geom_histogram() +
    coord_cartesian(ylim = c(0, 10)) +
    labs(title = "Visualizing Outliers")
## `stat_bin()` using `bins = 30`. Pick better value `binwidth`.

Missing Values

cleaned_data %>%
    mutate(losses = ifelse(losses > 100000, NA, losses)) %>%
    ggplot(aes(x = dead, y = losses)) +
    geom_point()
## Warning: Removed 739 rows containing missing values or values outside the scale range
## (`geom_point()`).

ggplot(data = cleaned_data, mapping = aes(x = dead, y = losses)) + 
  geom_point(na.rm = TRUE)

Covariation

A categorical and continuous variable

cleaned_data %>%
    ggplot(aes(x = region, y = losses)) +
    geom_boxplot()

Two categorical variables

cleaned_data %>%
    count(region, geo_group) %>%
    ggplot(aes(x = region, y = geo_group, fill = n)) +
    geom_tile()

Two continous variables

library(hexbin)
cleaned_data %>%
    ggplot(aes(x = dead, y = losses)) +
    geom_hex()

cleaned_data %>%
    filter(dead < 100000) %>%
    ggplot(aes(x = dead, y = losses)) +
    geom_boxplot(aes(group = cut_width(dead, 10000)))
## Warning: Orientation is not uniquely specified when both the x and y aesthetics are
## continuous. Picking default orientation 'x'.

Patterns and models

cleaned_data4 <- cleaned_data %>%
    filter(!is.na(dead) & !is.na(losses))

mod <- lm(log(losses + 1) ~ log(dead + 1), data = cleaned_data4)

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

cleaned_data4 %>%
    ggplot(aes(x = dead, y = resid)) +
    geom_point()