data <- read_excel("CliffordSalmonidDataApply10.xlsx")
data <- data %>%
mutate(region = factor(region))
data
## # A tibble: 2,808 × 9
## species date geo_group region losses dead discarded escaped
## <chr> <dttm> <chr> <fct> <dbl> <dbl> <dbl> <dbl>
## 1 salmon 2020-01-01 00:00:00 area 1 31425 28126 3299 0
## 2 salmon 2020-01-01 00:00:00 area 2 324116 277888 46113 0
## 3 salmon 2020-01-01 00:00:00 area 3 844829 776983 63770 0
## 4 salmon 2020-01-01 00:00:00 area 4 676852 623159 51823 0
## 5 salmon 2020-01-01 00:00:00 area 5 109269 97627 11424 0
## 6 salmon 2020-01-01 00:00:00 area 6 548921 531193 15710 0
## 7 salmon 2020-01-01 00:00:00 area 7 231487 228847 2640 0
## 8 salmon 2020-01-01 00:00:00 area 8 442659 431218 11228 0
## 9 salmon 2020-01-01 00:00:00 area 9 311127 308298 2212 0
## 10 salmon 2020-01-01 00:00:00 area 10 288849 281808 3120 0
## # ℹ 2,798 more rows
## # ℹ 1 more variable: other <dbl>
Make two bar charts here - one before ordering another after
data %>%
group_by(region) %>%
summarise(total_losses = sum(losses, na.rm = TRUE)) %>%
ggplot(aes(x = total_losses, y = region)) +
geom_col() +
labs(title = "Total Losses by Region (Unordered)",
x = "Total Losses",
y = "Region")
data %>%
group_by(region) %>%
summarise(total_losses = sum(losses, na.rm = TRUE)) %>%
ggplot(aes(x = total_losses,
y = fct_reorder(.f = region, .x = total_losses))) +
geom_col() +
labs(title = "Total Losses by Region (Ordered)",
x = "Total Losses",
y = NULL)
Show examples of three functions:
data %>%
mutate(region_rec = fct_recode(region,
"Region One" = "1",
"Region Two" = "2")) %>%
select(region, region_rec) %>%
filter(region %in% c("1", "2"))
## # A tibble: 288 × 2
## region region_rec
## <fct> <fct>
## 1 1 Region One
## 2 2 Region Two
## 3 1 Region One
## 4 2 Region Two
## 5 1 Region One
## 6 2 Region Two
## 7 1 Region One
## 8 2 Region Two
## 9 1 Region One
## 10 2 Region Two
## # ℹ 278 more rows
data %>%
mutate(region_col = fct_collapse(region,
"Zone A" = c("1", "2", "3"),
"Zone B" = c("4", "5", "6"))) %>%
select(region, region_col)
## # A tibble: 2,808 × 2
## region region_col
## <fct> <fct>
## 1 1 Zone A
## 2 2 Zone A
## 3 3 Zone A
## 4 4 Zone B
## 5 5 Zone B
## 6 6 Zone B
## 7 7 7
## 8 8 8
## 9 9 9
## 10 10 10
## # ℹ 2,798 more rows
data %>%
count(region)
## # A tibble: 23 × 2
## region n
## <fct> <int>
## 1 1 144
## 2 10 144
## 3 11 72
## 4 12 72
## 5 13 72
## 6 2 144
## 7 3 144
## 8 4 144
## 9 5 144
## 10 6 144
## # ℹ 13 more rows
data %>%
mutate(region_lump = fct_lump(region, n = 5)) %>%
distinct(region_lump)
## # A tibble: 17 × 1
## region_lump
## <fct>
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## 7 Other
## 8 9
## 9 10
## 10 Agder
## 11 Møre og Romsdal
## 12 Nordland
## 13 Norge
## 14 Rogaland
## 15 Troms
## 16 Trøndelag
## 17 Vestland
No need to do anything here.