Data entry and cleanup
library(ggplot2)
all_states <- map_data("state")
electionData <- read.csv("2012pres.csv")
names(electionData)
## [1] "StateAB" "state" "ObamaElectoral" "RomneyElectoral"
## [5] "ObamaPopular" "RomneyPopular" "OthersPopular" "TotalPopular"
electionData$TotalElectoral <- electionData$ObamaElectoral+electionData$RomneyElectoral
electionData$ObamaPerc <- electionData$ObamaPopular/electionData$TotalPopular
electionData$RomneyPerc <- electionData$RomneyPopular/electionData$TotalPopular
electionData$OtherPerc <- electionData$OthersPopular/electionData$TotalPopular
names(electionData)[2] <- "region"
stateData <- merge(all_states,electionData,by="region")
Basic Choropleths
plot2012Obama <- ggplot()+geom_polygon(data=stateData,aes(x=long, y=lat, group = group, fill=ObamaPerc),color="grey50")+coord_map()
plot2012Obama + scale_fill_gradient2(name="BHO Perc",low="darkred",mid="white",high="darkblue",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())

plot2012Romney <- ggplot()+geom_polygon(data=stateData,aes(x=long, y=lat, group = group, fill=RomneyPerc),color="grey50")+coord_map()
plot2012Romney + scale_fill_gradient2(name="WMR Perc",low="darkblue",mid="white",high="darkred",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())

Basic/Rectangular Cartograms (Treemaps)
library(treemap)
## Warning: package 'treemap' was built under R version 3.3.2
?treemap
## starting httpd help server ...
## done
RColorBrewer::display.brewer.all()

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="ObamaElectoral",type="value",palette="RdBu")

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="RomneyElectoral",type="value",palette="-RdBu")

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="ObamaPerc",type="value",palette="RdBu",mapping=c(0,.5,1),title="2012 Popular Vote by State")

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="RomneyPerc",type="value",palette="-RdBu",mapping=c(0,.5,1),title="2012 Popular Vote by State",title.legend="Romney's Percentage")

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="ObamaPerc",type="value",palette="RdBu",mapping=c(0,.5,1),algorithm="squarified",title="2012 Popular Vote by State")

treemap(electionData,index="StateAB",vSize="TotalElectoral",vColor="ObamaPerc",type="value",palette="RdBu",mapping=c(0,.5,1),algorithm = "pivotSize",title="2012 Popular Vote by State")

treemap(electionData,index="StateAB",vSize="TotalPopular",vColor="OtherPerc",type="value")

Create shapefile with election data
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.1-10, (SVN revision 622)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
## Path to GDAL shared files: C:/Users/knm6/Documents/R/win-library/3.3/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
## Path to PROJ.4 shared files: C:/Users/knm6/Documents/R/win-library/3.3/rgdal/proj
## Linking to sp version: 1.2-3
state_map <- readOGR(dsn = ".", layer = "States")
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "States"
## with 55 features
## It has 3 fields
names(state_map)
## [1] "STATE_ABBR" "STATE_NAME" "STATE_FIPS"
names(electionData)[1] <- "STATE_ABBR"
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.3.2
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
state_map@data <- state_map@data %>% left_join(electionData, by = "STATE_ABBR")
## Warning in left_join_impl(x, y, by$x, by$y, suffix$x, suffix$y): joining
## factors with different levels, coercing to character vector
str(state_map@data)
## 'data.frame': 55 obs. of 14 variables:
## $ STATE_ABBR : chr "DC" "AZ" "OH" "CA" ...
## $ STATE_NAME : Factor w/ 54 levels "Alabama","Alaska",..: 10 4 37 6 1 5 7 8 9 11 ...
## $ STATE_FIPS : Factor w/ 55 levels "00","01","02",..: 10 4 37 6 2 5 7 8 9 11 ...
## $ region : Factor w/ 51 levels "alabama","alaska",..: 9 3 36 5 1 4 6 7 8 10 ...
## $ ObamaElectoral : int 3 0 18 55 0 0 9 7 3 29 ...
## $ RomneyElectoral: int 0 11 0 0 9 6 0 0 0 0 ...
## $ ObamaPopular : int 267070 1025232 2827709 7854285 795696 394409 1323102 905083 242584 4237756 ...
## $ RomneyPopular : int 21381 1233654 2661437 4839958 1255925 647744 1185243 634892 165484 4163447 ...
## $ OthersPopular : int 5313 40368 91701 344304 22717 27315 61177 18985 5853 72976 ...
## $ TotalPopular : int 293764 2299254 5580847 13038547 2074338 1069468 2569522 1558960 413921 8474179 ...
## $ TotalElectoral : int 3 11 18 55 9 6 9 7 3 29 ...
## $ ObamaPerc : num 0.909 0.446 0.507 0.602 0.384 ...
## $ RomneyPerc : num 0.0728 0.5365 0.4769 0.3712 0.6055 ...
## $ OtherPerc : num 0.0181 0.0176 0.0164 0.0264 0.011 ...
writeOGR(state_map,dsn = ".",layer = "ElectoralPolygon",
driver = "ESRI Shapefile",overwrite_layer = TRUE)
## Warning in writeOGR(state_map, dsn = ".", layer = "ElectoralPolygon",
## driver = "ESRI Shapefile", : Field names abbreviated for ESRI Shapefile
## driver
Move to ScapeToad to create cartogram shapefile
Load cartogram shapefile and plot contents
state_cg <- readOGR(dsn = ".",layer = "ElectoralCartogramHQ")
## OGR data source with driver: ESRI Shapefile
## Source: ".", layer: "ElectoralCartogramHQ"
## with 55 features
## It has 15 fields
plot(state_map)

plot(state_cg)

Create data.frame object from cartogram shapefile
library(maptools)
## Checking rgeos availability: TRUE
library(plyr)
## -------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## -------------------------------------------------------------------------
##
## Attaching package: 'plyr'
## The following objects are masked from 'package:dplyr':
##
## arrange, count, desc, failwith, id, mutate, rename, summarise,
## summarize
## The following object is masked from 'package:maps':
##
## ozone
state_map_df <- state_cg
state_map_df@data$id <- rownames(state_map_df@data)
state_map_df.points <- fortify(state_map_df, region="id")
state.df.cart <- join(state_map_df.points, state_map_df@data, by="id")
Create, display, and save various cartograms
obamaCart2012 <- ggplot(state.df.cart) +
geom_polygon(aes(long,lat, group=group,fill=ObamPrc)) +
coord_equal()
obamaCart2012 + scale_fill_gradient2(name="BHO Perc",low="darkred",mid="white",high="darkblue",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())

obamaCartFinal <- obamaCart2012 + scale_fill_gradient2(name="BHO Perc",low="darkred",mid="white",high="darkblue",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())
obamaCartFinal

ggsave("cartBHO.png")
## Saving 7 x 5 in image
obamaCartFinal + expand_limits(fill = seq(0, 1, by = .1))

ggsave("expObama.png")
## Saving 7 x 5 in image
romneyCart2012 <- ggplot(state.df.cart) +
geom_polygon(aes(long,lat, group=group,fill=RmnyPrc)) +
coord_equal()
romneyCartFinal <- romneyCart2012 + scale_fill_gradient2(name="WMR Perc",low="darkblue",mid="white",high="darkred",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())
romneyCartFinal + expand_limits(fill = seq(0, 1, by = .1))

ggsave("expRomney.png")
## Saving 7 x 5 in image
library(scales)
romneyCart2012 + scale_fill_gradient2(name="WMR Perc",low="blue",mid="white",high="red",midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())

romneyCart2012 + scale_fill_gradient2(name="WMR Perc",low=muted("blue"),mid="white",high=muted("red"),midpoint=.5)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())

romneyCartFinal <- romneyCart2012 + scale_fill_gradient2(name="WMR Perc",low=muted("blue"),mid="white",high=muted("red"),midpoint=.5,guide=F)+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())
romneyCartFinal

ggsave("cartWMR.png")
## Saving 7 x 5 in image
otherCart2012 <- ggplot(state.df.cart) +
geom_polygon(aes(long,lat, group=group,fill=OthrsPp/TtlPplr)) +
coord_equal()
otherCart2012 <- otherCart2012 + scale_fill_gradient(name="3rd party share",low="white",high="darkgreen")+labs(x="",y="",title="2012 Election Results")+theme_classic()+ theme(axis.ticks.y = element_blank(),axis.text.y = element_blank(), axis.ticks.x = element_blank(),axis.text.x = element_blank())
otherCart2012

ggsave("cart3rdParty.png")
## Saving 7 x 5 in image