Wednesday, January 28, 2015

Some GIS Electoral Code Examples: Part II





# Political piece is here.
# Plotting "Votes Good" (e.g. Good Ballots by Cumulative MatchBacks) for 2014 and 2012 General Elections
# Adapted from the geospatial blog-post by Steven Brey and Bethany Yollin of Mazama Science :
# [Using R: Working with Geospatial Data](http://mazamascience.com/WorkingWithData/?p=1277).
# - RMF 01/08/2015

library(sp) 
library(rgdal)
library(rgeos)
library(plyr)
library(ggmap)
library(ggplot2)

# Matchbacks from 2014 Consolidated
setwd("C:/Politics")
MB_2014_Consolidated <- read.delim("MB_2014_Consolidated.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_2014_Consolidated_status <- subset(MB_2014_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2014_Consolidated_Good <- subset(MB_2014_Consolidated_status, AVReturnStatus == "Good")
MB2014TO <- arrange(data.frame(with(MB_2014_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

# Matchbacks from 2012 Consolidated
MB_2012_Consolidated <- read.delim("MBGE2012.txt", header = TRUE, strip.white = TRUE, sep = "\t", quote = "", stringsAsFactors = FALSE)
MB_Consolidated_status <- subset(MB_2012_Consolidated, select = c(RegistrationNumber,PrecinctID,AVReturnStatus,AVReturnChallenge))
MB_2012_Consolidated_Good <- subset(MB_Consolidated_status, AVReturnStatus == "Good")
MB2012TO <- arrange(data.frame(with(MB_2012_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

setwd("C:/Politics/WhatcomPrecincts")
WCshp <- readOGR("C:/Politics/WhatcomPrecincts","NewPrecincts2014")
plot(WCshp,col="red")
names(WCshp)

setwd("C:/Politics/BellinghamPrecincts")
COBshp <- readOGR("C:/Politics/BellinghamPrecincts","CityOfBellinghamVotingPrecincts2014")
plot(COBshp,col="blue")
names(COBshp)

### WC Data
colnames(MB2014TO) <- c("NAME", "VotesGood")
MB2014TO$NAME <- as.numeric(as.character(MB2014TO$NAME))
WCshp_merge_2014 <- sp::merge(WCshp,MB2014TO,by="NAME")

colnames(MB2012TO) <- c("NAME", "VotesGood")
MB2012TO$NAME <- as.numeric(as.character(MB2012TO$NAME))
WCshp_merge_2012 <- sp::merge(WCshp,MB2012TO,by="NAME")

dataProjected_2014 <- WCshp_merge_2014
dataProjected_2012 <- WCshp_merge_2012
dataProjected <- dataProjected_2014

## 2014 GE Results
# add to data a new column termed "id" composed of the rownames of data
 dataProjected@data$id <- rownames(dataProjected@data)
# create a data.frame from our spatial object
SpatialObjectDF <- fortify(dataProjected, region = "id")
# create a data.frame name (potentially different from layerName)
dataName <- "dataProjected"
# merge the "fortified" data with the data from our spatial object
FortifiedSPDF <- merge(SpatialObjectDF, dataProjected@data, by = "id")

ggPrecinct <- ggplot(data = FortifiedSPDF, aes(x=long, y=lat,
                                                        group = group, 
                                                        fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  scale_colour_brewer(breaks=c(1:109),labels=c(1:109)) +
  # scale_fill_gradient(breaks=c(rev(as.numeric(sort(unique(FortifiedSPDF$VotesGood)))))) +
  #                   labels=c(min(sort(FortifiedSPDF$VotesGood)) : max(sort(FortifiedSPDF$VotesGood)))) +
  coord_equal() +
  theme(axis.title = element_blank(), axis.text = element_blank()) +
  labs(title = "Precincts of Whatcom County without Bellingham (200 Series) 2014", fill = "VotesGood")

print(ggPrecinct)
 ggsave("gg2014a.jpg",ggPrecinct)

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat, group = group,
                                            fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  # scale_fill_continuous(low = "#132B43", high = "#56B1F7",
    scale_fill_continuous(low = rgb(1,0,0,.25), high = rgb(0,0,1,.75),
       space = "Lab", na.value = "grey50", guide = "colourbar") +
  coord_equal() +
  # theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) +
  labs(title = "Precincts of Whatcom County without Bellingham (200 Series) 2014", fill = "VotesGood")

 print(ggPrecinct)
  ggsave("gg2014b.jpg",ggPrecinct)
 ## 2012 GE Results
 dataProjected <- dataProjected_2012

# add to data a new column termed "id" composed of the rownames of data
 dataProjected@data$id <- rownames(dataProjected@data)
# create a data.frame from our spatial object
SpatialObjectDF <- fortify(dataProjected, region = "id")
# create a data.frame name (potentially different from layerName)
dataName <- "dataProjected"
# merge the "fortified" data with the data from our spatial object
FortifiedSPDF <- merge(SpatialObjectDF, dataProjected@data, by = "id")

ggPrecinct <- ggplot(data = FortifiedSPDF, aes(x=long, y=lat,
                                                        group = group, 
                                                        fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  scale_colour_brewer(breaks=c(1:109),labels=c(1:109)) +
  # scale_fill_gradient(breaks=c(rev(as.numeric(sort(unique(FortifiedSPDF$VotesGood)))))) +
  #                   labels=c(min(sort(FortifiedSPDF$VotesGood)) : max(sort(FortifiedSPDF$VotesGood)))) +
  coord_equal() +
  theme(axis.title = element_blank(), axis.text = element_blank()) +
  labs(title = "Precincts of Whatcom County without Bellingham (200 Series) 2012", fill = "VotesGood")

print(ggPrecinct)
 ggsave("gg2012a.jpg",ggPrecinct)

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat, group = group,
                                            fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  # scale_fill_continuous(low = "#132B43", high = "#56B1F7",
    scale_fill_continuous(low = rgb(1,0,0,.25), high = rgb(0,0,1,.75),
       space = "Lab", na.value = "grey50", guide = "colourbar") +
  coord_equal() +
  # theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) +
  labs(title = "Precincts of Whatcom County without Bellingham (200 Series) 2012", fill = "VotesGood")

 print(ggPrecinct)
  ggsave("gg2012b.jpg",ggPrecinct)


#####------###### Now Bellingham

colnames(MB2014TO) <- c("PRECINCT_N", "VotesGood")
MB2014TO$PRECINCT_N <- as.numeric(as.character(MB2014TO$PRECINCT_N))
COBshp_merge_2014 <- sp::merge(COBshp,MB2014TO,by="PRECINCT_N")

colnames(MB2012TO) <- c("PRECINCT_N", "VotesGood")
MB2012TO$PRECINCT_N <- as.numeric(as.character(MB2012TO$PRECINCT_N))
COBshp_merge_2012 <- sp::merge(COBshp,MB2012TO,by="PRECINCT_N")

dataProjected_2014 <- COBshp_merge_2014
dataProjected_2012 <- COBshp_merge_2012

## 2014 GE Results
dataProjected <- dataProjected_2014

# add to data a new column termed "id" composed of the rownames of data
 dataProjected@data$id <- rownames(dataProjected@data)
# create a data.frame from our spatial object
 SpatialObjectDF <- fortify(dataProjected, region = "id")
# create a data.frame name (potentially different from layerName)
 dataName <- "dataProjected"
# merge the "fortified" data with the data from our spatial object
FortifiedSPDF <- merge(SpatialObjectDF, dataProjected@data, by = "id")

ggPrecinct <- ggplot(data = FortifiedSPDF, aes(x=long, y=lat,
                                                        group = group, 
                                                        fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  scale_colour_brewer(breaks=c(1:68),labels=c(1:68)) +
  coord_equal() +
  theme(axis.title = element_blank(), axis.text = element_blank()) +
  labs(title = "Bellingham Precincts 2014", fill = "Votes Good")

print(ggPrecinct)
 ggsave("gg2014c.jpg",ggPrecinct)

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat, group = group,
                                            fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  # scale_fill_continuous(low = "#132B43", high = "#56B1F7",
    scale_fill_continuous(low = rgb(1,0,0,.25), high = rgb(0,0,1,.75),
       space = "Lab", na.value = "grey50", guide = "colourbar") +
  coord_equal() +
  # theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) +
  labs(title = "Bellingham Precincts 2014", fill = "Votes Good")

 print(ggPrecinct)
  ggsave("gg2014d.jpg",ggPrecinct)
  
##2012 GE Results
 dataProjected <- dataProjected_2014
# add to data a new column termed "id" composed of the rownames of data
 dataProjected@data$id <- rownames(dataProjected@data)
# create a data.frame from our spatial object
 SpatialObjectDF <- fortify(dataProjected, region = "id")
# create a data.frame name (potentially different from layerName)
 dataName <- "dataProjected"
# merge the "fortified" data with the data from our spatial object
FortifiedSPDF <- merge(SpatialObjectDF, dataProjected@data, by = "id")

ggPrecinct <- ggplot(data = FortifiedSPDF, aes(x=long, y=lat,
                                                        group = group, 
                                                        fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  scale_colour_brewer(breaks=c(1:68),labels=c(1:68)) +
  coord_equal() +
  theme(axis.title = element_blank(), axis.text = element_blank()) +
  labs(title = "Bellingham Precincts 2012", fill = "Votes Good")

print(ggPrecinct)
 ggsave("gg2012c.jpg",ggPrecinct)

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat, group = group,
                                            fill = VotesGood)) +
  geom_polygon()  +
  geom_path(color = "white") +
  # scale_fill_continuous(low = "#132B43", high = "#56B1F7",
    scale_fill_continuous(low = rgb(1,0,0,.25), high = rgb(0,0,1,.75),
       space = "Lab", na.value = "grey50", guide = "colourbar") +
  coord_equal() +
  # theme(legend.position = "none", title = element_blank(), axis.text = element_blank()) +
  labs(title = "Bellingham Precincts 2012", fill = "Votes Good")

 print(ggPrecinct)
  ggsave("gg2012d.jpg",ggPrecinct)

No comments:

Post a Comment