Leonard
Leonard

Reputation: 139

How to color states in US map in R

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: enter image description here

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

Upvotes: 1

Views: 10343

Answers (2)

desc
desc

Reputation: 1210

@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)

Upvotes: 4

IRTFM
IRTFM

Reputation: 263342

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)

enter image description here

Upvotes: 4

Related Questions