hafen / geofacet

R package for geographical faceting with ggplot2
https://hafen.github.io/geofacet/
Other
336 stars 43 forks source link

New Grid: Counties of Nevada, United States #271

Closed schmidtDETR closed 3 years ago

schmidtDETR commented 3 years ago

This grid defines a layout for counties in the State of Nevada, USA. Codes are state FIPS codes, names are county names. A blank space is used in cell 3,3 to attempt to maintain the overall shape of the state when grids are squares.

Example code, using data from FRED:


#### load packages
library(tigris, data.table, ggplot2, geofacet, tidyquant, tidyverse)

#### Set names 
nv_counties <- filter(fips_codes, state == "NV") %>%
  mutate(fred = case_when(
    county_code == "001" ~ "NVCHUR1URN", # Churchill County
    county_code == "003" ~"NVCLAR3URN", # Clark County
    county_code == "005" ~"NVDOUG5URN", # Douglas County
    county_code == "007" ~"NVELKO7URN", # Elko County
    county_code == "009" ~"NVESME9URN", # Esmeralda County
    county_code == "011" ~"NVEURE1URN", # Eureka County
    county_code == "013" ~"NVHUMB3URN", # Humboldt County
    county_code == "015" ~"NVLAND5URN", # Lander County
    county_code == "017" ~"NVLINC7URN", # Lincoln County
    county_code == "019" ~"NVLYON9URN", # Lyon County
    county_code == "021" ~"NVMINE1URN", # Mineral County
    county_code == "023" ~"NVNYEC3URN", # Nye County
    county_code == "027" ~"NVPERS7URN", # Pershing County
    county_code == "029" ~"NVSTOR9URN", # Storey County
    county_code == "031" ~"NVWASH7URN", # Washoe County
    county_code == "033" ~"NVWHIT3URN", # White Pine County
    county_code == "510" ~"NVCARS0URN" # Carson City
    )
  )

#### get NV county unemployment data
nv_unemployment_rates <- tq_get(c("NVCHUR1URN", # Churchill County
                                  "NVCLAR3URN", # Clark County
                                  "NVDOUG5URN", # Douglas County
                                  "NVELKO7URN", # Elko County
                                  "NVESME9URN", # Esmeralda County
                                  "NVEURE1URN", # Eureka County
                                  "NVHUMB3URN", # Humboldt County
                                  "NVLAND5URN", # Lander County
                                  "NVLINC7URN", # Lincoln County
                                  "NVLYON9URN", # Lyon County
                                  "NVMINE1URN", # Mineral County
                                  "NVNYEC3URN", # Nye County
                                  "NVPERS7URN", # Pershing County
                                  "NVSTOR9URN", # Storey County
                                  "NVWASH7URN", # Washoe County
                                  "NVWHIT3URN", # White Pine County
                                  "NVCARS0URN" # Carson City
                                  )
                                , get = "economic.data", 
                                from = "2016-01-01",
                                to = "2020-08-31"
                                ) %>%
  left_join(nv_counties, by = c("symbol" = "fred"))

#### Geofacet grid
mygrid <- data.frame(
  row = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6),
  col = c(4, 3, 2, 1, 2, 4, 3, 1, 2, 4, 1, 2, 3, 4, 3, 4, 4),
  code = c("007", "013", "027", "031", "001", "011", "015", "029", "019", "033", "510", "005", "021", "023", "009", "017", "003"),
  name = c("Elko County", "Humboldt County", "Pershing County", "Washoe County", "Churchill County", "Eureka County", "Lander County", "Storey County", "Lyon County", "White Pine County", "Carson City", "Douglas County", "Mineral County", "Nye County", "Esmeralda County", "Lincoln County", "Clark County"),
  stringsAsFactors = FALSE
)
# geofacet::grid_preview(mygrid)

#### Plot it!
ggplot(nv_unemployment_rates, aes(x = date, y = price)) +
  geom_line() +
  theme_minimal()+
  facet_geo(~ county_code, grid = mygrid, label = "name") +
  theme(axis.text.x = element_text(angle = 90),
        panel.border = element_rect(color = "black", size = 0.1, fill = NA)) +
  labs(
    title = "Unemployment Rates in Nevada, 2016-2020",
    y = "Unemployment Rate (%)",
    x = "Year"
  )

Grid data:

row,col,code,name
1,4,007,Elko County
1,3,013,Humboldt County
1,2,027,Pershing County
1,1,031,Washoe County
2,2,001,Churchill County
2,4,011,Eureka County
2,3,015,Lander County
2,1,029,Storey County
3,2,019,Lyon County
3,4,033,White Pine County
3,1,510,Carson City
4,2,005,Douglas County
4,3,021,Mineral County
4,4,023,Nye County
5,3,009,Esmeralda County
5,4,017,Lincoln County
6,4,003,Clark County

map

stedy commented 3 years ago

Closing as duplicate of https://github.com/hafen/geofacet/issues/272