library(tidyverse)
library(lubridate)
library(geofacet)
library(ggsci)
library(gganimate)
theme_set(theme_bw(18) +
theme(legend.position = "bottom"))
download.file('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2021/2021-07-20/drought.csv', destfile = "drought.csv")
drought <- readr::read_csv("drought.csv") %>%
dplyr::mutate(
year = valid_start %>% year(),
map_date = map_date %>% as.character() %>% lubridate::ymd(),
season = quarter(valid_start, with_year = FALSE, fiscal_start = 3),
season = case_when(
season == 1 ~ "Spring",
season == 2 ~ "Summer",
season == 3 ~ "Autumn",
season == 4 ~ "Winter"),
month = valid_start %>% lubridate::month(label = TRUE),
drought_lvl = drought_lvl %>%
fct_relevel(c("None", paste0("D", 0:4)))
)
subdata = drought %>%
group_by(year, month, state_abb, drought_lvl) %>%
dplyr::summarise(
avg_area_pct = mean(area_pct, na.rm = TRUE)) %>%
dplyr::mutate(
year_month_day = paste0(year, "-", month, "-01") %>% ymd()) %>%
ungroup()
p = subdata %>%
dplyr::filter(drought_lvl == "D3") %>%
ggplot(aes(x = year_month_day,
y = avg_area_pct,
colour = drought_lvl)) +
geom_line() +
scale_x_date(date_breaks = "2 years", date_labels = "%Y") +
scale_colour_d3() +
facet_geo(~state_abb, grid = "us_state_grid2") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
labs(x = "",
y = "Percent of area impacted")
ggsave(plot = p, filename = "plot.pdf", height = 20, width = 30)
library(plotly)
subdata2 = subdata %>%
dplyr::mutate(time = paste0(year, "-", month) %>%
fct_reorder(year_month_day))
# write_rds(x = subdata2, file = "subset_drought_data.rds")
fig <- plot_ly(
data = subdata2 %>%
dplyr::filter(drought_lvl == "D3"),
type = 'choropleth',
locationmode = 'USA-states',
locations = ~state_abb,
z = ~avg_area_pct,
frame = ~time,
color = ~avg_area_pct,
zauto = FALSE,
zmin = 0,
zmax = 100) %>%
layout(
title = "Severe droughts in the US",
geo = list(
scope = 'usa'),
showlegend = FALSE) %>%
colorbar(
title = "Avg. % of area impacted"
) %>%
animation_opts(frame = 20)
fig
sessioninfo::session_info()
## - Session info ---------------------------------------------------------------
## setting value
## version R version 4.1.0 (2021-05-18)
## os Windows 10 x64
## system x86_64, mingw32
## ui RTerm
## language (EN)
## collate English_Australia.1252
## ctype English_Australia.1252
## tz Australia/Sydney
## date 2021-07-25
##
## - Packages -------------------------------------------------------------------
## package * version date lib source
## assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
## backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.0)
## broom 0.7.8 2021-06-24 [1] CRAN (R 4.1.0)
## bslib 0.2.5.1 2021-05-18 [1] CRAN (R 4.1.0)
## cellranger 1.1.0 2016-07-27 [1] CRAN (R 4.1.0)
## class 7.3-19 2021-05-03 [2] CRAN (R 4.1.0)
## classInt 0.4-3 2020-04-07 [1] CRAN (R 4.1.0)
## cli 3.0.0 2021-06-30 [1] CRAN (R 4.1.0)
## colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)
## crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0)
## crosstalk 1.1.1 2021-01-12 [1] CRAN (R 4.1.0)
## data.table 1.14.0 2021-02-21 [1] CRAN (R 4.1.0)
## DBI 1.1.1 2021-01-15 [1] CRAN (R 4.1.0)
## dbplyr 2.1.1 2021-04-06 [1] CRAN (R 4.1.0)
## digest 0.6.27 2020-10-24 [1] CRAN (R 4.1.0)
## dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
## e1071 1.7-7 2021-05-23 [1] CRAN (R 4.1.0)
## ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
## evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0)
## fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0)
## farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0)
## forcats * 0.5.1 2021-01-27 [1] CRAN (R 4.1.0)
## fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0)
## generics 0.1.0 2020-10-31 [1] CRAN (R 4.1.0)
## geofacet * 0.2.0 2021-07-21 [1] Github (hafen/geofacet@37be2b8)
## geogrid 0.1.1 2018-12-11 [1] CRAN (R 4.1.0)
## gganimate * 1.0.7 2020-10-15 [1] CRAN (R 4.1.0)
## ggplot2 * 3.3.5 2021-06-25 [1] CRAN (R 4.1.0)
## ggrepel 0.9.1 2021-01-15 [1] CRAN (R 4.1.0)
## ggsci * 2.9 2018-05-14 [1] CRAN (R 4.1.0)
## gifski 1.4.3-1 2021-05-02 [1] CRAN (R 4.1.0)
## glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0)
## gridExtra 2.3 2017-09-09 [1] CRAN (R 4.1.0)
## gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0)
## haven 2.4.1 2021-04-23 [1] CRAN (R 4.1.0)
## hms 1.1.0 2021-05-17 [1] CRAN (R 4.1.0)
## htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0)
## htmlwidgets 1.5.3 2020-12-10 [1] CRAN (R 4.1.0)
## httr 1.4.2 2020-07-20 [1] CRAN (R 4.1.0)
## imguR 1.0.3 2016-03-29 [1] CRAN (R 4.1.0)
## jpeg 0.1-8.1 2019-10-24 [1] CRAN (R 4.1.0)
## jquerylib 0.1.4 2021-04-26 [1] CRAN (R 4.1.0)
## jsonlite 1.7.2 2020-12-09 [1] CRAN (R 4.1.0)
## KernSmooth 2.23-20 2021-05-03 [2] CRAN (R 4.1.0)
## knitr 1.33 2021-04-24 [1] CRAN (R 4.1.0)
## labeling 0.4.2 2020-10-20 [1] CRAN (R 4.1.0)
## lattice 0.20-44 2021-05-02 [2] CRAN (R 4.1.0)
## lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.1.0)
## lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.1.0)
## lubridate * 1.7.10 2021-02-26 [1] CRAN (R 4.1.0)
## magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)
## modelr 0.1.8 2020-05-19 [1] CRAN (R 4.1.0)
## munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0)
## pillar 1.6.1 2021-05-16 [1] CRAN (R 4.1.0)
## pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0)
## plotly * 4.9.4.1 2021-06-18 [1] CRAN (R 4.1.0)
## png 0.1-7 2013-12-03 [1] CRAN (R 4.1.0)
## prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.1.0)
## progress 1.2.2 2019-05-16 [1] CRAN (R 4.1.0)
## proxy 0.4-26 2021-06-07 [1] CRAN (R 4.1.0)
## purrr * 0.3.4 2020-04-17 [1] CRAN (R 4.1.0)
## R6 2.5.0 2020-10-28 [1] CRAN (R 4.1.0)
## Rcpp 1.0.7 2021-07-07 [1] CRAN (R 4.1.0)
## readr * 1.4.0 2020-10-05 [1] CRAN (R 4.1.0)
## readxl 1.3.1 2019-03-13 [1] CRAN (R 4.1.0)
## reprex 2.0.0 2021-04-02 [1] CRAN (R 4.1.0)
## rgeos 0.5-5 2020-09-07 [1] CRAN (R 4.1.0)
## rlang 0.4.11 2021-04-30 [1] CRAN (R 4.1.0)
## rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.1.0)
## rnaturalearth 0.1.0 2017-03-21 [1] CRAN (R 4.1.0)
## rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0)
## rvest 1.0.0 2021-03-09 [1] CRAN (R 4.1.0)
## sass 0.4.0 2021-05-12 [1] CRAN (R 4.1.0)
## scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0)
## sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0)
## sf 1.0-1 2021-06-29 [1] CRAN (R 4.1.0)
## sp 1.4-5 2021-01-10 [1] CRAN (R 4.1.0)
## stringi 1.6.2 2021-05-17 [1] CRAN (R 4.1.0)
## stringr * 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
## tibble * 3.1.2 2021-05-16 [1] CRAN (R 4.1.0)
## tidyr * 1.1.3 2021-03-03 [1] CRAN (R 4.1.0)
## tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0)
## tidyverse * 1.3.1 2021-04-15 [1] CRAN (R 4.1.0)
## tweenr 1.0.2 2021-03-23 [1] CRAN (R 4.1.0)
## units 0.7-2 2021-06-08 [1] CRAN (R 4.1.0)
## utf8 1.2.1 2021-03-12 [1] CRAN (R 4.1.0)
## vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0)
## viridisLite 0.4.0 2021-04-13 [1] CRAN (R 4.1.0)
## withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0)
## xfun 0.24 2021-06-15 [1] CRAN (R 4.1.0)
## xml2 1.3.2 2020-04-23 [1] CRAN (R 4.1.0)
## yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)
##
## [1] C:/Users/Kevin/Documents/R/win-library/4.1
## [2] C:/Program Files/R/R-4.1.0/library