如何在R

时间:2015-12-16 00:11:46

标签: r plot mapping

我想画一张美国地图,其中一些州有蓝色,另一些州有白色

现在,我有州名称列表:

c=c("ILLINOIS", "KANSAS","LOUISIANA","MAINE","MICHIGAN","MINNESOTA","MISSISSIPPI" )

然而,当我使用:

map(database = "state",regions = c,col = "blue",fill=T)

它只显示: enter image description here

这不是我想要的,我想看到其他白色状态,我该怎么办?

2 个答案:

答案 0 :(得分:4)

目前你要求R只绘制那些状态,所以你需要删除region - 参数并找出一种方法来匹配颜色选择和状态名称:

require(maps)
namevec <- map(database = "state", col = "blue",fill=T, namesonly=TRUE)

> str(namevec)
 chr [1:63] "alabama" "arizona" "arkansas" "california" ...

所以试试这个:

map(database = "state",col = c("white", "blue")[1+(namevec %in% tolower(c) )],fill=T)

enter image description here

答案 1 :(得分:4)

评论中的@rawr解决方案有效。使用填充颜色创建完整地图:

map(database = "state")
map(database = "state",regions = c,col = "blue",fill=T,add=TRUE)