geom_map“map_id”参考问题 [英] geom_map "map_id" reference issue

查看:1000
本文介绍了geom_map“map_id”参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用FIPS代码连接两个数据集来创建美国县的等值线图。我正在使用 maps 软件包 county.fips 数据,像这样组合成一个data.table(可能不是整合FIPS数据的最优雅的方式):

  library( ggplot2)
图书馆(地图)
图书馆(data.table)
县< - map_data(县)
数据(县.fips)
县。 fips< - as.data.table(county.fips)
county.fips $ polyname< - as.character(county.fips $ polyname)
county.fips [,paste0(type ,1:2):= tstrsplit(polyname,,)]
names(county.fips)< - c(FIPS,polyname,region,subregion)
县< - 合并(县,县,地区,by = c(地区,子地区),全部= T)
县< - 县[,1:7]
县< - as.data.table(县)
县< - na.omit(县)
setkey(县,定购)
县[区域==华盛顿& subregion ==san juan,FIPS:= 53055]
county [region ==washington& subregion ==pierce,FIPS:= 53053]
county [region ==florida& subregion ==okaloosa,FIPS:= 12091]
county [region ==louisiana& subregion ==st martin,FIPS:= 22099]
county [region ==north carolina& subregion ==currituck,FIPS:= 37053]
county [region ==texas& subregion ==galveston,FIPS:= 48167]
county [region ==virginia& subregion ==accomack,FIPS:= 51001]

我想使用数据集制作地图,并使用具有相应FIPS列的不同数据集填写相应的县。使用 geom_map ,特别是 map_id 参数时会出现问题。



以下代码返回错误单位(x,default.units)中的错误:'x'和'units'的长度必须> 0 当我使用 map_id = FIPS

运行时

  ggplot()+ 
geom_map(data = county,map = county,$ b $ aes(x = long,y = lat,map_id = FIPS))

但是,使用 map_id = region 运行它会返回一个普通地图和 em>使用 map_id = subregion 运行它会返回一个包含3个缺失状态中的2个的映射。我找到的最接近的答案是


I am trying to create a choropleth map of US counties with two datasets connected by FIPS codes. I am using the maps package county and county.fips data, combined into one data.table like this (probably not the most elegant way of integrating the FIPS data):

    library(ggplot2)
    library(maps)
    library(data.table)
    county <- map_data("county")    
    data(county.fips)
    county.fips <- as.data.table(county.fips)
    county.fips$polyname <- as.character(county.fips$polyname)    
    county.fips[, paste0("type", 1:2) := tstrsplit(polyname, ",")]
    names(county.fips) <- c("FIPS","polyname","region","subregion")
    county <- merge(county, county.fips, by=c("region", "subregion"), all=T)
    county <- county[,1:7]
    county <- as.data.table(county)
    county <- na.omit(county)
    setkey(county, order)
    county[region=="washington" & subregion=="san juan", FIPS := 53055]
    county[region=="washington" & subregion=="pierce", FIPS := 53053]
    county[region=="florida" & subregion=="okaloosa", FIPS := 12091]
    county[region=="louisiana" & subregion=="st martin", FIPS := 22099]
    county[region=="north carolina" & subregion=="currituck", FIPS := 37053]
    county[region=="texas" & subregion=="galveston", FIPS := 48167]
    county[region=="virginia" & subregion=="accomack", FIPS := 51001]

I want to use the county dataset here to make the map and use a different dataset with a corresponding FIPS column to fill out the respective counties. The issue comes up when using geom_map and specifically the map_id argument.

The following code returns the error Error in unit(x, default.units) : 'x' and 'units' must have length > 0 when I run it with map_id=FIPS

    ggplot() +
  geom_map(data=county, map=county,
           aes(x=long, y=lat, map_id=FIPS))

However, running it with map_id=region returns a normal map and running it with map_id=subregion returns a map with about 2 out of 3 states missing. The closest answer I found was this, suggesting that map_id needs to be set to region or id, but changing the FIPS column name didn't help.

Can anyone explain what is going on here? My understanding is that map_id is only needed as a key to another df$column; am I incorrect in that? Ideally I would like to be able to tie in my second dataset, through the FIPS column, like this:

    ggplot() +
  geom_map(data=county, map=county,
           aes(x=long, y=lat, map_id=FIPS)) +
  geom_map(data=DT2, map=county,
           aes(fill=Revenue, map_id=FIPS))

解决方案

A couple things going on here. First, I noticed in the example above, it's cutting off the leading zeros on some of the FIPS codes. All FIPS need to be five digits. You can add the leading zeros by adding this line to the end of the data prep.

county$FIPS <- formatC(county$FIPS, width = 5, format = "d", flag = "0")

As for ggplot, you're missing group=group in your aes(). It's hard to reproduce because I'm not sure what you're using for the choropleth fill, but the following should work:

ggplot(county, aes(long, lat, group = group)) +
geom_polygon(aes(fill = YOUR_FILL_DATA), colour = alpha("white", 1/2), size = 0.2)

EDIT: I generated a column of random numbers to use as a fill rate:

county$new.row <- sample(100, size = nrow(county), replace = TRUE)

and ran the same ggplot code from above.

这篇关于geom_map“map_id”参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆