How to color states in US map in R

坚强是说给别人听的谎言 提交于 2021-02-07 10:31:11

问题


I want to draw a US map with some states have blue color and others have white color

Now, I have the list of names of state:

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

However, when I use:

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

It only shows:

This is not I want, I want to see other states with white color, what should I do?


回答1:


@rawr solution in comments worked. to create a full map with fill colors:

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



回答2:


At the moment you are asking R to only plot those states so you need to drop the region-argument and figure out a way to match color choices with the names of the states:

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

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

So try this:

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



来源:https://stackoverflow.com/questions/34301835/how-to-color-states-in-us-map-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!