library(tidyverse)
library(broom)
library(geojsonio)
library(RColorBrewer)
library(rgdal)
library(rgeos)
Drought HEX
# Set the fonts
library(showtext)
library(sysfonts)
library(extrafont)
::showtext_auto()
showtext::showtext_opts(dpi=320)
showtextfont_add_google(name="Chelsea Market",
family="Chelsea Market")
#tuesdata <- tidytuesdayR::tt_load('2022-06-14')
<- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2022/2022-06-14/drought-fips.csv') drought_fips
Verify the avg level of drought for each state
%>%
drought_fipsgroup_by(State)%>%
summarise(drought=mean(DSCI))%>%
ungroup() %>%
ggplot(aes(x=fct_reorder(State,-drought),y=drought))+
geom_col()+
labs(x="",y="",
title="Avg level of drought for each state")+
::theme_economist_white()+
ggthemestheme(axis.text.x = element_text(size=5))
<- drought_fips%>%
drought_avggroup_by(State)%>%
summarise(drought=mean(DSCI))%>%
ungroup()
Load the map hex data and make extra features
source: https://d3-graph-gallery.com/graph/hexbinmap_geo_basic.html
# load json data
<-
map_hex geojson_read("us_states_hexgrid.geojson.json", what = "sp")
# map_hex%>%class # SpatialPolygonsDataFrame
# set the names
@data <-
map_hex@data %>%
map_hexmutate(google_name = gsub(" \\(United States\\)", "", google_name))
# make a smaller sized hex
<- gBuffer(map_hex, width = -.15, byid = T)
map_hex_buffer
# tidy to dataframe
<- tidy(map_hex_buffer, region = "iso3166_2")
map_hex_tidy
# add drought level
<-
hex_drought %>%
map_hex_tidy left_join(drought_avg, by = c("id"="State"))
# make the centroids with state names
<- cbind.data.frame(data.frame(gCentroid(map_hex_buffer, byid = T),
centr id = map_hex@data$iso3166_2))
Make the map hex with avg drought level
ggplot()+
geom_polygon(data=map_hex,
mapping=aes(long,lat,group=group),
fill="brown",
color="#f8bc05")+
geom_polygon(data=hex_drought,
mapping=aes(long,lat,group=group,fill=drought),
color="white")+
geom_text(data=centr,
aes(x=x, y=y, label=id),
color="white",size=2,fontface="bold") +
scale_fill_gradient(low = "grey", high = "brown")+
labs(title="US intense drought locations",
caption="DataSource: #TidyTuesday 2022 week24 & Drought.gov | DataViz: @FGazzelloni",
fill="Level")+
coord_map()+
::theme_map()+
ggthemestheme(text=element_text(color="white",family="Chelsea Market"),
panel.background = element_rect(fill="#162e51",color="#f8bc05",size=1),
plot.background = element_rect(fill="#0071bc",color="#f8bc05",size=1),
plot.title = element_text(size=20,color="#f8bc05"),
legend.background = element_rect(fill="#0071bc",color="#f8bc05",size=0.5),
legend.position = c(0.01,0.3))
ggsave("w24_drought.png",
dpi=320,
width = 5.9,
height = 4)