什么等同于 map_data 与 FIP 代码以按县制作 R ggplot 地图
What is equivalent of map_data with FIP Code to make R ggplot map by county
我有一张按县划分的佐治亚州地图,其频率部分使用按县名的等值连接。由于名称不同,一些县正在减少。我需要使用 FIPS 代码而不是名称。
如何更改代码以根据 FIP 代码而不是名称加入?
# 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")
带有 FIP 代码的示例数据集的 link 在这里
https://drive.google.com/file/d/1GrDS8qq7sgQII3-s5EmX-8n304P1ujWa/view?usp=sharing
我能够加入地图包中的 county.fips 来创建地图。
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")
我有一张按县划分的佐治亚州地图,其频率部分使用按县名的等值连接。由于名称不同,一些县正在减少。我需要使用 FIPS 代码而不是名称。
如何更改代码以根据 FIP 代码而不是名称加入?
# 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")
带有 FIP 代码的示例数据集的 link 在这里 https://drive.google.com/file/d/1GrDS8qq7sgQII3-s5EmX-8n304P1ujWa/view?usp=sharing
我能够加入地图包中的 county.fips 来创建地图。
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")