【问题标题】:What is equivalent of map_data with FIP Code to make R ggplot map by county什么相当于带有 FIP 代码的 map_data 以按县制作 R ggplot 地图
【发布时间】:2019-04-13 23:16:34
【问题描述】:

我有一张按县划分的佐治亚州地图,其频率部分使用县名的等值连接。由于名称差异,一些县正在放弃。我需要使用 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 代码的示例数据集的链接 https://drive.google.com/file/d/1GrDS8qq7sgQII3-s5EmX-8n304P1ujWa/view?usp=sharing

【问题讨论】:

  • 几个与映射相关的包都有县 FIPS 代码的查找表。在maps 中,它是数据集county.fips。加入你的数据。除此之外,您最好将数据样本放在帖子中,而不是让人们需要从第三方下载未知文件

标签: r ggplot2 maps fips


【解决方案1】:

我能够加入地图包中的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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多