I would like to generate a SpatialPolygonsDataFrame that I can use to plot a cartogram whose polygons are proportional to a variable.
A clear example is how FiveThirtyEight created a hex cartogram with each US state made up of hex polygons equating to the number of electoral college seats they have. The data they use is straight forward, US geodata + electoral college distribution by state + state names. The outcome is here: https://projects.fivethirtyeight.com/2016-election-forecast/#electoral-map
I tried to reproduce the cartogram using the code below but can't quite get some aspect of the initial object: Specifically, how do I merge the electoral college data with the US geodata so that I have 538 observations? All reproductions I have seen start off from a ready SpatialPolygonsDataFrame object file from FiveThirtyEight.
Below is my work so far, trying to merge the two objects but ending up with a dataframe without the polygons (50 rows instead of 538).
Get US state level shapefile from GADM:
us_states <- raster::getData("GADM", country = "United States", level = 1)
us_states
class : SpatialPolygonsDataFrame
features : 51
extent : -179.1506, 179.7734, 18.90986, 72.6875 (xmin, xmax, ymin, ymax)
crs : +proj=longlat +datum=WGS84
variables : 10
Get electoral_college from https://worldpopulationreview.com/state-rankings/electoral-votes-by-state
electoral_college <- read.csv("./ecollege.csv")
Merge the two:
geocoll <- merge(x=us_states, y=electoral_college, by.x="NAME_1", by.y="State")
This results in a df (geocoll) without the polygons. Ideally, I would want x polygons (hex, circles or squares) for a state. 55 for California, for example.