Import data
data <- read_excel("../00_data/myData.xlsx")
data
## # A tibble: 21,560 × 12
## year gnis_id geometry metad…¹ numbe…² parkn…³ region state unit_…⁴ unit_…⁵
## <dbl> <dbl> <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr>
## 1 1904 1163670 POLYGON NA 1 Crater… PW OR CRLA Crater…
## 2 1941 1531834 MULTIPOLY… NA 1 Lake R… PW WA LARO Lake R…
## 3 1961 2055170 MULTIPOLY… NA 1 Lewis … PW WA LEWI Lewis …
## 4 1935 1530459 MULTIPOLY… NA 1 Olympic PW WA OLYM Olympi…
## 5 1982 277263 POLYGON NA 1 Santa … PW CA SAMO Santa …
## 6 1919 578853 MULTIPOLY… NA 1 NA NE ME ACAD Acadia…
## 7 1969 1329499 MULTIPOLY… NA 1 NA IM TX AMIS Amista…
## 8 1967 589056 POLYGON NA 1 NA NE MD ASIS Assate…
## 9 1944 1377082 POLYGON NA 1 NA IM TX BIBE Big Be…
## 10 1989 302659 POLYGON NA 1 NA SE FL BICY Big Cy…
## # … with 21,550 more rows, 2 more variables: unit_type <chr>, visitors <dbl>,
## # and abbreviated variable names ¹metadata, ²number_of_records, ³parkname,
## # ⁴unit_code, ⁵unit_name
Introduction
Questions
Variation
Visualizing distributions
data %>%
ggplot(aes(x = region)) +
geom_bar()

data %>%
ggplot(mapping = aes(x = year)) +
geom_histogram(binwidth = 10)
## Warning: Removed 386 rows containing non-finite values (stat_bin).

data %>%
filter(year > 1920) %>%
ggplot(aes(x = year)) +
geom_histogram(binwidth = 10)

data %>%
ggplot(aes (x = year)) +
geom_freqpoly()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 386 rows containing non-finite values (stat_bin).

Typical values
data %>%
# Filter out early years
filter(year > 1920) %>%
# Plot
ggplot(aes(x = year)) +
geom_histogram(binwidth = 10)

Unusual values
Missing Values
Covariation
A categorical and continuous variable
data %>%
ggplot(aes(x = year, y = number_of_records)) +
geom_boxplot()
## Warning: Removed 386 rows containing non-finite values (stat_boxplot).

Two categorical variables
data %>%
count(region, unit_type) %>%
ggplot(aes(x = region, y = unit_type, fill = n)) +
geom_tile()

Two continous variables
library(hexbin)
data %>%
ggplot(aes(x = region, y = year)) +
geom_hex()
## Warning: Removed 386 rows containing non-finite values (stat_binhex).

data %>%
filter(year > 1920) %>%
ggplot(aes(x = year, y = region)) +
geom_boxplot(aes(group = cut_width(year, 5)))

Patterns and models
library(modelr)
mod <- lm(log(gnis_id) ~ log(year), data = data)
data2 <- data %>%
modelr::add_residuals(mod) %>%
mutate(resid = exp(resid))
data2 %>%
ggplot(aes(gnis_id, resid)) +
geom_point()
## Warning: Removed 387 rows containing missing values (geom_point).

data2 %>%
ggplot(aes(year, resid)) +
geom_boxplot()
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?
## Warning: Removed 386 rows containing missing values (stat_boxplot).
## Warning: Removed 1 rows containing non-finite values (stat_boxplot).
