A choropleth map displays divided geographic regions that are colored in relation to some numerical value. The maps
library provides some basic maps for you to do some quick visualization. Here is a map of the continental US.
library(maps)
map('state', projection = "polyconic" )
Some stuff to add. This is the stuff.
I grabbed some census data from the US Census site for each state and saved it as a CSV file. It is currently hosted on a Github site I use for all my ENVS teaching and we can read it directly from Github using the URL.
library(readr)
url <- "https://raw.githubusercontent.com/dyerlab/ENVS-Lectures/master/lectures/spatial/chloropleth_maps/states.csv"
sz <- read_csv( url )
head(sz)
In this data set, there are 48 states (Hawaii & Alaska are not there) and the District of Columbia. To color these, I’m going to use a color palette defined in the RColorBrewer
library that goes from yellow to orange to red. This is a sequential color ramp (see brewer.pal.info
for other palettes).
library(RColorBrewer)
colors <- brewer.pal(9,"YlOrRd")
colors
[1] "#FFFFCC" "#FFEDA0" "#FED976" "#FEB24C" "#FD8D3C" "#FC4E2A" "#E31A1C"
[8] "#BD0026" "#800026"
#FFFFCC
#FFEDA0
#FED976
#FEB24C
#FD8D3C
#FC4E2A
#E31A1C
#BD0026
#800026
Then I’m going stretch these colors out from 9 distinct colors to 49 colors, one for each State and D.C.
colors <- colorRampPalette( colors )(49)
colors
[1] "#FFFFCC" "#FFFCC4" "#FFF9BD" "#FFF6B6" "#FFF3AE" "#FFF0A7" "#FFEDA0"
[8] "#FEE999" "#FEE692" "#FEE38B" "#FEDF84" "#FEDC7D" "#FED976" "#FED26F"
[15] "#FECC68" "#FEC561" "#FEBF5A" "#FEB853" "#FEB24C" "#FDAB49" "#FDA546"
[22] "#FD9F44" "#FD9941" "#FD933E" "#FD8D3C" "#FC8239" "#FC7836" "#FC6D33"
[29] "#FC6330" "#FC582D" "#FC4E2A" "#F74527" "#F33C25" "#EF3423" "#EB2B20"
[36] "#E7221E" "#E31A1C" "#DC151D" "#D6111F" "#D00D20" "#C90822" "#C30424"
[43] "#BD0026" "#B20026" "#A80026" "#9E0026" "#940026" "#8A0026" "#800026"
#FFFFCC
#FFFCC4
#FFF9BD
#FFF6B6
#FFF3AE
#FFF0A7
#FFEDA0
#FEE999
#FEE692
#FEE38B
#FEDF84
#FEDC7D
#FED976
#FED26F
#FECC68
#FEC561
#FEBF5A
#FEB853
#FEB24C
#FDAB49
#FDA546
#FD9F44
#FD9941
#FD933E
#FD8D3C
#FC8239
#FC7836
#FC6D33
#FC6330
#FC582D
#FC4E2A
#F74527
#F33C25
#EF3423
#EB2B20
#E7221E
#E31A1C
#DC151D
#D6111F
#D00D20
#C90822
#C30424
#BD0026
#B20026
#A80026
#9E0026
#940026
#8A0026
#800026
Then I can cut
the data into 49 chunks and then assign each state to one of thos
groups <- cut( sz$population, 49 )
sz$colors <- colors[ groups ]
head(sz)
map( 'state',
col=sz$colors,
fill=TRUE,
projection="polyconic")