Read the file

path <- "/Users/yuhe/Downloads/gdswr_data/Chapter5"
okcounty <- st_read(paste0(path,"/ok_counties.shp"), quiet = TRUE)
tpoint <- st_read(paste0(path,"/ok_tornado_point.shp"), quiet = TRUE)
tpath <- st_read(paste0(path,"/ok_tornado_path.shp"), quiet = TRUE)

Exercise 1

Try to fill different counties with different colors for okcounty data.

ggplot(data = okcounty) +
  geom_sf(fill = NA) +
  geom_sf(aes(fill = GEOID)) +
  theme(legend.position = "none")

Exercise 2

Plot tornado from different years in different colors in the county map.

tpoint_16_21 <- tpoint %>%
  filter(yr >= 2016 & yr <= 2021) %>%
  select(om, yr, date)
tpath_16_21 <- tpath %>%
  filter(yr >= 2016 & yr <= 2021) %>%
  select(om, yr, date)

ggplot() +
  geom_sf(data = okcounty, fill = NA) +
  geom_sf(data = tpoint_16_21, aes(color = as.factor(yr))) +
  theme_bw()

Exercise 3

Plot the okcounty map with each county color-filled by the density of tornadoes between 2000 and 2021.

tpoint_00_21 <- tpoint %>%
  filter(yr >= 2000 & yr <= 2021) %>%
  select(om, yr, date, inj)

countypnt <- st_join(tpoint_00_21, okcounty)

countypnt <- st_drop_geometry(countypnt)
countysum <- countypnt %>%
  group_by(GEOID) %>%
  summarize(tcnt = n())

countymap <- okcounty %>%
  left_join(countysum, by = "GEOID") %>%
  replace(is.na(.), 0) %>%
  mutate(area = st_area(okcounty),
         tdens = 10^6 * 10^3 * tcnt / area) %>%
  drop_units()

ggplot(data = countymap) +
  geom_sf(aes(fill = tdens)) +
  scale_fill_distiller(name = expression("Tornadoes/1000 km"^2), 
                       palette = "YlOrRd", 
                       breaks = pretty_breaks(),
                       direction = 1) +
  theme_void() +
  theme(legend.position = "bottom")

Exercise 4

Choose a proper palette map to visualize the average injuries per year by tornadoes in each county between 2000 and 2021.

countysum2 <- countypnt %>%
  group_by(GEOID) %>%
  summarize(injuries = mean(inj))

countymap2 <- okcounty %>%
  left_join(countysum2, by = "GEOID")

ggplot(data = countymap2) +
  geom_sf(aes(fill = injuries)) +
  scale_fill_distiller(name = expression("Average injuries per Year"), 
                       palette = "YlOrBr", 
                       breaks = pretty_breaks(),
                       direction = 1) +
  theme_void() +
  theme(legend.position = "bottom")

Exercise 5

Reproduce the map in the previous page with breaking density with 6 groups - 0 to 2, 2 to 4, 4 to 6, 6 to 8, 8 to 10 and above 10.

countymap2 <- countymap2 %>%
  mutate(injuries_c = cut(injuries,
                        breaks = c(0,2,4,6,8,10,Inf),
                        include.lowest = T))

ggplot(data = countymap2) +
  geom_sf(aes(fill = injuries)) +
  scale_fill_distiller(name = expression("Average injuries per Year"), 
                       palette = "YlOrBr", 
                       breaks = pretty_breaks(),
                       direction = 1) +
  theme_void() +
  theme(legend.position = "bottom")

Exercise 6

Generate a map of tornado paths where the paths from each year (2016-2021) are displayed as a different color, Create a composite figure containing the map of tornado paths and the map of tornado points using plot_grid().

p1 <- ggplot() +
  geom_sf(data = okcounty, fill = NA) +
  geom_sf(data = tpath_16_21, aes(color = as.factor(yr)))

p2 <- ggplot() +
  geom_sf(data = okcounty, fill = NA) +
  geom_sf(data = tpoint_16_21, aes(color = as.factor(yr)))

plot_grid(p1, p2, labels = c("Tornado Path by Year", "Tornado Touchdown by Year"))