Search code examples
rggplot2mapsfips

What is equivalent of map_data with FIP Code to make R ggplot map by county


I have a map of Georgia by county with frequencies that is partially working using an equijoin by county name. Some counties are dropping off because of name differences. I need to use FIPS code instead of name.

How can I change the code to join based on FIPs code instead of name?

enter image description here

# Input load. Please do not change #
`dataset` = read.csv('C:/temp/input_df_df0e8484-0924-4613-9af6-2fdc4b3e67ad.csv', check.names = FALSE, encoding = "UTF-8", blank.lines.skip = FALSE);
# Original Script. Please update your script content here and once completed copy below section back to the original editing window #
library(tidyverse)
library(readr)
library(maps)

frequency_final <- dataset%>% 
                        mutate(county_join = tolower(str_remove_all(County, " County")))  

state<- map_data("county",dataset$State,)
state_final <- inner_join(state, frequency_final ,by=c('subregion' = 'county_join'))

state_base <- ggplot(data = state_final , mapping = aes(x = long, y = lat, group = subregion)) + 
coord_fixed(1.3) + 
geom_polygon(color = "black", fill = "gray")

ditch_the_axes <- theme(
axis.text = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
panel.border = element_blank(),
panel.grid = element_blank(),
axis.title = element_blank()
)

state_base + 
geom_polygon(aes(fill =ID), color = "white") +
geom_polygon(color = "black", fill = NA) +
theme_bw() +
ditch_the_axes +
scale_fill_gradientn(colours = rev(rainbow(7)),
               breaks = c(2, 4, 10, 100, 1000, 10000),
               trans = "log10")

A link to the sample dataset with FIP Codes is here https://drive.google.com/file/d/1GrDS8qq7sgQII3-s5EmX-8n304P1ujWa/view?usp=sharing


Solution

  • I was able to join to county.fips in the maps package to create the map.

    library(tidyverse)
    library(readr)
    library(maps)
    library(sringr)
    
    data(county.fips)
    
    frequency_final <- dataset%>% 
                            mutate(county_join = tolower(str_remove_all(County, " County"))) %>% 
                            mutate(fips_join = as.integer(paste(StateFIPSCode, str_pad(CountyFipsCode,3,pad="0"),sep="")))
    
    state<- map_data("county",dataset$State)
    
    state2  <- state %>%
      mutate(polyname = paste(region,subregion,sep=",")) %>%
      left_join(county.fips, by="polyname")
    
    state_final <- inner_join(state2, frequency_final ,by=c('fips' = 'fips_join'))
    
    state_base <- ggplot(data = state_final , mapping = aes(x = long, y = lat, group = subregion)) + 
    coord_fixed(1.3) + 
    geom_polygon(color = "black", fill = "gray")
    
    ditch_the_axes <- theme(
    axis.text = element_blank(),
    axis.line = element_blank(),
    axis.ticks = element_blank(),
    panel.border = element_blank(),
    panel.grid = element_blank(),
    axis.title = element_blank()
    )
    
    state_base + 
    geom_polygon(aes(fill =ID), color = "white") +
    geom_polygon(color = "black", fill = NA) +
    theme_bw() +
    ditch_the_axes +
    scale_fill_gradientn(colours = rev(rainbow(7)),
                   breaks = c(2, 4, 10, 100, 1000, 10000),
                   trans = "log10")
    

    enter image description here