Wednesday, December 2, 2015

2015 Whatcom County GE: GIS Code Part II


County Executive difference per precinct (Jack Louws - Joy Gilfilen). Click to Enlarge.  More maps and political piece here.



# Plotting "Joy_vs_Jack" (e.g. Good Ballots by Cumulative MatchBacks) for Jack and Joy 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 12:25 PM 12/1/2015

library(sp) 
library(rgdal)
library(rgeos)
detach(package:plyr)
library(dplyr)
# detach(package:dplyr)
# library(plyr)
library(ggplot2)
library(ggmap)

download.file("http://results.vote.wa.gov/results/current/export/20151103_WhatcomPrecincts.csv","20151103_WhatcomPrecincts.csv")
header <- c("Prop_DESC","Prop_NAME","Precinct1","Precinct2","Votes")
WCGEN2015 <- read.csv("C:/Politics/WC2015/11.23/20151103_WhatcomPrecincts.csv",fill=FALSE,header=FALSE,col.names=header,stringsAsFactors = FALSE)
#names(WCGEN2015)
#[1] "Prop_DESC" "Prop_NAME" "Precinct1" "Precinct2" "Votes"
data=filter(WCGEN2015,Precinct1 != "Total")
    
q1 <- t(dcast(filter(data,Prop_DESC == "Whatcom County Executive"),Prop_NAME ~ Precinct2))
Using Votes as value column: use value.var to override.
q1 <- as.data.frame(q1)[-1,];colnames(q1) <- c("Jack","Joy")
q1$Jack <- (as.numeric(as.character(q1$Jack)))
q1$Joy <- (as.numeric(as.character(q1$Joy)))
q1 <- arrange(with(q1,cbind("PID" = as.integer(as.character(row.names(q1))),q1,"Jack_Joy" = as.numeric(as.character(Jack-Joy)))))
cat('
 head(q1)
  PID Jack Joy Jack_Joy
1 101  168 145       23
2 102  189  78      111
3 103  269  98      171
4 104  174  48      126
5 105  102  54       48
6 106  155  79       76
')

# Matchbacks from Consolidated
setwd("C:/Politics")

MBJackTO <- q1[,c("PID","Jack")];colnames(MBJackTO) <- c("NAMES","VotesGood")
#MBJackTO <- arrange(data.frame(with(Jack_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))
MBJoyTO <- q1[,c("PID","Joy")];colnames(MBJoyTO) <- c("NAMES","VotesGood")
#MBJackTO <- arrange(data.frame(with(Jack_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))
Jack_Joy <- q1[,c("PID","Jack_Joy")];colnames(Jack_Joy) <- c("NAMES","VotesGood")
#MBJackTO <- arrange(data.frame(with(Jack_Consolidated_Good,(table(PrecinctID)))),(PrecinctID))

setwd("C:/Politics/WhatcomPrecincts")
WCshp <- readOGR("C:/Politics/WhatcomCountyPrecincts2015","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(MBJackTO) <- c("NAME", "VotesGood")
MBJackTO$NAME <- as.numeric(as.character(MBJackTO$NAME))
WCshp_merge_Jack <- sp::merge(WCshp,MBJackTO,by="NAME")

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

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

dataProjected_Jack <- WCshp_merge_Jack
dataProjected_Joy <- WCshp_merge_Joy
dataProjected_Jack_Joy <- WCshp_merge_Jack_Joy

dataProjected <- dataProjected_Joy

## Joy 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,
# ggPrecinct <- ggplot(data = FortifiedSPDF, aes(
                                                        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 = "Whatcom County for Joy Gilfilen", fill = "VotesGood")

print(ggPrecinct)
 ggsave("ggJoya.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat,
# ggPrecinct <- ggplot(data = FortifiedSPDF,aes(
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 = "Whatcom County for Joy Gilfilen", fill = "VotesGood")

 print(ggPrecinct)
  ggsave("ggJoyb.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))


 ## Jack GE Results
 dataProjected <- dataProjected_Jack

# 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 = "Whatcom County for Jack Louws", fill = "VotesGood")

print(ggPrecinct)
 ggsave("ggJacka.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

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 = "Whatcom County for Jack Louws", fill = "VotesGood")

 print(ggPrecinct)
 print(ggPrecinct)
  ggsave("ggJackb.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))


 ## Jack_Joy GE Results
dataProjected <- dataProjected_Jack_Joy
# 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,
# ggPrecinct <- ggplot(data = FortifiedSPDF, aes(
                                                        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 = "Whatcom County Executive", fill = "VotesGood")

print(ggPrecinct)
 ggsave("ggJack_Joya.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

ggPrecinct <- ggplot(data = FortifiedSPDF,aes(x=long, y=lat,
# ggPrecinct <- ggplot(data = FortifiedSPDF,aes(
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 = "Whatcom County Executive", fill = "VotesGood")

 print(ggPrecinct)
  ggsave("ggJack_Joyb.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))
  

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

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

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

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

dataProjected_Jack <- COBshp_merge_Jack
dataProjected_Joy <- COBshp_merge_Joy
dataProjected_Jack_Joy <- COBshp_merge_Jack_Joy

## Jack_Joy GE Results
dataProjected <- dataProjected_Jack

# 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 Jack", fill = "Votes Good")

print(ggPrecinct)
 ggsave("ggJackc.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

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 Jack", fill = "Votes Good")

 print(ggPrecinct)
  ggsave("ggJackd.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

  
##Joy GE Results
 dataProjected <- dataProjected_Joy
# 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 Joy", fill = "Votes Good")

print(ggPrecinct)
 ggsave("ggJoyc.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

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 Joy", fill = "Votes Good")

 print(ggPrecinct)
  ggsave("ggJoyd.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))
  
## Jack_Joy GE Results
dataProjected <- dataProjected_Jack_Joy
# 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 Executive", fill = "Votes Good")

print(ggPrecinct)
 ggsave("ggJackJoyc.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

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 Executive", fill = "Votes Good")

print(ggPrecinct)
print(ggPrecinct)
  ggsave("ggJackJoyd.jpg",ggPrecinct,width = 11, height = 8, units = "in", pointsize = 16,
          quality = 100, bg = "white", family = "", restoreConsole = TRUE,
          type = c("windows"))

No comments:

Post a Comment