7.6 Mapping relative risks
d$RR <- res$summary.fitted.values[, "mean"]
d$LL <- res$summary.fitted.values[, "0.025quant"]
d$UL <- res$summary.fitted.values[, "0.975quant"]map_sf <- merge(
map_sf, d,
by.x = c("NAME", "year"),
by.y = c("county", "year")
)ggplot(map_sf) +
geom_sf(aes(fill = RR)) +
facet_wrap(~year, dir = "h", ncol = 7) +
ggtitle("RR") + theme_bw() +
theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
) +
scale_fill_gradient2(
midpoint = 1, low = "blue", mid = "white", high = "red"
)
Animate the plot:
library(gganimate)
ggplot(map_sf) +
geom_sf(aes(fill = RR)) +
theme_bw() +
theme(
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
) +
scale_fill_gradient2(
midpoint = 1, low = "blue", mid = "white", high = "red"
) +
transition_time(year) +
labs(title = "Year: {round(frame_time, 0)}")To save the animation:
# anim_save("oh_map.gif")