r - plotting US heat map with text -


inputs:

percent.turnout  us.state 70               ca 80               nm 76               ri 

i have data each of 50 states in us. also, state abbreviation us.state consistent abbreviations in function state.abb

i create map percent.turnout printed on each state. furthermore, using colorbrewer package, color each state based on percent.turnout relative other states.

i not familiar ggplot syntax, suggestions in base r appreciated (if feasible)

enter image description here

if you'd use ggplot2, major thing need map state abbreviation column full state name in lower case (for this, can use state.name, make sure apply tolower() on in right format).

from there, it's matter of joining dataset state's geospatial information , plotting data. following segment of code takes through step step:

# first, need ggplot2 library: > library(ggplot2) # load geospatial data states # (there more options map_data function,  # if intrested in taking look). > states <- map_data("state") # here i'm creating sample dataset yours.  # dataset have 2 columns: region (or state) # , number represent value # want plot (here value numerical order of states). > sim_data <- data.frame(region=unique(states$region), percent.turnout=match(unique(states$region), unique(states$region))) # merge our dataset geospatial data: > sim_data_geo <- merge(states, sim_data, by="region") # following should give plot without numbers:  > qplot(long, lat, data=sim_data_geo, geom="polygon", fill=percent.turnout, group=group) 

this output of segment of code above:

enter image description here

now, said you'd add value percent.turnout map. here, need find center point of various states. can calculate geospatial data retrieved above (in states dataframe), results won't impressive. thankfully, r has values centers of states calculated us, , can leverage that, follows:

# we'll use state.center list tell # center of state is. > snames <- data.frame(region=tolower(state.name), long=state.center$x, lat=state.center$y) # again, need join our original dataset  # value should printed @ center. > snames <- merge(snames, sim_data, by="region") # , finally, put together:  > ggplot(sim_data_geo, aes(long, lat)) + geom_polygon(aes(group=group, fill=percent.turnout)) + geom_text(data=snames, aes(long, lat, label=percent.turnout)) 

and output of the last statement above:

enter image description here


Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -