R:地图包填充 states/counties 不在数据中

R: Maps Package filling in states/counties that aren't in data

我用以下代码制作了一张美国州地图,但我的数据不包括所有州,但无论如何地图上的所有内容都是彩色的。这很奇怪,因为我有一个变量可以清楚地为数据中的每个区域分配一种颜色。当我使用选项 (exact=TRUE) 时,它不会 return 任何地图并且出现错误。为什么不在地图中将缺失的数据留空?这是否意味着颜色是随机的?

这是我使用 excel 格式的数据的 link:https://docs.google.com/spreadsheets/d/19VHQdl5-i3r9OzJmJsB9FsrQU5Qk_lnkWmspq0ilAN0/edit?usp=sharing

如果您将这个 excel 文件加载到 R 中并将数据库保存为 Trump,那么以下所有代码都应该可以使这个 map image

library(maps)

# I created a set of colors I want to use, then a variable that numbers the levels
# The variable has an integer that will tell the map which color in the list to color the region
colors = c("#F1EEF6", "#D4B9DA", "#DF65B0", "#DD1C77", "#980043")
Trump$fraction_votes2 <- as.character(Trump$fraction_votes)
Trump$fraction_votes2 <- as.numeric(Trump$fraction_votes2)
Trump$colorBuckets <- as.numeric(cut(Trump$fraction_votes2, c(0, 0.3, 0.4, 0.5, 0.6, 1.0)))
lnames = c("0-30%", "31-40%", "41-50%", "51-60%", "61-100%")

par(mar=c(1,1,1,1)) # This solves the "plot region too large problem)
map("county", fill=TRUE, exact=TRUE, col = colors[Trump$colorBuckets] , lwd = 0.1)
title(main="Trump: Percentage of Votes Won by County",               font.main=2,cex.main=1.2, col.main="maroon4", family="Gill Sans Light")
legend("bottomleft",lnames, col = colors, lwd=3,bty="n"
   , cex=0.3, text.font=8, y.intersp=2, title="Voter Percentages\n Key") 

这是一个更完整的解决方案。这将绘制整个地图。关键是要确保按县列出的颜色与地图函数使用的列表的顺序相同。我将 map(county) 函数调用到 return 列表。然后我将列表拆分成一个数据框。使用数据框 "statelist" 中的 "col" 列分配正确的颜色。

#function to split a string and return a 2 column dataframe
strtodf<-function (list){
  slist<-strsplit(list, ",")
  x<-sapply(slist, FUN= function(x) {x[1]})
  y<-sapply(slist, FUN= function(x) {x[2]})
  df<-data.frame(state=x, county=y, stringsAsFactors = FALSE)
  return(df)
}

trump<-read.csv("trump.csv")
colors = c("#F1EEF6", "#D4B9DA", "#DF65B0", "#DD1C77", "#980043")
lnames = c("0-30%", "31-40%", "41-50%", "51-60%", "61-100%")
#create smaller dataframe with just columns of interest
trumpReduced<-data.frame(county=tolower(trump$county), state=tolower(trump$state), stringsAsFactors = FALSE)
trumpReduced$fraction_votes2 <- as.numeric(as.character(trump$fraction_votes))
trumpReduced$colorBuckets <- as.numeric(cut(trumpReduced$fraction_votes2, c(0, 0.3, 0.4, 0.5, 0.6, 1.0)))

library(maps)
#get the list of counties plotted in the map in the correct order
maplist<-map("county", namesonly = TRUE, plot=FALSE)
statelist<-strtodf(maplist)  #convert to dataframe
statelist$row<-as.numeric(rownames(statelist))  #index column
statelist$col<-NA   #add default color

#merge counties to the correct color and resort 
statelist<-merge(statelist, trumpReduced, sort=FALSE, all.x=TRUE)
statelist<-statelist[order(statelist$row),]

#plot map.
maplist<-map("county", fill=TRUE, col=colors[statelist$colorBuckets])
title(main="Trump: Percentage of Votes Won by County", font.main=2, cex.main=1.2, col.main="maroon4", family="Gill Sans Light")
legend("bottomleft", lnames, col = colors, lwd=3,bty="n", cex=0.3, text.font=8, y.intersp=2, title="Voter Percentages\n Key")