FIPS コードで接続された 2 つのデータセットを使用して、米国の郡のコロプレス マップを作成しようとしています。maps
パッケージcounty
とデータを、次のように 1 つの data.table に組み合わせて使用していますcounty.fips
(おそらく、FIPS データを統合する最もエレガントな方法ではありません)。
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]
ここでデータセットを使用しcounty
てマップを作成し、対応する FIPS 列を持つ別のデータセットを使用してそれぞれの郡を記入します。geom_map
特にmap_id
引数を使用すると、問題が発生します。
次のコードError in unit(x, default.units) : 'x' and 'units' must have length > 0
を実行すると、エラーが返されますmap_id=FIPS
ggplot() +
geom_map(data=county, map=county,
aes(x=long, y=lat, map_id=FIPS))
ただし、 で実行すると法線map_id=region
マップが返され、 で実行すると3 つの状態のうち約 2 つが欠落しているマップが返されます。私が見つけた最も近い答えはこれで、またはに設定する必要があることを示唆していますが、列名を変更しても役に立ちませんでした。map_id=subregion
map_id
region
id
FIPS
ここで何が起こっているのか誰でも説明できますか? 私の理解では、それmap_id
は別のキーとしてのみ必要df$column
です。私はそれで間違っていますか?FIPS
理想的には、次のように、列を介して 2 番目のデータセットを結合できるようにしたいと考えています。
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))