diff --git a/.RData b/.RData index 60c2378..3a3d964 100644 Binary files a/.RData and b/.RData differ diff --git a/.Rhistory b/.Rhistory index 46a06b8..9045676 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,30 +1,24 @@ -panel.border = element_blank(), -panel.background = element_blank(), -legend.title=element_blank()) +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=22)) +gg <- gg + theme(plot.caption=element_text(face="bold", family="Lato Regular", size=9, color="gray28", margin=margin(t=10, r=80))) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(strip.background=element_blank()) +gg <- gg + theme(strip.text.x = element_text(size = 10, colour = "grey5")) gg -ggsave(gg, file="output/pres_chart.png", width=4.7, height=4, type="cairo-png") -ggsave(gg, file=paste0("output/archive/pres_chart", date_time, ".png"), width=4.7, height=4, type="cairo-png") -image_background <- image_read("background.png") -trend_logo <- image_read("trend_masthead.png") -pres_map <- image_read("output/pres_map.png") %>% -image_crop("1700x1200+360") -pres_chart <- image_read("output/pres_chart.png") -pres_map_edit <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path="output/pres_map.png", format="png") -pres_map_edit2 <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path=paste0("output/archive/pres_map", date_time, ".png"), format="png") -# FTP UPLOADING +ggsave(gg, file="output/white_population.png", width=8, height=5, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/white_population.png") +ftpUpload("output/white_population.png", ftp_url) +View(per) +per$OC <- ifelse(Clinton>Obama, "Clinton", "Obama") +per$OC <- ifelse(per$Clinton>per$Obama, "Clinton", "Obama") +table$per$OC +table(per$OC) +26/128 +per$TR <- ifelse(per$Trump>per$Romney, "Trump", "Romney") +table(per$TR) +y2012 <- read.csv("data/2012_election_results.csv", stringsAsFactors=F) +y2012 <- y2012[1:169,] source("keys.R") -ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/pres_map.png") -ftp_url_archive <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/archive/pres_map", date_time, ".png") -ftpUpload("output/pres_map.png", ftp_url) -ftpUpload(paste0("output/archive/pres_map", date_time, ".png"), ftp_url_archive) +library(censusapi) library(jsonlite) library(dplyr) library(purrr) @@ -109,76 +103,6 @@ pres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_result pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), "Clinton", pres_results$winner) pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), "Trump", pres_results$winner) pres_results$winner <- factor(pres_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) -town_shape <- readOGR(dsn="maps", layer="ctgeo") -town_shape_df <- fortify(town_shape, region="NAME10") -voters_map <- left_join(town_shape_df, pres_results) -voters_map$color <- "" -voters_map$color <- ifelse(is.na(voters_map$winner) | voters_map$winner=="Results not ready", "gray84", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="", "gray50", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="Trump", "tomato", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="Clinton", "lightskyblue", voters_map$color) -# theme_opts <- list(theme(panel.grid.minor = element_blank(), -# panel.grid.major = element_blank(), -# panel.background = element_blank(), -# plot.background = element_rect(fill="#e6e8ed"), -# panel.border = element_blank(), -# axis.line = element_blank(), -# axis.text.x = element_blank(), -# axis.text.y = element_blank(), -# axis.ticks = element_blank(), -# axis.title.x = element_blank(), -# axis.title.y = element_blank(), -# plot.title = element_text(size=22))) -# voters_map$color <- NULL -time_is <- Sys.time() -hour_is <- hour(time_is) -minute_is <- minute(time_is) -time_e <- ifelse(time_is>12, "p.m.", "a.m.") -time_e <- ifelse(time_is==0 | time_is==24, "a.m.", time_e) -hour_is <- ifelse(hour_is>12, hour_is-12, hour_is) -time_e <- ifelse(hour_is==0, "a.m.", time_e) -hour_is <- ifelse(hour_is==0, 12, hour_is) -the_time <- paste0(hour_is, ":", minute_is, " ", time_e) -# plot map -gg <- ggplot(voters_map, aes(long,lat, group=group)) -gg <- gg + geom_polygon(aes(fill=winner)) -gg <- gg + geom_path(color="white") -gg <- gg + scale_fill_manual(values= c("Results not ready"="gainsboro", "Clinton" = "lightskyblue", "Trump"="tomato", "Tie"="gray50")) -gg <- gg + labs(x=NULL, y=NULL, -title="Where candidates lead", -# title="Presidential results by town", -# subtitle="Where candidates are currently leading in votes. Results are unofficial.", -subtitle=paste0("Unofficial votes as of ", the_time), -caption="Source: Office of Secretary of the State") -gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) -gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) -gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) -gg <- gg + theme(axis.line = element_blank(), -axis.text = element_blank(), -axis.ticks = element_blank(), -panel.grid.major = element_blank(), -panel.grid.minor = element_blank(), -panel.border = element_blank(), -panel.background = element_blank(), -legend.title=element_blank()) -gg <- gg + coord_map("sinusoidal") -gg -ggsave(gg, file="output/pres_map.png", width=8, height=4, type="cairo-png") -date_time <- Sys.time() -date_time <- gsub(" EDT", "", date_time) -date_time <- gsub(" ", "", date_time) -date_time <- gsub("-", "", date_time) -date_time <- gsub(":", "", date_time) -ggsave(gg, file=paste0("output/archive/pres_map", date_time, ".png"), width=8, height=4, type="cairo-png") -# + theme_opts -#setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret) -#tweet("Boston Common", mediaPath = "map.png") -# Let's randomize -# -# pres_results$`Clinton and Kaine` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Trump and Pence` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Johnson and Weld` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Stein and Baraka` <- sample(1:100, 1, replace=TRUE) total_results <- filter(town_results, type=="votes") %>% filter(first=="Donald" | first=="Hillary" | first=="Jill" | name=="Johnson and Weld") %>% select(number, t.town_id., name) @@ -193,320 +117,396 @@ total_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_res total_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), "Clinton", total_results$winner) total_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), "Trump", total_results$winner) total_results$winner <- factor(total_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) -chart_results <- total_results %>% -select(id, `Clinton and Kaine`, `Trump and Pence`, `Johnson and Weld`, `Stein and Baraka`) %>% -summarise(Clinton=sum(`Clinton and Kaine`, na.rm=T), Trump=sum(`Trump and Pence`), Johnson=sum(`Johnson and Weld`, na.rm=T), Stein=sum(`Stein and Baraka`)) %>% -gather(candidate, votes, 1:4) -chart_results$candidate <- factor(chart_results$candidate, levels = chart_results$candidate[order(chart_results$votes)]) -ymid <- mean(range(chart_results$votes)) -gg <- ggplot(chart_results, aes(x=candidate, y=votes, color=NULL, fill=candidate)) -gg <- gg + geom_bar(stat="identity", position="dodge") -gg <- gg + geom_text(aes(x=candidate, y=votes, ymax=votes, family="Lato", label=prettyNum(votes, big.mark=","), -hjust=ifelse(votes < ymid, -0.1, 1.1), size = 3), -position = position_dodge(width=1)) -gg <- gg + coord_flip() -gg <- gg + scale_fill_manual(values= c("Stein"="lightgreen", "Johnson"="#ffec80", "Clinton" = "lightskyblue", "Trump"="tomato")) -gg <- gg + labs(x=NULL, y=NULL, -title=NULL) -gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) +colnames(total_results) <- c("Town", "Clinton", "Johnson16", "Stein", "Trump", "winner") +colnames(pres_results) <- c("Town", "Clinton", "Johnson16", "Stein", "Trump", "winner") +p2012 <- y2012 +p2012$Romney <- round(y2012$Romney/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Obama <- round(y2012$Obama/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Anderson <- round(y2012$Anderson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Johnson <- round(y2012$Johnson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +tot <- left_join(y2012, total_results) +per <- left_join(p2012, pres_results) +per$TCdiff <- per$Trump-per$Clinton +per$ROdiff <- per$Romney-per$Obama +per$COdiff <- per$Clinton-per$Obama +per$TRdiff <- per$Trump-per$Romney +# margins +library(knitr) +tot$vote_diff <- tot$Clinton-tot$Trump +tot <- arrange(tot, vote_diff) +tot <- subset(tot, (vote_diff<6) & (vote_diff > -6)) +tot <- subset(tot, winner!="Results not ready") +kable(tot) +# where Clinton lead the most +per_margins <- arrange(per, TCdiff) +per_margins <- subset(per_margins, winner!="Results not ready") +clinton_lead <- head(per_margins, 5) +kable(clinton_lead) +trump_lead <- tail(per_margins, 5) +kable(trump_lead) +## Obama versus Clinton +library(ggalt) +per$Trump[per$Trump == 0.00] <- NA +per$Clinton[per$Clinton == 0.00] <- NA +per <- subset(per, !is.na(per$Clinton)) +# start here +per <- arrange(per, Clinton) +per$Town <- factor(per$Town, levels=unique(per$Town)) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc) +gg <- ggplot() +gg <- gg + geom_dumbbell(data=per, aes(x=Clinton, xend=Obama, y=Town, group=Town), color="#a3c4dc", size=0.5, point.colour.l="#0e668b") +# gg <- gg + scale_x_continuous(label=percent) +gg <- gg + labs(x=NULL, y=NULL) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + theme_bw() +gg <- gg + theme(axis.title = element_text(family = "Lato Black", color="#666666", face="bold", size=6)) +gg <- gg + theme(plot.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.grid.minor=element_blank()) +gg <- gg + theme(panel.grid.major.y=element_blank()) +gg <- gg + theme(panel.grid.major.x=element_line()) +gg <- gg+ geom_vline(xintercept = 50) +gg <- gg+ geom_vline(xintercept = 20, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 40, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 60, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") +gg <- gg + theme(axis.ticks=element_blank()) +#gg <- gg + theme(axis.text = element_text(size = 4)) +gg <- gg + labs(title = "Support for Clinton and Obama", +subtitle= "Percent of votes in 2012 compared to 2016", +caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + annotate("text", x = 13.2, y = 150, label = "Barack Obama", size=3, colour="gray30") +gg <- gg + annotate("text", x = 12.8, y = 153, label = "Hillary Clinton", size=3, colour="gray30") +gg <- gg + annotate("point", x = 3, y = 150, colour = "#a3c4dc", size = 2) +gg <- gg + annotate("point", x = 3, y = 153, colour = "#0e668b", size = 2) +gg <- gg + theme(panel.border=element_blank()) gg <- gg + theme(axis.text.y = element_text(family="Lato Regular", size=12)) gg <- gg + theme(legend.position="none") gg <- gg + theme(axis.line = element_blank(), -axis.text.x = element_blank(), +axis.text.y = element_blank(), axis.ticks = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), legend.title=element_blank()) -gg -ggsave(gg, file="output/pres_chart.png", width=4.7, height=4, type="cairo-png") -ggsave(gg, file=paste0("output/archive/pres_chart", date_time, ".png"), width=4.7, height=4, type="cairo-png") -image_background <- image_read("background.png") -trend_logo <- image_read("trend_masthead.png") -pres_map <- image_read("output/pres_map.png") %>% -image_crop("1700x1200+360") -pres_chart <- image_read("output/pres_chart.png") -pres_map_edit <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path="output/pres_map.png", format="png") -pres_map_edit2 <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path=paste0("output/archive/pres_map", date_time, ".png"), format="png") -# FTP UPLOADING -source("keys.R") -ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/pres_map.png") -ftp_url_archive <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/archive/pres_map", date_time, ".png") -ftpUpload("output/pres_map.png", ftp_url) -ftpUpload(paste0("output/archive/pres_map", date_time, ".png"), ftp_url_archive) -time_is -hour_is -minue_is -minute_is -time_e -time_is <- Sys.time() -hour_is <- hour(time_is) -minute_is <- minute(time_is) -time_e <- ifelse(time_is>12, "p.m.", "a.m.") -time_e <- ifelse(time_is==0 | time_is==24, "a.m.", time_e) -hour_is <- ifelse(hour_is>12, hour_is-12, hour_is) -time_e <- ifelse(hour_is==0, "a.m.", time_e) -hour_is <- ifelse(hour_is==0, 12, hour_is) -time_e -time_is <- Sys.time() -time_e <- ifelse(time_is>12, "p.m.", "a.m.") -time_e -time_is -time_is>12 -time_e <- ifelse(hour_is>12, "p.m.", "a.m.") -time_e -time_e <- ifelse(time_is==0 | time_is==24, "a.m.", time_e) -hour_is -time_e -hour_is -the_time -time_e -the_time <- paste0(hour_is, ":", minute_is, " ", time_e) -the_time -library(jsonlite) -library(dplyr) -library(purrr) -library(tidyr) -library(scales) -library(ggplot2) -library(ggalt) -library(stringr) -library(rgdal) -require(maptools) -library(RCurl) -library(twitteR) -library(lubridate) -election_data_url <- "https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json" -lookup_url <- "https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json" -election_data <- fromJSON(election_data_url, simplifyVector=F) -#town_results <- data.frame((election_data[[6]])) -town_results <- data.frame((election_data[[7]])) -town_results <- data.frame(t(town_results)) -town_results$details <- rownames(town_results) -rownames(town_results) <- NULL -town_results$type <- "" -town_results$type <- ifelse(grepl(".TO", town_results$details), "percent_votes", "votes") -town_results$candidate <- town_results$details -town_results$candidate <- gsub(".TO", "", town_results$candidate) -town_results$candidate <- gsub(".V", "", town_results$candidate) -town_results$town <- gsub("\\..*", "", town_results$candidate) -town_results$town <- gsub("X", "", town_results$town) -town_results$candidate <- gsub(".*\\.", "", town_results$candidate) -town_results$details <- NULL -town_results$number <- town_results$t.town_results. -town_results$t.town_results. <- NULL -lookup_data <- fromJSON(lookup_url, simplifyVector=F) -town_id <- data.frame((lookup_data[[10]])) -town_id <- data.frame(t(town_id)) -town_id$town <- rownames(town_id) -rownames(town_id) <- NULL -town_id$town <- gsub("X", "", town_id$town) -town_results <- left_join(town_results, town_id) -candidate_id <- data.frame((lookup_data[[3]])) -candidate_id <- data.frame(t(candidate_id)) -candidate_id$details <- rownames(candidate_id) -candidate_id$cat <- "" -candidate_id$cat <- ifelse(grepl(".CO", candidate_id$details), "county", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".NM", candidate_id$details), "name", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".LN", candidate_id$details), "last", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".MN", candidate_id$details), "middle", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".P", candidate_id$details), "party", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".FN", candidate_id$details), "first", candidate_id$cat) -candidate_id$cat <- ifelse(grepl(".AD", candidate_id$details), "address", candidate_id$cat) -candidate_id$candidate <- gsub("\\..*", "", candidate_id$details) -candidate_id$candidate <- gsub("X", "", candidate_id$candidate) -candidate_id$details <- NULL -candidate_id$text <- candidate_id$t.candidate_id. -candidate_id$t.candidate_id. <- NULL -candidate_id <- spread(candidate_id, cat, text) -town_results <- left_join(town_results, candidate_id) -party_id <- data.frame((lookup_data[[7]])) -party_id <- data.frame(t(party_id)) -party_id$details <- rownames(party_id) -rownames(party_id) <- NULL -party_id$category <- "" -party_id$category <- ifelse(grepl(".P", party_id$details), "party_p", party_id$category) -party_id$category <- ifelse(grepl(".NM", party_id$details), "party_name", party_id$category) -party_id$category <- ifelse(grepl(".CD", party_id$details), "party_abbr", party_id$category) -party_id$party <- gsub("\\..*", "", party_id$details) -party_id$party <- gsub("X", "", party_id$party) -party_id$details <- NULL -party_id <- spread(party_id, category, t.party_id.) -town_results <- left_join(town_results, party_id) -pres_results <- filter(town_results, type=="percent_votes") %>% -filter(first=="Donald" | first=="Hillary" | first=="Jill" | name=="Johnson and Weld") %>% -select(number, t.town_id., name) -colnames(pres_results) <- c("percent", "id", "candidate") -pres_results$percent <- gsub("%", "", pres_results$percent) -pres_results$percent <- as.numeric(pres_results$percent ) -pres_results <- pres_results %>% -spread(candidate, percent) -pres_results$winner <- "" -pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` == pres_results$`Trump and Pence`), "Tie", pres_results$winner) -pres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_results$`Trump and Pence`==0), "Results not ready", pres_results$winner) -pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), "Clinton", pres_results$winner) -pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), "Trump", pres_results$winner) -pres_results$winner <- factor(pres_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) -town_shape <- readOGR(dsn="maps", layer="ctgeo") -town_shape_df <- fortify(town_shape, region="NAME10") -voters_map <- left_join(town_shape_df, pres_results) -voters_map$color <- "" -voters_map$color <- ifelse(is.na(voters_map$winner) | voters_map$winner=="Results not ready", "gray84", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="", "gray50", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="Trump", "tomato", voters_map$color) -voters_map$color <- ifelse(voters_map$winner=="Clinton", "lightskyblue", voters_map$color) -# theme_opts <- list(theme(panel.grid.minor = element_blank(), -# panel.grid.major = element_blank(), -# panel.background = element_blank(), -# plot.background = element_rect(fill="#e6e8ed"), -# panel.border = element_blank(), -# axis.line = element_blank(), -# axis.text.x = element_blank(), -# axis.text.y = element_blank(), -# axis.ticks = element_blank(), -# axis.title.x = element_blank(), -# axis.title.y = element_blank(), -# plot.title = element_text(size=22))) -# voters_map$color <- NULL -time_is <- Sys.time() -hour_is <- hour(time_is) -minute_is <- minute(time_is) -time_e <- ifelse(hour_is>12, "p.m.", "a.m.") -time_e <- ifelse(time_is==0 | time_is==24, "a.m.", time_e) -hour_is <- ifelse(hour_is>12, hour_is-12, hour_is) -time_e <- ifelse(hour_is==0, "a.m.", time_e) -hour_is <- ifelse(hour_is==0, 12, hour_is) -the_time <- paste0(hour_is, ":", minute_is, " ", time_e) -# plot map -gg <- ggplot(voters_map, aes(long,lat, group=group)) -gg <- gg + geom_polygon(aes(fill=winner)) -gg <- gg + geom_path(color="white") -gg <- gg + scale_fill_manual(values= c("Results not ready"="gainsboro", "Clinton" = "lightskyblue", "Trump"="tomato", "Tie"="gray50")) +gg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family="Lato", label=Town), size=2) +ggsave(gg, file="output/hillary_obama_1.png", width=6, height=12, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/hillary_obama_1.png") +ftpUpload("output/hillary_obama_1.png", ftp_url) +# OK, alphabetical +per <- arrange(per, desc(as.character(Town))) +per$Town <- factor(per$Town, levels=unique(per$Town)) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc) +gg <- ggplot(per, aes(x=Clinton, xend=Obama, y=Town, group=Town)) +gg <- gg + geom_dumbbell(color="#a3c4dc", size=0.5, point.colour.l="#0e668b") +# gg <- gg + scale_x_continuous(label=percent) +gg <- gg + labs(x=NULL, y=NULL) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + theme_bw() +gg <- gg + theme(axis.title = element_text(family = "Lato Black", color="#666666", face="bold", size=6)) +gg <- gg + theme(plot.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.grid.minor=element_blank()) +gg <- gg + theme(panel.grid.major.y=element_blank()) +gg <- gg + theme(panel.grid.major.x=element_line()) +gg <- gg+ geom_vline(xintercept = 50) +gg <- gg+ geom_vline(xintercept = 20, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 40, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 60, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") +gg <- gg + theme(axis.ticks=element_blank()) +#gg <- gg + theme(axis.text = element_text(size = 4)) +gg <- gg + labs(title = "Support for Clinton and Obama", +subtitle= "Percent of votes in 2012 compared to 2016", +caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + annotate("text", x = 13.2, y = 150, label = "Barack Obama", size=3, colour="gray30") +gg <- gg + annotate("text", x = 12.8, y = 153, label = "Hillary Clinton", size=3, colour="gray30") +gg <- gg + annotate("point", x = 3, y = 150, colour = "#a3c4dc", size = 2) +gg <- gg + annotate("point", x = 3, y = 153, colour = "#0e668b", size = 2) +gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(axis.text.y = element_text(family="Lato Regular", size=12)) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(axis.line = element_blank(), +axis.text.y = element_blank(), +axis.ticks = element_blank(), +panel.grid.major = element_blank(), +panel.grid.minor = element_blank(), +panel.border = element_blank(), +panel.background = element_blank(), +legend.title=element_blank()) +gg <- gg + geom_text(aes(x=arrange_hc, y=Town, family="Lato", label=Town), size=2) +ggsave(gg, file="output/hillary_obama_2.png", width=6, height=12, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/hillary_obama_2.png") +ftpUpload("output/hillary_obama_2.png", ftp_url) +# Trump versus Romney +# start here +per <- arrange(per, Trump) +per$Town <- factor(per$Town, levels=unique(per$Town)) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc) +gg <- ggplot() +gg <- gg + geom_dumbbell(data=per, aes(x=Trump, xend=Romney, y=Town, group=Town), color="tomato1", size=0.5, point.colour.l="red4") +# gg <- gg + scale_x_continuous(label=percent) +gg <- gg + labs(x=NULL, y=NULL) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + theme_bw() +gg <- gg + theme(axis.title = element_text(family = "Lato Black", color="#666666", face="bold", size=6)) +gg <- gg + theme(plot.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.grid.minor=element_blank()) +gg <- gg + theme(panel.grid.major.y=element_blank()) +gg <- gg + theme(panel.grid.major.x=element_line()) +gg <- gg+ geom_vline(xintercept = 50) +gg <- gg+ geom_vline(xintercept = 20, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 40, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 60, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") +gg <- gg + theme(axis.ticks=element_blank()) +#gg <- gg + theme(axis.text = element_text(size = 4)) +gg <- gg + labs(title = "Support for Trump and Romney", +subtitle= "Percent of votes in 2012 compared to 2016", +caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + annotate("text", x = 11.8, y = 150, label = "Mitt Romney", size=3, colour="gray30") +gg <- gg + annotate("text", x = 12.8, y = 153, label = "Donald Trump", size=3, colour="gray30") +gg <- gg + annotate("point", x = 3, y = 150, colour = "tomato1", size = 2) +gg <- gg + annotate("point", x = 3, y = 153, colour = "red4", size = 2) +gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(axis.text.y = element_text(family="Lato Regular", size=12)) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(axis.line = element_blank(), +axis.text.y = element_blank(), +axis.ticks = element_blank(), +panel.grid.major = element_blank(), +panel.grid.minor = element_blank(), +panel.border = element_blank(), +panel.background = element_blank(), +legend.title=element_blank()) +gg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family="Lato", label=Town), size=2) +ggsave(gg, file="output/trump_romney_1.png", width=6, height=12, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/trump_romney_1.png") +ftpUpload("output/trump_romney_1.png", ftp_url) +# OK, alphabetical +per <- arrange(per, desc(as.character(Town))) +per$Town <- factor(per$Town, levels=unique(per$Town)) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc) +gg <- ggplot(per, aes(x=Trump, xend=Romney, y=Town, group=Town)) +gg <- gg + geom_dumbbell(color="tomato1", size=0.5, point.colour.l="red4") +# gg <- gg + scale_x_continuous(label=percent) +gg <- gg + labs(x=NULL, y=NULL) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + theme_bw() +gg <- gg + theme(axis.title = element_text(family = "Lato Black", color="#666666", face="bold", size=6)) +gg <- gg + theme(plot.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.background=element_rect(fill="#f7f7f7")) +gg <- gg + theme(panel.grid.minor=element_blank()) +gg <- gg + theme(panel.grid.major.y=element_blank()) +gg <- gg + theme(panel.grid.major.x=element_line()) +gg <- gg+ geom_vline(xintercept = 50) +gg <- gg+ geom_vline(xintercept = 20, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 40, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 60, linetype="dotted", colour="lightgray") +gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") +gg <- gg + theme(axis.ticks=element_blank()) +#gg <- gg + theme(axis.text = element_text(size = 4)) +gg <- gg + labs(title = "Support for Trump and Romney", +subtitle= "Percent of votes in 2012 compared to 2016", +caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + annotate("text", x = 11.8, y = 150, label = "Mitt Romney", size=3, colour="gray30") +gg <- gg + annotate("text", x = 12.8, y = 153, label = "Donald Trump", size=3, colour="gray30") +gg <- gg + annotate("point", x = 3, y = 150, colour = "tomato1", size = 2) +gg <- gg + annotate("point", x = 3, y = 153, colour = "red4", size = 2) +gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(axis.text.y = element_text(family="Lato Regular", size=12)) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(axis.line = element_blank(), +axis.text.y = element_blank(), +axis.ticks = element_blank(), +panel.grid.major = element_blank(), +panel.grid.minor = element_blank(), +panel.border = element_blank(), +panel.background = element_blank(), +legend.title=element_blank()) +gg <- gg + geom_text(aes(x=arrange_hc, y=Town, family="Lato", label=Town), size=2) +ggsave(gg, file="output/trump_romney_2.png", width=6, height=12, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/trump_romney_2.png") +ftpUpload("output/trump_romney_2.png", ftp_url) +# By town type +urban <- read.csv("data/urban_rural.csv", stringsAsFactors=FALSE) +urban <- urban[c("NAME10", "Type")] +colnames(urban) <- c("Town", "Type") +per <- left_join(per, urban) +total_results <- left_join(total_results, urban) +town_type_analysis <-per %>% +select(Town, Type, Trump, Clinton) %>% +gather("candidate", "percent", 3:4) +gg <- ggplot(town_type_analysis, aes(x = Type, y = percent, fill = candidate)) +gg <- gg + geom_boxplot() +gg <- gg + scale_fill_manual(values= c("Clinton" = "lightskyblue", "Trump"="tomato")) gg <- gg + labs(x=NULL, y=NULL, -title="Where candidates lead", -# title="Presidential results by town", -# subtitle="Where candidates are currently leading in votes. Results are unofficial.", -subtitle=paste0("Unofficial votes as of ", the_time), -caption="Source: Office of Secretary of the State") +title="Presidential support by town type", +subtitle="Trump got the rural voters in Connecticut while voters in \nurban cities supported Clinton", +caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + theme(axis.text.x = element_text(family="Lato Regular", size=15)) gg <- gg + theme(axis.line = element_blank(), -axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), legend.title=element_blank()) -gg <- gg + coord_map("sinusoidal") gg -ggsave(gg, file="output/pres_map.png", width=8, height=4, type="cairo-png") -date_time <- Sys.time() -date_time <- gsub(" EDT", "", date_time) -date_time <- gsub(" ", "", date_time) -date_time <- gsub("-", "", date_time) -date_time <- gsub(":", "", date_time) -ggsave(gg, file=paste0("output/archive/pres_map", date_time, ".png"), width=8, height=4, type="cairo-png") -# + theme_opts -#setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret) -#tweet("Boston Common", mediaPath = "map.png") -# Let's randomize -# -# pres_results$`Clinton and Kaine` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Trump and Pence` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Johnson and Weld` <- sample(1:100, 1, replace=TRUE) -# pres_results$`Stein and Baraka` <- sample(1:100, 1, replace=TRUE) -total_results <- filter(town_results, type=="votes") %>% -filter(first=="Donald" | first=="Hillary" | first=="Jill" | name=="Johnson and Weld") %>% -select(number, t.town_id., name) -colnames(total_results) <- c("percent", "id", "candidate") -total_results$percent <- gsub("%", "", total_results$percent) -total_results$percent <- as.numeric(total_results$percent ) -total_results <- total_results %>% -spread(candidate, percent) -total_results$winner <- "" -total_results$winner <- ifelse((total_results$`Clinton and Kaine` == total_results$`Trump and Pence`), "Tie", total_results$winner) -total_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_results$`Trump and Pence`==0), "Results not ready", total_results$winner) -total_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), "Clinton", total_results$winner) -total_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), "Trump", total_results$winner) -total_results$winner <- factor(total_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) -chart_results <- total_results %>% -select(id, `Clinton and Kaine`, `Trump and Pence`, `Johnson and Weld`, `Stein and Baraka`) %>% -summarise(Clinton=sum(`Clinton and Kaine`, na.rm=T), Trump=sum(`Trump and Pence`), Johnson=sum(`Johnson and Weld`, na.rm=T), Stein=sum(`Stein and Baraka`)) %>% -gather(candidate, votes, 1:4) -chart_results$candidate <- factor(chart_results$candidate, levels = chart_results$candidate[order(chart_results$votes)]) -ymid <- mean(range(chart_results$votes)) -gg <- ggplot(chart_results, aes(x=candidate, y=votes, color=NULL, fill=candidate)) -gg <- gg + geom_bar(stat="identity", position="dodge") -gg <- gg + geom_text(aes(x=candidate, y=votes, ymax=votes, family="Lato", label=prettyNum(votes, big.mark=","), -hjust=ifelse(votes < ymid, -0.1, 1.1), size = 3), -position = position_dodge(width=1)) -gg <- gg + coord_flip() -gg <- gg + scale_fill_manual(values= c("Stein"="lightgreen", "Johnson"="#ffec80", "Clinton" = "lightskyblue", "Trump"="tomato")) -gg <- gg + labs(x=NULL, y=NULL, -title=NULL) +# By whiteness of town +race_towns <- getCensus(name="acs5", +vintage=2014, +key=census_key, +vars=c("NAME", "B02001_001E", "B02001_002E", "B02001_003E", +"B02001_004E", "B02001_005E", "B03001_003E"), +region="county subdivision:*", regionin="state:09") +colnames(race_towns) <- c("town", "state", "county", "countysub", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic") +race_towns <- race_towns[c("town", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic")] +race_towns <- subset(race_towns, !grepl("County subdivisions", town)) +race_towns$town <- gsub(" town.*", "", race_towns$town) +race_towns_long <- race_towns %>% +gather("race_ethnicity", "population", 3:7) %>% +mutate(percent_population=round(population/total_pop*100,2)) +race_towns_white <- race_towns_long %>% +filter(race_ethnicity=="White") %>% +select(town, percent_population) +colnames(race_towns_white) <- c("Town", "Percent_White") +per <- left_join(per, race_towns_white) +per2 <- per %>% +mutate(Minorities=100-Percent_White) %>% +select(id, Minorities, Clinton, Trump) %>% +gather("candidate", "percent", 3:4) +gg <- ggplot(per, aes(Percent_White, Trump)) +gg <- gg + geom_point() +gg <- gg + labs(x="Percent white population", y="Percent votes for Trump", +title="Trump votes compared to white population", +subtitle="The whiter a town, the higher likelihood they supported Donald Trump.", +caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) -gg <- gg + theme(axis.text.y = element_text(family="Lato Regular", size=12)) +gg <- gg + theme(axis.line = element_blank(), +axis.ticks = element_blank(), +panel.border = element_blank(), +panel.background = element_blank(), +legend.title=element_blank()) +gg <- gg + theme_bw() +gg +per2 <- per %>% +mutate(Minorities=100-Percent_White) %>% +select(id, Percent_White, Clinton, Trump) %>% +gather("candidate", "percent", 3:4) +#%>% +# filter(percent > 50) +# Swapped? +gg <- ggplot() +gg <- gg + geom_point(data=per2, aes(Percent_White, percent, color=candidate)) +gg <- gg + scale_color_manual(values= c("lightskyblue", "tomato")) +gg <- gg + facet_wrap(~candidate) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + scale_y_continuous(limits = c(0, 110)) +gg <- gg + labs(x="Percent white population", y="Percent of votes", +title="Votes compared to white population", +subtitle="The larger the white population, the higher the likelihood they supported Donald Trump.", +caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme_bw(base_family="Lato Regular") +gg <- gg + theme(axis.ticks.y=element_blank()) +#gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(legend.key=element_blank()) +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=22)) +gg <- gg + theme(plot.caption=element_text(face="bold", family="Lato Regular", size=9, color="gray28", margin=margin(t=10, r=80))) gg <- gg + theme(legend.position="none") +gg <- gg + theme(strip.background=element_blank()) +gg <- gg + theme(strip.text.x = element_text(size = 10, colour = "grey5")) +gg +ggsave(gg, file="output/white_population.png", width=8, height=5, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/white_population.png") +ftpUpload("output/white_population.png", ftp_url) +## Income? +race_towns <- getCensus(name="acs5", +vintage=2014, +key=census_key, +vars=c("NAME", "B02001_001E", "B02001_002E", "B02001_003E", +"B02001_004E", "B02001_005E", "B03001_003E"), +region="county subdivision:*", regionin="state:09") +colnames(race_towns) <- c("town", "state", "county", "countysub", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic") +race_towns <- race_towns[c("town", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic")] +race_towns <- subset(race_towns, !grepl("County subdivisions", town)) +race_towns$town <- gsub(" town.*", "", race_towns$town) +race_towns_long <- race_towns %>% +gather("race_ethnicity", "population", 3:7) %>% +mutate(percent_population=round(population/total_pop*100,2)) +race_towns_white <- race_towns_long %>% +filter(race_ethnicity=="White") %>% +select(town, percent_population) +colnames(race_towns_white) <- c("Town", "Percent_White") +per <- left_join(per, race_towns_white) +per2 <- per %>% +mutate(Minorities=100-Percent_White) %>% +select(id, Minorities, Clinton, Trump) %>% +gather("candidate", "percent", 3:4) +``` +## Flip map +per$winner2012 <- ifelse(per$Obama>per$Romney, "Obama", "Romney") +per$flip <- "" +per$flip <- ifelse(per$winner=="Clinton" & per$winner2012=="Obama", "Clinton retained", per$flip) +per$flip <- ifelse(per$winner=="Trump" & per$winner2012=="Romney", "Trump retained", per$flip) +per$flip <- ifelse(per$winner=="Trump" & per$winner2012=="Obama", "Flipped to Trump", per$flip) +per$flip <- ifelse(per$winner=="Clinton" & per$winner2012=="Romney", "Flipped to Clinton", per$flip) +town_shape <- readOGR(dsn="maps", layer="ctgeo") +town_shape_df <- fortify(town_shape, region="NAME10") +names(per)[names(per) == 'Town'] <- 'id' +voters_map <- left_join(town_shape_df,per) +gg <- ggplot(voters_map, aes(long,lat, group=group)) +gg <- gg + geom_polygon(aes(fill=flip)) +gg <- gg + geom_path(color="white") +gg <- gg + scale_fill_manual(values= c("Clinton retained"="lightskyblue", "Flipped to Clinton" = "navyblue", "Trump retained"="tomato", "Flipped to Trump"="red4")) +gg <- gg + labs(x=NULL, y=NULL, +title="Where towns chose or flipped for Clinton or Trump", +# title="Presidential results by town", +# subtitle="Where candidates are currently leading in votes. Results are unofficial.", +subtitle="Compared to whether Obama or Romney won in 2012", +caption="Source: Office of Secretary of the State \nAndrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) gg <- gg + theme(axis.line = element_blank(), -axis.text.x = element_blank(), +axis.text = element_blank(), axis.ticks = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), panel.border = element_blank(), panel.background = element_blank(), legend.title=element_blank()) +gg <- gg + coord_map("sinusoidal") gg -ggsave(gg, file="output/pres_chart.png", width=4.7, height=4, type="cairo-png") -ggsave(gg, file=paste0("output/archive/pres_chart", date_time, ".png"), width=4.7, height=4, type="cairo-png") -image_background <- image_read("background.png") -trend_logo <- image_read("trend_masthead.png") -pres_map <- image_read("output/pres_map.png") %>% -image_crop("1700x1200+360") -pres_chart <- image_read("output/pres_chart.png") -pres_map_edit <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path="output/pres_map.png", format="png") -pres_map_edit2 <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path=paste0("output/archive/pres_map", date_time, ".png"), format="png") -library(magick) -image_background <- image_read("background.png") -trend_logo <- image_read("trend_masthead.png") -pres_map <- image_read("output/pres_map.png") %>% -image_crop("1700x1200+360") -pres_chart <- image_read("output/pres_chart.png") -pres_map_edit <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path="output/pres_map.png", format="png") -pres_map_edit2 <- image_read("background.png") %>% -image_composite(pres_map) %>% -image_composite(image_scale(pres_chart, "x1000"), offset="+1225+180") %>% -image_scale("x700") %>% -image_write(path=paste0("output/archive/pres_map", date_time, ".png"), format="png") -# FTP UPLOADING -source("keys.R") -ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/pres_map.png") -ftp_url_archive <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/archive/pres_map", date_time, ".png") -ftpUpload("output/pres_map.png", ftp_url) -ftpUpload(paste0("output/archive/pres_map", date_time, ".png"), ftp_url_archive) +#ggsave(gg, file="output/pres_map.png", width=8, height=4, type="cairo-png") diff --git a/.Rproj.user/6400D2F9/pcs/source-pane.pper b/.Rproj.user/6400D2F9/pcs/source-pane.pper index 70829f6..f53e558 100644 --- a/.Rproj.user/6400D2F9/pcs/source-pane.pper +++ b/.Rproj.user/6400D2F9/pcs/source-pane.pper @@ -1,3 +1,3 @@ { - "activeTab" : 1 + "activeTab" : 4 } \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/pcs/windowlayoutstate.pper b/.Rproj.user/6400D2F9/pcs/windowlayoutstate.pper index 13fb420..acf0f81 100644 --- a/.Rproj.user/6400D2F9/pcs/windowlayoutstate.pper +++ b/.Rproj.user/6400D2F9/pcs/windowlayoutstate.pper @@ -1,13 +1,13 @@ { "left" : { "panelheight" : 762, - "splitterpos" : 319, + "splitterpos" : 318, "topwindowstate" : "NORMAL", "windowheight" : 800 }, "right" : { "panelheight" : 762, - "splitterpos" : 478, + "splitterpos" : 477, "topwindowstate" : "NORMAL", "windowheight" : 800 } diff --git a/.Rproj.user/6400D2F9/rmd-outputs b/.Rproj.user/6400D2F9/rmd-outputs index 3f2ff2d..c030220 100644 --- a/.Rproj.user/6400D2F9/rmd-outputs +++ b/.Rproj.user/6400D2F9/rmd-outputs @@ -1,3 +1,8 @@ +~/Documents/Github/trendct-data/2016/11/election-2016-r/index.html +~/Documents/Github/trendct-data/2016/11/election-2016-r/index.html +~/Documents/Github/trendct-data/2016/11/election-2016-r/index.html +~/Documents/Github/trendct-data/2016/11/election-2016-r/index.html +~/Documents/Github/trendct-data/2016/11/election-2016-r/index.html diff --git a/.Rproj.user/6400D2F9/sdb/prop/INDEX b/.Rproj.user/6400D2F9/sdb/prop/INDEX index c895d1d..3573914 100644 --- a/.Rproj.user/6400D2F9/sdb/prop/INDEX +++ b/.Rproj.user/6400D2F9/sdb/prop/INDEX @@ -1,5 +1,6 @@ ~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F10%2Fdrought-2016%2Fmaps_maker.R="9EA8005" ~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F11%2Felection-2016-r%2F.gitignore="BF6DEB15" +~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F11%2Felection-2016-r%2Fanalysis2.Rmd="49233A53" ~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F11%2Felection-2016-r%2Fanalysis2016.R="1101D8BF" ~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F11%2Felection-2016-r%2Findex.Rmd="611A7C9" ~%2FDocuments%2FGithub%2Ftrendct-data%2F2016%2F11%2Felection-2016-r%2Fkeys.R="88F0A28" diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/155930B5 b/.Rproj.user/6400D2F9/sdb/s-B5824203/155930B5 deleted file mode 100644 index 330460a..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/155930B5 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478675261692.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "155930B5", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478675261692, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "83B32485", - "caption" : "electoral", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=electoral&cache_key=83B32485", - "displayedObservations" : 2, - "environment" : "", - "object" : "electoral", - "preview" : 0, - "totalObservations" : 2, - "variables" : 2 - }, - "relative_order" : 18, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/1A6A5222 b/.Rproj.user/6400D2F9/sdb/s-B5824203/1A6A5222 deleted file mode 100644 index 4475f27..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/1A6A5222 +++ /dev/null @@ -1,21 +0,0 @@ -{ - "collab_server" : "", - "contents" : "library(jsonlite)\nlibrary(dplyr)\nlibrary(purrr)\nlibrary(tidyr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(ggalt)\nlibrary(stringr)\nlibrary(rgdal)\nrequire(maptools)\nlibrary(RCurl)\nlibrary(twitteR)\nelection_data_url <- \"https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json\"\n\nlookup_url <- \"https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json\"\n\nlookup_data <- fromJSON(lookup_url, simplifyVector=F)\npolling_places <- data.frame((lookup_data[[4]]))\npolling_places <- data.frame(t(polling_places))\npolling_places$poll_id <- rownames(polling_places)\nrownames(polling_places) <- NULL\npolling_places$poll_id <- gsub(\"X\", \"\", polling_places$poll_id)\ncolnames(polling_places) <- c(\"polling_place\", \"poll_id\")\npolling_places$town <- gsub(\" -.*\", \"\", polling_places$polling_place)\npolling_places$town <- gsub(\"-.*\", \"\", polling_places$town)\npolling_places$poll_spot <- gsub(\" -.*\", \"\", polling_places$polling_place)\npolling_places$poll_spot <- gsub(\".*-\", \"\", polling_places$poll_spot)\npolling_places$town_id <- gsub(\"\\\\-.*\", \"\", polling_places$polling_place)\npolling_places$town_id <- gsub(\"-.*\", \"\", polling_places$town_id)\npolling_places$dashes <- length(grep(\"\\\\-\", polling_places$polling_place))\n\nlibrary(reshape)\n\n\nlookup_data <- fromJSON(lookup_url, simplifyVector=F)\npolling_places <- data.frame((lookup_data[[4]]))\npolling_places <- data.frame(t(polling_places))\npolling_places$poll_id <- rownames(polling_places)\nrownames(polling_places) <- NULL\npolling_places$poll_id <- gsub(\"X\", \"\", polling_places$poll_id)\ncolnames(polling_places) <- c(\"polling_place\", \"poll_id\")\npolling_places3 <- polling_places[c(\"polling_place\")]\npolling_places2 <- transform(polling_places3, polling_place = colsplit(polling_place, split=\"\\\\-\", names=c(\"a\", \"b\", \"c\", \"d\")))\nlibrary(tidyr)\n\ntest <- separate(data=polling_places3, col=polling_place, into=(\"a\", \"b\", \"c\", \"d\"))\n", - "created" : 1478612942434.000, - "dirty" : true, - "encoding" : "", - "folds" : "", - "hash" : "2197847848", - "id" : "1A6A5222", - "lastKnownWriteTime" : 6, - "last_content_update" : 1478648426063, - "path" : null, - "project_path" : null, - "properties" : { - "tempName" : "Untitled3" - }, - "relative_order" : 10, - "source_on_save" : false, - "source_window" : "", - "type" : "r_source" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/3134A2D9 b/.Rproj.user/6400D2F9/sdb/s-B5824203/3134A2D9 deleted file mode 100644 index ded2418..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/3134A2D9 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478710072896.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "3134A2D9", - "lastKnownWriteTime" : 4294967296, - "last_content_update" : 1478710072896, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "75607C18", - "caption" : "race_towns", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=race_towns&cache_key=75607C18", - "displayedObservations" : 169, - "environment" : "", - "object" : "race_towns", - "preview" : 0, - "totalObservations" : 169, - "variables" : 7 - }, - "relative_order" : 20, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/316DD4A5 b/.Rproj.user/6400D2F9/sdb/s-B5824203/316DD4A5 deleted file mode 100644 index b6b93b2..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/316DD4A5 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478663236487.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "316DD4A5", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478663236487, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "BE2D09E7", - "caption" : "total_results", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=total_results&cache_key=BE2D09E7", - "displayedObservations" : "169", - "environment" : "", - "object" : "total_results", - "preview" : "0", - "totalObservations" : "169", - "variables" : "6" - }, - "relative_order" : 9, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/3AA9658 b/.Rproj.user/6400D2F9/sdb/s-B5824203/3AA9658 deleted file mode 100644 index 69b733d..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/3AA9658 +++ /dev/null @@ -1,22 +0,0 @@ -{ - "collab_server" : "", - "contents" : "---\ntitle: \"2016 CT election analysis\"\nauthor: \"Andrew Ba Tran\"\ndate: \"November 9, 2016\"\noutput: html_document\n---\n\n```{r setup, echo=FALSE, warning=F, message=F, fig.width=8, fig.height=12}\n\ny2012 <- read.csv(\"data/2012_election_results.csv\", stringsAsFactors=F)\ny2012 <- y2012[1:169,]\n\nsource(\"keys.R\")\nlibrary(censusapi)\nlibrary(jsonlite)\nlibrary(dplyr)\nlibrary(purrr)\nlibrary(tidyr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(ggalt)\nlibrary(stringr)\nlibrary(rgdal)\nrequire(maptools)\nlibrary(RCurl)\nlibrary(twitteR)\nlibrary(lubridate)\nelection_data_url <- \"https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json\"\n\nlookup_url <- \"https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json\"\n\n\nelection_data <- fromJSON(election_data_url, simplifyVector=F)\n#town_results <- data.frame((election_data[[6]]))\ntown_results <- data.frame((election_data[[7]]))\ntown_results <- data.frame(t(town_results))\ntown_results$details <- rownames(town_results) \nrownames(town_results) <- NULL\ntown_results$type <- \"\"\n\ntown_results$type <- ifelse(grepl(\".TO\", town_results$details), \"percent_votes\", \"votes\")\ntown_results$candidate <- town_results$details\ntown_results$candidate <- gsub(\".TO\", \"\", town_results$candidate)\ntown_results$candidate <- gsub(\".V\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"\\\\..*\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"X\", \"\", town_results$town)\ntown_results$candidate <- gsub(\".*\\\\.\", \"\", town_results$candidate)\ntown_results$details <- NULL\ntown_results$number <- town_results$t.town_results.\ntown_results$t.town_results. <- NULL\n\nlookup_data <- fromJSON(lookup_url, simplifyVector=F)\ntown_id <- data.frame((lookup_data[[10]]))\ntown_id <- data.frame(t(town_id))\ntown_id$town <- rownames(town_id)\nrownames(town_id) <- NULL\ntown_id$town <- gsub(\"X\", \"\", town_id$town)\n\ntown_results <- left_join(town_results, town_id)\n\ncandidate_id <- data.frame((lookup_data[[3]]))\ncandidate_id <- data.frame(t(candidate_id))\ncandidate_id$details <- rownames(candidate_id)\ncandidate_id$cat <- \"\"\ncandidate_id$cat <- ifelse(grepl(\".CO\", candidate_id$details), \"county\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".NM\", candidate_id$details), \"name\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".LN\", candidate_id$details), \"last\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".MN\", candidate_id$details), \"middle\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".P\", candidate_id$details), \"party\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".FN\", candidate_id$details), \"first\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".AD\", candidate_id$details), \"address\", candidate_id$cat)\ncandidate_id$candidate <- gsub(\"\\\\..*\", \"\", candidate_id$details)\ncandidate_id$candidate <- gsub(\"X\", \"\", candidate_id$candidate)\ncandidate_id$details <- NULL\ncandidate_id$text <- candidate_id$t.candidate_id.\ncandidate_id$t.candidate_id. <- NULL\ncandidate_id <- spread(candidate_id, cat, text)\n\ntown_results <- left_join(town_results, candidate_id)\n\n\nparty_id <- data.frame((lookup_data[[7]]))\nparty_id <- data.frame(t(party_id))\nparty_id$details <- rownames(party_id)\nrownames(party_id) <- NULL\nparty_id$category <- \"\"\nparty_id$category <- ifelse(grepl(\".P\", party_id$details), \"party_p\", party_id$category)\nparty_id$category <- ifelse(grepl(\".NM\", party_id$details), \"party_name\", party_id$category)\nparty_id$category <- ifelse(grepl(\".CD\", party_id$details), \"party_abbr\", party_id$category)\nparty_id$party <- gsub(\"\\\\..*\", \"\", party_id$details)\nparty_id$party <- gsub(\"X\", \"\", party_id$party)\n\nparty_id$details <- NULL\nparty_id <- spread(party_id, category, t.party_id.)\n\ntown_results <- left_join(town_results, party_id)\n\n\n\npres_results <- filter(town_results, type==\"percent_votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(pres_results) <- c(\"percent\", \"id\", \"candidate\")\npres_results$percent <- gsub(\"%\", \"\", pres_results$percent)\npres_results$percent <- as.numeric(pres_results$percent )\npres_results <- pres_results %>%\n spread(candidate, percent)\n\n\npres_results$winner <- \"\"\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` == pres_results$`Trump and Pence`), \"Tie\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_results$`Trump and Pence`==0), \"Results not ready\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), \"Clinton\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), \"Trump\", pres_results$winner)\npres_results$winner <- factor(pres_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\ntotal_results <- filter(town_results, type==\"votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(total_results) <- c(\"percent\", \"id\", \"candidate\")\ntotal_results$percent <- gsub(\"%\", \"\", total_results$percent)\ntotal_results$percent <- as.numeric(total_results$percent )\ntotal_results <- total_results %>%\n spread(candidate, percent)\n\ntotal_results$winner <- \"\"\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` == total_results$`Trump and Pence`), \"Tie\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_results$`Trump and Pence`==0), \"Results not ready\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), \"Clinton\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), \"Trump\", total_results$winner)\ntotal_results$winner <- factor(total_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\ncolnames(total_results) <- c(\"Town\", \"Clinton\", \"Johnson16\", \"Stein\", \"Trump\", \"winner\")\ncolnames(pres_results) <- c(\"Town\", \"Clinton\", \"Johnson16\", \"Stein\", \"Trump\", \"winner\")\n\np2012 <- y2012\np2012$Romney <- round(y2012$Romney/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Obama <- round(y2012$Obama/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Anderson <- round(y2012$Anderson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Johnson <- round(y2012$Johnson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\n\n\ntot <- left_join(y2012, total_results)\nper <- left_join(p2012, pres_results)\n\nper$TCdiff <- per$Trump-per$Clinton\nper$ROdiff <- per$Romney-per$Obama\n\nper$COdiff <- per$Clinton-per$Obama\nper$TRdiff <- per$Trump-per$Romney\n\n# margins\nlibrary(knitr)\n\ntot$vote_diff <- tot$Clinton-tot$Trump\ntot <- arrange(tot, vote_diff)\ntot <- subset(tot, (vote_diff<6) & (vote_diff > -6))\ntot <- subset(tot, winner!=\"Results not ready\")\n```\n\n# Towns with the smallest margins between Trump and Clinton\n```{r table1, echo=F}\nkable(tot)\n```\n\n# Where Clinton lead the most in percent votes\n\n```{r table2, echo=F}\nper_margins <- arrange(per, TCdiff)\nper_margins <- subset(per_margins, winner!=\"Results not ready\")\n\nclinton_lead <- head(per_margins, 5)\nkable(clinton_lead)\n```\n\n# Where trump lead the most in percent votes\n\n```{r table3, echo=F}\ntrump_lead <- tail(per_margins, 5)\nkable(trump_lead)\n```\n\n```{r setup2, echo=FALSE, warning=F, message=F, fig.width=8, fig.height=12}\nlibrary(ggalt)\n\nper$Trump[per$Trump == 0.00] <- NA\nper$Clinton[per$Clinton == 0.00] <- NA\n\nper <- subset(per, !is.na(per$Clinton))\n\n# start here\n\nper <- arrange(per, Clinton)\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0)\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc)\n\ngg <- ggplot()\ngg <- gg + geom_dumbbell(data=per, aes(x=Clinton, xend=Obama, y=Town, group=Town), color=\"#a3c4dc\", size=0.5, point.colour.l=\"#0e668b\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Clinton and Obama\",\n subtitle= \"Percent of votes in 2012 compared to 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + annotate(\"text\", x = 13.2, y = 150, label = \"Barack Obama\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Hillary Clinton\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"#a3c4dc\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"#0e668b\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\nprint(gg)\n\n# OK, alphabetical\n\nper <- arrange(per, desc(as.character(Town)))\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0)\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc)\n\n\ngg <- ggplot(per, aes(x=Clinton, xend=Obama, y=Town, group=Town))\ngg <- gg + geom_dumbbell(color=\"#a3c4dc\", size=0.5, point.colour.l=\"#0e668b\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Clinton and Obama\",\n subtitle= \"Percent of votes in 2012 compared 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\n\ngg <- gg + annotate(\"text\", x = 13.2, y = 150, label = \"Barack Obama\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Hillary Clinton\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"#a3c4dc\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"#0e668b\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nprint(gg)\n\n# Trump versus Romney\n\n# start here\n\nper <- arrange(per, Trump)\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0)\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc)\n\ngg <- ggplot()\ngg <- gg + geom_dumbbell(data=per, aes(x=Trump, xend=Romney, y=Town, group=Town), color=\"tomato1\", size=0.5, point.colour.l=\"red4\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Trump and Romney\",\n subtitle= \"Percent of votes in 2012 compared to 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + annotate(\"text\", x = 11.8, y = 150, label = \"Mitt Romney\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Donald Trump\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"tomato1\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"red4\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nprint(gg)\n\n# OK, alphabetical\n\nper <- arrange(per, desc(as.character(Town)))\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0)\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc)\n\n\ngg <- ggplot(per, aes(x=Trump, xend=Romney, y=Town, group=Town))\ngg <- gg + geom_dumbbell(color=\"tomato1\", size=0.5, point.colour.l=\"red4\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Trump and Romney\",\n subtitle= \"Percent of votes in 2012 compared 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\n\ngg <- gg + annotate(\"text\", x = 11.8, y = 150, label = \"Mitt Romney\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Donald Trump\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"tomato1\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"red4\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nprint(gg)\n\n```\n\n```{r part2, echo=FALSE, warning=F, message=F, fig.height=5, fig.width=8}\n\n# By town type\n\nurban <- read.csv(\"data/urban_rural.csv\", stringsAsFactors=FALSE)\nurban <- urban[c(\"NAME10\", \"Type\")]\ncolnames(urban) <- c(\"Town\", \"Type\")\n\nper <- left_join(per, urban)\ntotal_results <- left_join(total_results, urban)\n\ntown_type_analysis <-per %>%\n select(Town, Type, Trump, Clinton) %>%\n gather(\"candidate\", \"percent\", 3:4)\n\ngg <- ggplot(town_type_analysis, aes(x = Type, y = percent, fill = candidate)) \ngg <- gg + geom_boxplot() \ngg <- gg + scale_fill_manual(values= c(\"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Presidential support by town type\",\n subtitle=\"Trump got the rural voters in Connecticut while voters in \\nurban cities supported Clinton\",\n caption=\"Source: Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.text.x = element_text(family=\"Lato Regular\", size=15))\ngg <- gg + theme(axis.line = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg\n\n```\n\n# By whiteness of town\n\n```{r part3, echo=FALSE, warning=F, message=F, fig.height=7, fig.width=7}\n\nrace_towns <- getCensus(name=\"acs5\",\n vintage=2014,\n key=census_key,\n vars=c(\"NAME\", \"B02001_001E\", \"B02001_002E\", \"B02001_003E\",\n \"B02001_004E\", \"B02001_005E\", \"B03001_003E\"),\n region=\"county subdivision:*\", regionin=\"state:09\")\n\ncolnames(race_towns) <- c(\"town\", \"state\", \"county\", \"countysub\", \"total_pop\", \"White\", \"Black\", \"Indian\", \"Asian\", \"Hispanic\")\nrace_towns <- race_towns[c(\"town\", \"total_pop\", \"White\", \"Black\", \"Indian\", \"Asian\", \"Hispanic\")]\nrace_towns <- subset(race_towns, !grepl(\"County subdivisions\", town))\nrace_towns$town <- gsub(\" town.*\", \"\", race_towns$town)\n\nrace_towns_long <- race_towns %>%\n gather(\"race_ethnicity\", \"population\", 3:7) %>%\n mutate(percent_population=round(population/total_pop*100,2))\n\nrace_towns_white <- race_towns_long %>%\n filter(race_ethnicity==\"White\") %>%\n select(town, percent_population)\n\ncolnames(race_towns_white) <- c(\"Town\", \"Percent_White\")\n\nper <- left_join(per, race_towns_white)\n\ngg <- ggplot(per, aes(Percent_White, Trump))\ngg <- gg + geom_point()\ngg <- gg + labs(x=\"Percent white population\", y=\"Percent votes for Trump\", \n title=\"Trump votes compared to white population\",\n subtitle=\"The whiter a town, the higher likelihood they supported Donald Trump.\",\n caption=\"Source: Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.ticks = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + theme_bw()\ngg\n\n\n# Swapped?\n\nper$winner2012 <- ifelse(per$Obama>per$Romney, \"Obama\", \"Romney\")\nper$flip <- \"\"\nper$flip <- ifelse(per$winner==\"Clinton\" & per$winner2012==\"Obama\", \"Clinton retained\", per$flip)\nper$flip <- ifelse(per$winner==\"Trump\" & per$winner2012==\"Romney\", \"Trump retained\", per$flip)\nper$flip <- ifelse(per$winner==\"Trump\" & per$winner2012==\"Obama\", \"Flipped to Trump\", per$flip)\nper$flip <- ifelse(per$winner==\"Clinton\" & per$winner2012==\"Romney\", \"Flipped to Clinton\", per$flip)\n\ntown_shape <- readOGR(dsn=\"maps\", layer=\"ctgeo\")\ntown_shape_df <- fortify(town_shape, region=\"NAME10\")\n\nnames(per)[names(per) == 'Town'] <- 'id'\n\n\nvoters_map <- left_join(town_shape_df,per)\n```\n\n```{r part4, echo=FALSE, warning=F, message=F, fig.height=6, fig.width=9}\n\ngg <- ggplot(voters_map, aes(long,lat, group=group))\ngg <- gg + geom_polygon(aes(fill=flip))\ngg <- gg + geom_path(color=\"white\")\ngg <- gg + scale_fill_manual(values= c(\"Clinton retained\"=\"lightskyblue\", \"Flipped to Clinton\" = \"navyblue\", \"Trump retained\"=\"tomato\", \"Flipped to Trump\"=\"red4\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Where towns chose or flipped for Clinton or Trump\",\n # title=\"Presidential results by town\",\n # subtitle=\"Where candidates are currently leading in votes. Results are unofficial.\",\n subtitle=\"Compared to whether Obama or Romney won in 2012\",\n caption=\"Source: Office of Secretary of the State \\nAndrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.text = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + coord_map(\"sinusoidal\") \n\ngg\n\n#ggsave(gg, file=\"output/pres_map.png\", width=8, height=4, type=\"cairo-png\")\n```\n", - "created" : 1478711469949.000, - "dirty" : false, - "encoding" : "UTF-8", - "folds" : "", - "hash" : "1076422648", - "id" : "3AA9658", - "lastKnownWriteTime" : 1478712242, - "last_content_update" : 1478712242061, - "path" : "~/Documents/Github/trendct-data/2016/11/election-2016-r/index.Rmd", - "project_path" : "index.Rmd", - "properties" : { - "last_setup_crc32" : "A66AF4738bb5b9c6", - "tempName" : "Untitled4" - }, - "relative_order" : 19, - "source_on_save" : false, - "source_window" : "", - "type" : "r_markdown" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/54C2F207 b/.Rproj.user/6400D2F9/sdb/s-B5824203/54C2F207 deleted file mode 100644 index 765fe2d..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/54C2F207 +++ /dev/null @@ -1,23 +0,0 @@ -{ - "collab_server" : "", - "contents" : "library(jsonlite)\nlibrary(dplyr)\nlibrary(purrr)\nlibrary(tidyr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(ggalt)\nlibrary(stringr)\nlibrary(rgdal)\nrequire(maptools)\nlibrary(RCurl)\nlibrary(twitteR)\nlibrary(lubridate)\nlibrary(magick)\nelection_data_url <- \"https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json\"\n\nlookup_url <- \"https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json\"\n\n\nelection_data <- fromJSON(election_data_url, simplifyVector=F)\n#town_results <- data.frame((election_data[[6]]))\ntown_results <- data.frame((election_data[[7]]))\ntown_results <- data.frame(t(town_results))\ntown_results$details <- rownames(town_results) \nrownames(town_results) <- NULL\ntown_results$type <- \"\"\n\ntown_results$type <- ifelse(grepl(\".TO\", town_results$details), \"percent_votes\", \"votes\")\ntown_results$candidate <- town_results$details\ntown_results$candidate <- gsub(\".TO\", \"\", town_results$candidate)\ntown_results$candidate <- gsub(\".V\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"\\\\..*\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"X\", \"\", town_results$town)\ntown_results$candidate <- gsub(\".*\\\\.\", \"\", town_results$candidate)\ntown_results$details <- NULL\ntown_results$number <- town_results$t.town_results.\ntown_results$t.town_results. <- NULL\n\nlookup_data <- fromJSON(lookup_url, simplifyVector=F)\ntown_id <- data.frame((lookup_data[[10]]))\ntown_id <- data.frame(t(town_id))\ntown_id$town <- rownames(town_id)\nrownames(town_id) <- NULL\ntown_id$town <- gsub(\"X\", \"\", town_id$town)\n\ntown_results <- left_join(town_results, town_id)\n\ncandidate_id <- data.frame((lookup_data[[3]]))\ncandidate_id <- data.frame(t(candidate_id))\ncandidate_id$details <- rownames(candidate_id)\ncandidate_id$cat <- \"\"\ncandidate_id$cat <- ifelse(grepl(\".CO\", candidate_id$details), \"county\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".NM\", candidate_id$details), \"name\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".LN\", candidate_id$details), \"last\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".MN\", candidate_id$details), \"middle\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".P\", candidate_id$details), \"party\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".FN\", candidate_id$details), \"first\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".AD\", candidate_id$details), \"address\", candidate_id$cat)\ncandidate_id$candidate <- gsub(\"\\\\..*\", \"\", candidate_id$details)\ncandidate_id$candidate <- gsub(\"X\", \"\", candidate_id$candidate)\ncandidate_id$details <- NULL\ncandidate_id$text <- candidate_id$t.candidate_id.\ncandidate_id$t.candidate_id. <- NULL\ncandidate_id <- spread(candidate_id, cat, text)\n\ntown_results <- left_join(town_results, candidate_id)\n\n\nparty_id <- data.frame((lookup_data[[7]]))\nparty_id <- data.frame(t(party_id))\nparty_id$details <- rownames(party_id)\nrownames(party_id) <- NULL\nparty_id$category <- \"\"\nparty_id$category <- ifelse(grepl(\".P\", party_id$details), \"party_p\", party_id$category)\nparty_id$category <- ifelse(grepl(\".NM\", party_id$details), \"party_name\", party_id$category)\nparty_id$category <- ifelse(grepl(\".CD\", party_id$details), \"party_abbr\", party_id$category)\nparty_id$party <- gsub(\"\\\\..*\", \"\", party_id$details)\nparty_id$party <- gsub(\"X\", \"\", party_id$party)\n\nparty_id$details <- NULL\nparty_id <- spread(party_id, category, t.party_id.)\n\ntown_results <- left_join(town_results, party_id)\n\n\n\npres_results <- filter(town_results, type==\"percent_votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(pres_results) <- c(\"percent\", \"id\", \"candidate\")\npres_results$percent <- gsub(\"%\", \"\", pres_results$percent)\npres_results$percent <- as.numeric(pres_results$percent )\npres_results <- pres_results %>%\n spread(candidate, percent)\n\npres_results$winner <- \"\"\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` == pres_results$`Trump and Pence`), \"Tie\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_results$`Trump and Pence`==0), \"Results not ready\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), \"Clinton\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), \"Trump\", pres_results$winner)\npres_results$winner <- factor(pres_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\n\ntown_shape <- readOGR(dsn=\"maps\", layer=\"ctgeo\")\ntown_shape_df <- fortify(town_shape, region=\"NAME10\")\nvoters_map <- left_join(town_shape_df, pres_results)\n\nvoters_map$color <- \"\"\nvoters_map$color <- ifelse(is.na(voters_map$winner) | voters_map$winner==\"Results not ready\", \"gray84\", voters_map$color)\nvoters_map$color <- ifelse(voters_map$winner==\"\", \"gray50\", voters_map$color)\n\nvoters_map$color <- ifelse(voters_map$winner==\"Trump\", \"tomato\", voters_map$color)\nvoters_map$color <- ifelse(voters_map$winner==\"Clinton\", \"lightskyblue\", voters_map$color)\n\n# theme_opts <- list(theme(panel.grid.minor = element_blank(),\n# panel.grid.major = element_blank(),\n# panel.background = element_blank(),\n# plot.background = element_rect(fill=\"#e6e8ed\"),\n# panel.border = element_blank(),\n# axis.line = element_blank(),\n# axis.text.x = element_blank(),\n# axis.text.y = element_blank(),\n# axis.ticks = element_blank(),\n# axis.title.x = element_blank(),\n# axis.title.y = element_blank(),\n# plot.title = element_text(size=22)))\n# voters_map$color <- NULL\n\ntime_is <- Sys.time()\nhour_is <- hour(time_is)\nminute_is <- minute(time_is)\ntime_e <- ifelse(hour_is>12, \"p.m.\", \"a.m.\")\ntime_e <- ifelse(time_is==0 | time_is==24, \"a.m.\", time_e)\nhour_is <- ifelse(hour_is>12, hour_is-12, hour_is)\ntime_e <- ifelse(hour_is==0, \"a.m.\", time_e)\nhour_is <- ifelse(hour_is==0, 12, hour_is)\n\nthe_time <- paste0(hour_is, \":\", minute_is, \" \", time_e)\n\n# plot map\ngg <- ggplot(voters_map, aes(long,lat, group=group))\ngg <- gg + geom_polygon(aes(fill=winner))\ngg <- gg + geom_path(color=\"white\")\ngg <- gg + scale_fill_manual(values= c(\"Results not ready\"=\"gainsboro\", \"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\", \"Tie\"=\"gray50\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Where candidates lead\",\n# title=\"Presidential results by town\",\n# subtitle=\"Where candidates are currently leading in votes. Results are unofficial.\",\n subtitle=paste0(\"Unofficial votes as of \", the_time),\n caption=\"Source: Office of Secretary of the State\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.text = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + coord_map(\"sinusoidal\") \n\ngg\n\nggsave(gg, file=\"output/pres_map.png\", width=8, height=4, type=\"cairo-png\")\n\ndate_time <- Sys.time()\ndate_time <- gsub(\" EDT\", \"\", date_time)\ndate_time <- gsub(\" \", \"\", date_time)\ndate_time <- gsub(\"-\", \"\", date_time)\ndate_time <- gsub(\":\", \"\", date_time)\n\nggsave(gg, file=paste0(\"output/archive/pres_map\", date_time, \".png\"), width=8, height=4, type=\"cairo-png\")\n\n# + theme_opts\n\n\n#setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)\n#tweet(\"Boston Common\", mediaPath = \"map.png\")\n\n# Let's randomize\n# \n# pres_results$`Clinton and Kaine` <- sample(1:100, 1, replace=TRUE)\n# pres_results$`Trump and Pence` <- sample(1:100, 1, replace=TRUE)\n# pres_results$`Johnson and Weld` <- sample(1:100, 1, replace=TRUE)\n# pres_results$`Stein and Baraka` <- sample(1:100, 1, replace=TRUE)\n\n\ntotal_results <- filter(town_results, type==\"votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(total_results) <- c(\"percent\", \"id\", \"candidate\")\ntotal_results$percent <- gsub(\"%\", \"\", total_results$percent)\ntotal_results$percent <- as.numeric(total_results$percent )\ntotal_results <- total_results %>%\n spread(candidate, percent)\n\ntotal_results$winner <- \"\"\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` == total_results$`Trump and Pence`), \"Tie\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_results$`Trump and Pence`==0), \"Results not ready\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), \"Clinton\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), \"Trump\", total_results$winner)\ntotal_results$winner <- factor(total_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\n\n\nchart_results <- total_results %>%\n select(id, `Clinton and Kaine`, `Trump and Pence`, `Johnson and Weld`, `Stein and Baraka`) %>%\n summarise(Clinton=sum(`Clinton and Kaine`, na.rm=T), Trump=sum(`Trump and Pence`), Johnson=sum(`Johnson and Weld`, na.rm=T), Stein=sum(`Stein and Baraka`)) %>%\n gather(candidate, votes, 1:4)\n\nchart_results$candidate <- factor(chart_results$candidate, levels = chart_results$candidate[order(chart_results$votes)])\n\nymid <- mean(range(chart_results$votes))\n\n\n\ngg <- ggplot(chart_results, aes(x=candidate, y=votes, color=NULL, fill=candidate))\ngg <- gg + geom_bar(stat=\"identity\", position=\"dodge\")\ngg <- gg + geom_text(aes(x=candidate, y=votes, ymax=votes, family=\"Lato\", label=prettyNum(votes, big.mark=\",\"), \n hjust=ifelse(votes < ymid, -0.1, 1.1), size = 3), \n position = position_dodge(width=1))\ngg <- gg + coord_flip()\ngg <- gg + scale_fill_manual(values= c(\"Stein\"=\"lightgreen\", \"Johnson\"=\"#ffec80\", \"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=NULL)\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.x = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg\n\nggsave(gg, file=\"output/pres_chart.png\", width=4.7, height=4, type=\"cairo-png\")\nggsave(gg, file=paste0(\"output/archive/pres_chart\", date_time, \".png\"), width=4.7, height=4, type=\"cairo-png\")\n\n\nimage_background <- image_read(\"background.png\")\n\n\ntrend_logo <- image_read(\"trend_masthead.png\")\npres_map <- image_read(\"output/pres_map.png\") %>%\n image_crop(\"1700x1200+360\")\npres_chart <- image_read(\"output/pres_chart.png\")\n\npres_map_edit <- image_read(\"background.png\") %>%\n image_composite(pres_map) %>%\n image_composite(image_scale(pres_chart, \"x1000\"), offset=\"+1225+180\") %>%\n image_scale(\"x700\") %>%\n image_write(path=\"output/pres_map.png\", format=\"png\")\n\npres_map_edit2 <- image_read(\"background.png\") %>%\n image_composite(pres_map) %>%\n image_composite(image_scale(pres_chart, \"x1000\"), offset=\"+1225+180\") %>%\n image_scale(\"x700\") %>%\n image_write(path=paste0(\"output/archive/pres_map\", date_time, \".png\"), format=\"png\")\n\n# FTP UPLOADING\n\n\nsource(\"keys.R\")\n\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/pres_map.png\")\nftp_url_archive <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/archive/pres_map\", date_time, \".png\")\n\nftpUpload(\"output/pres_map.png\", ftp_url)\nftpUpload(paste0(\"output/archive/pres_map\", date_time, \".png\"), ftp_url_archive)\n\n", - "created" : 1478273544638.000, - "dirty" : false, - "encoding" : "UTF-8", - "folds" : "", - "hash" : "646942687", - "id" : "54C2F207", - "lastKnownWriteTime" : 1478692267, - "last_content_update" : 1478692267315, - "path" : "~/Documents/Github/trendct-data/2016/11/election-2016-r/pres_map_generator.R", - "project_path" : "pres_map_generator.R", - "properties" : { - "docOutlineSize" : "73.74974679625065", - "docOutlineVisible" : "0", - "tempName" : "Untitled1" - }, - "relative_order" : 1, - "source_on_save" : false, - "source_window" : "", - "type" : "r_source" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/5BD288D7 b/.Rproj.user/6400D2F9/sdb/s-B5824203/5BD288D7 deleted file mode 100644 index 4bd7536..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/5BD288D7 +++ /dev/null @@ -1,21 +0,0 @@ -{ - "collab_server" : "", - "contents" : "# US Map generator\nlibrary(albersusa)\nlibrary(rgeos)\nlibrary(maptools)\nlibrary(ggplot2)\nlibrary(ggalt)\nlibrary(ggthemes)\nlibrary(dplyr)\nlibrary(magick)\n\n\ndate_time <- Sys.time()\ndate_time <- gsub(\" EDT\", \"\", date_time)\ndate_time <- gsub(\" \", \"\", date_time)\ndate_time <- gsub(\"-\", \"\", date_time)\ndate_time <- gsub(\":\", \"\", date_time)\n\nus_results <- read.csv(\"https://docs.google.com/spreadsheets/d/1toi7RJluTCvzRd_IeUc0uc93nrvs6VV9Sof9nR3zaeo/pub?output=csv\", stringsAsFactors=F)\nus_map <- fortify(usa_composite(), region=\"name\")\n\nus_map <- left_join(us_map, us_results)\n\nus_map$Winner <- ifelse(us_map$Winner==\"\", \"Results not ready\", us_map$Winner)\nus_map$Winner <- factor(us_map$Winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n# \n# gg <- ggplot()\n# gg <- gg + geom_map(data=us_map, map=us_map,\n# aes(x=long, y=lat, map_id=id),\n# color=\"#2b2b2b\", size=0.15, fill=\"#e5e3df\")\n# gg <- gg + coord_map(\"polyconic\")\n# gg <- gg + theme_map()\n# gg\n# \n\n\ngg <- ggplot(us_map, aes(long,lat, group=group))\ngg <- gg + geom_polygon(aes(fill=Winner))\ngg <- gg + geom_path(color=\"white\", size=.25)\ngg <- gg + scale_fill_manual(values= c(\"Results not ready\"=\"gray84\", \"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\", \"Tie\"=\"gray50\"))\n#gg <- gg + geom_path(color = \"white\") \ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Presidential results\",\n subtitle=\"Where winners have been called.\",\n caption=\"Source: The Associated Press\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.text = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + coord_map(\"polyconic\")\n\ngg\nggsave(gg, file=\"output/us_pres_map.png\", width=9, height=4, type=\"cairo-png\")\n\n\n\n# Image merge time\n# \n# trend_logo <- image_read(\"trend_masthead.png\")\n# \n# pres_map_edit <- image_read(\"output/us_pres_map.png\") %>%\n# image_crop(\"2200x1200+250\") %>%\n# image_composite(image_scale(trend_logo, \"x50\"), offset=\"+130+1130\") %>%\n# image_scale(\"x700\") %>%\n# image_write(path=\"output/us_pres_map.png\", format=\"png\")\n\nelectoral <- us_results %>%\n group_by(Winner) %>%\n summarise(Electoral_Votes=sum(Electoral.Votes, na.rm=T))\n\nleftover <- subset(electoral, Winner==\"\")\nleftover_count <- leftover$Electoral_Votes\n\nelectoral <- electoral %>%\n filter(Winner==\"Clinton\" | Winner==\"Trump\")\n\nelectoral$Electoral_Votes[2] <- 264\n\ngg <- ggplot(electoral, aes(x = Winner, y = Electoral_Votes, fill = Winner)) \ngg <- gg + geom_bar(stat = \"identity\")\ngg <- gg + geom_hline(yintercept=270)\ngg <- gg + annotate(\"text\", x = 1.5, y = 280, family=\"Lato Regular\", size=6, label = \"270 Electoral Votes to win\")\ngg <- gg + geom_text(aes(x=Winner, y=Electoral_Votes, ymax=Electoral_Votes, family=\"Lato\", label=Electoral_Votes, \n vjust=ifelse(Electoral_Votes<255, -.5, 1.7), size = 3), \n position = position_dodge(width=1))\ngg <- gg + scale_fill_manual(values= c(\"Stein\"=\"lightgreen\", \"Johnson\"=\"#ffec80\", \"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=NULL)\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.text.x = element_text(family=\"Lato Regular\", size=15))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg\n\nggsave(gg, file=\"output/us_pres_chart.png\", width=4, height=4, type=\"cairo-png\")\nggsave(gg, file=paste0(\"output/archive/us_pres_chart\", date_time, \".png\"), width=4, height=4, type=\"cairo-png\")\n\n\nimage_background <- image_read(\"background.png\")\n\n\ntrend_logo <- image_read(\"trend_masthead.png\")\npres_map <- image_read(\"output/us_pres_map.png\") %>%\n image_crop(\"1700x1200+380\")\n\npres_chart <- image_read(\"output/us_pres_chart.png\")\n\npres_map_edit <- image_read(\"background.png\") %>%\n image_composite(pres_map) %>%\n image_composite(image_scale(pres_chart, \"x1000\"), offset=\"+1325+100\") %>%\n image_scale(\"x700\") %>%\n image_write(path=\"output/us_pres_map.png\", format=\"png\")\n\npres_map_edit <- image_read(\"background.png\") %>%\n image_composite(pres_map) %>%\n image_composite(image_scale(pres_chart, \"x1000\"), offset=\"+1325+100\") %>%\n image_scale(\"x700\") %>%\n image_write(path=paste0(\"output/archive/us_pres_map\", date_time, \".png\"), format=\"png\")\n\nsource(\"keys.R\")\n\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/pres_map.png\")\nftp_url_archive <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/social_images/archive/us_pres_map\", date_time, \".png\")\n\n#ftpUpload(\"output/us_pres_map.png\", ftp_url)\nftpUpload(paste0(\"output/archive/us_pres_map\", date_time, \".png\"), ftp_url_archive)\n\n\nif (electoral$Electoral_Votes[1]>=270) {\n cat(\"Hillary Clinton has reached\", electoral$Electoral_Votes[1], \"electoral votes and has won the election.\n Donald Trump has so far reached\", electoral$Electoral_Votes[2], \"but still can't win even if he gets the remaining\", leftover_count, \".\")\n} else if (electoral$Electoral_Votes[2]>=270) {\n cat(\"Donald Trump has reached\", electoral$Electoral_Votes[2], \"electoral votes and has won the election.\n Hillary Clinton has so far reached \", electoral$Electoral_Votes[1], \"but still can't win even if he gets the remaining\", leftover_count, \".\")\n} else {\n cat(\"Donald Trump has reached\", electoral$Electoral_Votes[2], \"electoral votes and Hillary Clinton has so far reached\", electoral$Electoral_Votes[1], \"electoral votes. \n There are\", leftover_count, \"electoral votes still up for grabs.\")\n}\n\n#setup_twitter_oauth(api_key, api_secret, access_token, access_token_secret)\n#tweet(\"Trump currently leads in electoral votes but the night is still young bit.ly/2f4jbWP\", mediaPath = \"output/us_pres_map.png\")\n", - "created" : 1478460509248.000, - "dirty" : false, - "encoding" : "UTF-8", - "folds" : "", - "hash" : "1611594675", - "id" : "5BD288D7", - "lastKnownWriteTime" : 1478675664, - "last_content_update" : 1478675664393, - "path" : "~/Documents/Github/trendct-data/2016/11/election-2016-r/usa_map.R", - "project_path" : "usa_map.R", - "properties" : { - "tempName" : "Untitled3" - }, - "relative_order" : 2, - "source_on_save" : false, - "source_window" : "", - "type" : "r_source" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/5C1279CC b/.Rproj.user/6400D2F9/sdb/s-B5824203/5C1279CC deleted file mode 100644 index ea4c6e1..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/5C1279CC +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478655704683.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "5C1279CC", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478655704683, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "4FD8036F", - "caption" : "pres_results", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=pres_results&cache_key=4FD8036F", - "displayedObservations" : "169", - "environment" : "", - "object" : "pres_results", - "preview" : "0", - "totalObservations" : "169", - "variables" : "6" - }, - "relative_order" : 4, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/612520E5 b/.Rproj.user/6400D2F9/sdb/s-B5824203/612520E5 deleted file mode 100644 index 91cb869..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/612520E5 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478710076705.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "612520E5", - "lastKnownWriteTime" : 4483043824, - "last_content_update" : 1478710076705, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "E7A3CF", - "caption" : "race_towns_long", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=race_towns_long&cache_key=E7A3CF", - "displayedObservations" : 845, - "environment" : "", - "object" : "race_towns_long", - "preview" : 0, - "totalObservations" : 845, - "variables" : 5 - }, - "relative_order" : 21, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/6A057C7F b/.Rproj.user/6400D2F9/sdb/s-B5824203/6A057C7F deleted file mode 100644 index 4d3d4d0..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/6A057C7F +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478667727524.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "6A057C7F", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478667727524, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "F4CC9E35", - "caption" : "per", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=per&cache_key=F4CC9E35", - "displayedObservations" : "154", - "environment" : "", - "object" : "per", - "preview" : "0", - "totalObservations" : "154", - "variables" : "18" - }, - "relative_order" : 13, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/73011573 b/.Rproj.user/6400D2F9/sdb/s-B5824203/73011573 deleted file mode 100644 index f3c7259..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/73011573 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478670152375.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "73011573", - "lastKnownWriteTime" : 4334768680, - "last_content_update" : 1478670152375, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "BE55A3B0", - "caption" : "clinton_lead", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=clinton_lead&cache_key=BE55A3B0", - "displayedObservations" : "5", - "environment" : "", - "object" : "clinton_lead", - "preview" : "0", - "totalObservations" : "5", - "variables" : "14" - }, - "relative_order" : 15, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/87678200 b/.Rproj.user/6400D2F9/sdb/s-B5824203/87678200 deleted file mode 100644 index 4a442aa..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/87678200 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478708818101.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "87678200", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478708818101, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "63BBE540", - "caption" : "town_type_analysis", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=town_type_analysis&cache_key=63BBE540", - "displayedObservations" : 308, - "environment" : "", - "object" : "town_type_analysis", - "preview" : 0, - "totalObservations" : 308, - "variables" : 4 - }, - "relative_order" : 19, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/94C90954 b/.Rproj.user/6400D2F9/sdb/s-B5824203/94C90954 deleted file mode 100644 index b44d56c..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/94C90954 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478670087483.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "94C90954", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478670087483, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "AA1B0ACD", - "caption" : "per_margins", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=per_margins&cache_key=AA1B0ACD", - "displayedObservations" : "126", - "environment" : "", - "object" : "per_margins", - "preview" : "0", - "totalObservations" : "126", - "variables" : "14" - }, - "relative_order" : 14, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/9550D2E3 b/.Rproj.user/6400D2F9/sdb/s-B5824203/9550D2E3 deleted file mode 100644 index 34c675b..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/9550D2E3 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478674988315.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "9550D2E3", - "lastKnownWriteTime" : 2305843009506189467, - "last_content_update" : 1478674988315, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "866740E", - "caption" : "us_results", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=us_results&cache_key=866740E", - "displayedObservations" : "51", - "environment" : "", - "object" : "us_results", - "preview" : "0", - "totalObservations" : "51", - "variables" : "5" - }, - "relative_order" : 17, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/A15B6A1E b/.Rproj.user/6400D2F9/sdb/s-B5824203/A15B6A1E deleted file mode 100644 index 0d93c3c..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/A15B6A1E +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478670195828.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "A15B6A1E", - "lastKnownWriteTime" : 6, - "last_content_update" : 1478670195828, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "2D7F0593", - "caption" : "trump_lead", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=trump_lead&cache_key=2D7F0593", - "displayedObservations" : 5, - "environment" : "", - "object" : "trump_lead", - "preview" : 0, - "totalObservations" : 5, - "variables" : 14 - }, - "relative_order" : 16, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/BF4EDC3E b/.Rproj.user/6400D2F9/sdb/s-B5824203/BF4EDC3E deleted file mode 100644 index d472674..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/BF4EDC3E +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478659673953.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "BF4EDC3E", - "lastKnownWriteTime" : 6, - "last_content_update" : 1478659673953, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "E74FA5B0", - "caption" : "y2012", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=y2012&cache_key=E74FA5B0", - "displayedObservations" : "169", - "environment" : "", - "object" : "y2012", - "preview" : "0", - "totalObservations" : "169", - "variables" : "5" - }, - "relative_order" : 8, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/C1A6D436 b/.Rproj.user/6400D2F9/sdb/s-B5824203/C1A6D436 deleted file mode 100644 index f3284f6..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/C1A6D436 +++ /dev/null @@ -1,29 +0,0 @@ -{ - "collab_server" : "", - "contents" : "", - "created" : 1478655772706.000, - "dirty" : false, - "encoding" : "", - "folds" : "", - "hash" : "0", - "id" : "C1A6D436", - "lastKnownWriteTime" : 4294967302, - "last_content_update" : 1478655772706, - "path" : null, - "project_path" : null, - "properties" : { - "cacheKey" : "275CC12A", - "caption" : "town_results", - "contentUrl" : "grid_resource/gridviewer.html?env=&obj=town_results&cache_key=275CC12A", - "displayedObservations" : "1876", - "environment" : "", - "object" : "town_results", - "preview" : "0", - "totalObservations" : "1876", - "variables" : "4" - }, - "relative_order" : 3, - "source_on_save" : false, - "source_window" : "", - "type" : "r_dataframe" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/D0832164 b/.Rproj.user/6400D2F9/sdb/s-B5824203/D0832164 deleted file mode 100644 index f1f96cc..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/D0832164 +++ /dev/null @@ -1,21 +0,0 @@ -{ - "collab_server" : "", - "contents" : "\n\n\"P\": \"22\",\n\"NM\": \"Working Families Party\",\n\"CD\": \"WF\"\n\ncandidate_id$details <- rownames(candidate_id)\n\"438\": {\n \"CO\": \"0\",\n \"NM\": \"Mary M. Mushinsky\",\n \"LN\": \"Mushinsky\",\n \"MN\": \"M.\",\n \"P\": \"1\",\n \"FN\": \"Mary\",\n \"AD\": \"188 South Cherry St., Wallingford, CT-06492\"\n \n election_data\n 1. \n townStatus\n town_num\n PR, TS\n \n 2. \n stateVotes\n note sure (344, etc)\n 616\n TO\n V\n \n 3.\n reports...\n IR\n IO\n \n 4.\n districts\n 1...\n TN: Town name\n S: False\n PN\n \n 5.\n \"election\": {\n \"PR\": \"0 of 734 (0%)\",\n \"RV\": \"0\",\n \"PT\": \"0 of 169\",\n \"BC\": \"0\",\n \"TO\": \"0.00\",\n \"T\": \"0 of 169\",\n \"ID\": \"1\"\n },\n \n 6.\n \"townVotes\": {\n \"165\": {\n \"1\": [\n {\n \"677\": {\n \"TO\": \"0.00%\",\n \"V\": \"0\"\n }\n },\n {\n \n \n 7. \"voterTurnout\": {\n \n \n ##\n 1.\n countyTowns\n TownName\n TownID\n CountyID\n \n \n 2. townParties\n \n \n 3. candidateIds\n \"438\": {\n \"CO\": \"0\",\n \"NM\": \"Mary M. Mushinsky\",\n \"LN\": \"Mushinsky\",\n \"MN\": \"M.\",\n \"P\": \"1\",\n \"FN\": \"Mary\",\n \"AD\": \"188 South Cherry St., Wallingford, CT-06492\"\n \n \n 4. pollingplaceIds\n \n \"344\": \"Middletown-1-Macdonough School - District 1\",\n \n \n 5. officeList\n \"officeList\": [\n {\n \"1\": {\n \"OO\": \"1\",\n \"D\": \"0\",\n \"DT\": \"\",\n \"OT\": \"SW\",\n \"ID\": \"1\",\n \"NM\": \"Presidential Electors for\"\n }\n },\n \n 6. election\n \n {\n \"NM\": \"11\\/08\\/2016 -- November Presidential Election\",\n \"DNM\": \"2016 Presidential Election\",\n \"EC\": \"SE\",\n \"P\": \"\",\n \"ET\": \"G\",\n \"DT\": \"11\\/08\\/2016\",\n \"ID\": \"1\"\n },\n \n 7.\n partyIds\n \"partyIds\": {\n \"12\": {\n \"P\": \"22\",\n \"NM\": \"Working Families Party\",\n \"CD\": \"WF\"\n },\n \n 8. townPollingPlaces\n \"townPollingPlaces\": {\n \"165\": [\n 777,\n 778\n ],\n \n 9. counties\n \n {\n \"1\": \"Fairfield\",\n \"3\": \"Litchfield\",\n \"2\": \"Hartford\",\n \"5\": \"New Haven\",\n \"4\": \"Middlesex\",\n \"7\": \"Tolland\",\n \"6\": \"New London\",\n \"8\": \"Windham\"\n \n 10.\n \n townIds\n \"townIds\": {\n \"165\": \"Windsor Locks\",\n \"133\": \"Sprague\",\n \"132\": \"South Windsor\",\n \"131\": \"Southington\",\n \"130\": \"Southbury\",\n \"137\": \"Stonington\",\n \"136\": \"Sterling\",", - "created" : 1478295384591.000, - "dirty" : true, - "encoding" : "", - "folds" : "", - "hash" : "4212685078", - "id" : "D0832164", - "lastKnownWriteTime" : 0, - "last_content_update" : 1478295393134, - "path" : null, - "project_path" : null, - "properties" : { - "tempName" : "Untitled2" - }, - "relative_order" : 9, - "source_on_save" : false, - "source_window" : "", - "type" : "r_source" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/DFF2A494 b/.Rproj.user/6400D2F9/sdb/s-B5824203/DFF2A494 deleted file mode 100644 index c5d153b..0000000 --- a/.Rproj.user/6400D2F9/sdb/s-B5824203/DFF2A494 +++ /dev/null @@ -1,21 +0,0 @@ -{ - "collab_server" : "", - "contents" : "\ny2012 <- read.csv(\"data/2012_election_results.csv\", stringsAsFactors=F)\ny2012 <- y2012[1:169,]\n\nsource(\"keys.R\")\nlibrary(censusapi)\nlibrary(jsonlite)\nlibrary(dplyr)\nlibrary(purrr)\nlibrary(tidyr)\nlibrary(scales)\nlibrary(ggplot2)\nlibrary(ggalt)\nlibrary(stringr)\nlibrary(rgdal)\nrequire(maptools)\nlibrary(RCurl)\nlibrary(twitteR)\nlibrary(lubridate)\nelection_data_url <- \"https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json\"\n\nlookup_url <- \"https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json\"\n\n\nelection_data <- fromJSON(election_data_url, simplifyVector=F)\n#town_results <- data.frame((election_data[[6]]))\ntown_results <- data.frame((election_data[[7]]))\ntown_results <- data.frame(t(town_results))\ntown_results$details <- rownames(town_results) \nrownames(town_results) <- NULL\ntown_results$type <- \"\"\n\ntown_results$type <- ifelse(grepl(\".TO\", town_results$details), \"percent_votes\", \"votes\")\ntown_results$candidate <- town_results$details\ntown_results$candidate <- gsub(\".TO\", \"\", town_results$candidate)\ntown_results$candidate <- gsub(\".V\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"\\\\..*\", \"\", town_results$candidate)\ntown_results$town <- gsub(\"X\", \"\", town_results$town)\ntown_results$candidate <- gsub(\".*\\\\.\", \"\", town_results$candidate)\ntown_results$details <- NULL\ntown_results$number <- town_results$t.town_results.\ntown_results$t.town_results. <- NULL\n\nlookup_data <- fromJSON(lookup_url, simplifyVector=F)\ntown_id <- data.frame((lookup_data[[10]]))\ntown_id <- data.frame(t(town_id))\ntown_id$town <- rownames(town_id)\nrownames(town_id) <- NULL\ntown_id$town <- gsub(\"X\", \"\", town_id$town)\n\ntown_results <- left_join(town_results, town_id)\n\ncandidate_id <- data.frame((lookup_data[[3]]))\ncandidate_id <- data.frame(t(candidate_id))\ncandidate_id$details <- rownames(candidate_id)\ncandidate_id$cat <- \"\"\ncandidate_id$cat <- ifelse(grepl(\".CO\", candidate_id$details), \"county\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".NM\", candidate_id$details), \"name\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".LN\", candidate_id$details), \"last\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".MN\", candidate_id$details), \"middle\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".P\", candidate_id$details), \"party\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".FN\", candidate_id$details), \"first\", candidate_id$cat)\ncandidate_id$cat <- ifelse(grepl(\".AD\", candidate_id$details), \"address\", candidate_id$cat)\ncandidate_id$candidate <- gsub(\"\\\\..*\", \"\", candidate_id$details)\ncandidate_id$candidate <- gsub(\"X\", \"\", candidate_id$candidate)\ncandidate_id$details <- NULL\ncandidate_id$text <- candidate_id$t.candidate_id.\ncandidate_id$t.candidate_id. <- NULL\ncandidate_id <- spread(candidate_id, cat, text)\n\ntown_results <- left_join(town_results, candidate_id)\n\n\nparty_id <- data.frame((lookup_data[[7]]))\nparty_id <- data.frame(t(party_id))\nparty_id$details <- rownames(party_id)\nrownames(party_id) <- NULL\nparty_id$category <- \"\"\nparty_id$category <- ifelse(grepl(\".P\", party_id$details), \"party_p\", party_id$category)\nparty_id$category <- ifelse(grepl(\".NM\", party_id$details), \"party_name\", party_id$category)\nparty_id$category <- ifelse(grepl(\".CD\", party_id$details), \"party_abbr\", party_id$category)\nparty_id$party <- gsub(\"\\\\..*\", \"\", party_id$details)\nparty_id$party <- gsub(\"X\", \"\", party_id$party)\n\nparty_id$details <- NULL\nparty_id <- spread(party_id, category, t.party_id.)\n\ntown_results <- left_join(town_results, party_id)\n\n\n\npres_results <- filter(town_results, type==\"percent_votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(pres_results) <- c(\"percent\", \"id\", \"candidate\")\npres_results$percent <- gsub(\"%\", \"\", pres_results$percent)\npres_results$percent <- as.numeric(pres_results$percent )\npres_results <- pres_results %>%\n spread(candidate, percent)\n\n\npres_results$winner <- \"\"\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` == pres_results$`Trump and Pence`), \"Tie\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_results$`Trump and Pence`==0), \"Results not ready\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), \"Clinton\", pres_results$winner)\npres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), \"Trump\", pres_results$winner)\npres_results$winner <- factor(pres_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\ntotal_results <- filter(town_results, type==\"votes\") %>%\n filter(first==\"Donald\" | first==\"Hillary\" | first==\"Jill\" | name==\"Johnson and Weld\") %>%\n select(number, t.town_id., name)\n\ncolnames(total_results) <- c(\"percent\", \"id\", \"candidate\")\ntotal_results$percent <- gsub(\"%\", \"\", total_results$percent)\ntotal_results$percent <- as.numeric(total_results$percent )\ntotal_results <- total_results %>%\n spread(candidate, percent)\n\ntotal_results$winner <- \"\"\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` == total_results$`Trump and Pence`), \"Tie\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_results$`Trump and Pence`==0), \"Results not ready\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), \"Clinton\", total_results$winner)\ntotal_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), \"Trump\", total_results$winner)\ntotal_results$winner <- factor(total_results$winner, levels = c(\"Clinton\", \"Trump\", \"Tie\", \"Results not ready\"))\n\n\ncolnames(total_results) <- c(\"Town\", \"Clinton\", \"Johnson16\", \"Stein\", \"Trump\", \"winner\")\ncolnames(pres_results) <- c(\"Town\", \"Clinton\", \"Johnson16\", \"Stein\", \"Trump\", \"winner\")\n\np2012 <- y2012\np2012$Romney <- round(y2012$Romney/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Obama <- round(y2012$Obama/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Anderson <- round(y2012$Anderson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\np2012$Johnson <- round(y2012$Johnson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2)\n\n\ntot <- left_join(y2012, total_results)\nper <- left_join(p2012, pres_results)\n\nper$TCdiff <- per$Trump-per$Clinton\nper$ROdiff <- per$Romney-per$Obama\n\nper$COdiff <- per$Clinton-per$Obama\nper$TRdiff <- per$Trump-per$Romney\n\n# margins\nlibrary(knitr)\n\ntot$vote_diff <- tot$Clinton-tot$Trump\ntot <- arrange(tot, vote_diff)\ntot <- subset(tot, (vote_diff<6) & (vote_diff > -6))\ntot <- subset(tot, winner!=\"Results not ready\")\nkable(tot)\n\n# where Clinton lead the most\n\nper_margins <- arrange(per, TCdiff)\nper_margins <- subset(per_margins, winner!=\"Results not ready\")\n\nclinton_lead <- head(per_margins, 5)\nkable(clinton_lead)\n\ntrump_lead <- tail(per_margins, 5)\nkable(trump_lead)\n\n\n## Obama versus Clinton\n\nlibrary(ggalt)\n\nper$Trump[per$Trump == 0.00] <- NA\nper$Clinton[per$Clinton == 0.00] <- NA\n\nper <- subset(per, !is.na(per$Clinton))\n\n# start here\n\nper <- arrange(per, Clinton)\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0)\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc)\n\ngg <- ggplot()\ngg <- gg + geom_dumbbell(data=per, aes(x=Clinton, xend=Obama, y=Town, group=Town), color=\"#a3c4dc\", size=0.5, point.colour.l=\"#0e668b\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Clinton and Obama\",\n subtitle= \"Percent of votes in 2012 compared to 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + annotate(\"text\", x = 13.2, y = 150, label = \"Barack Obama\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Hillary Clinton\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"#a3c4dc\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"#0e668b\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nggsave(gg, file=\"output/hillary_obama_1.png\", width=6, height=12, type=\"cairo-png\")\n\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/hillary_obama_1.png\")\nftpUpload(\"output/hillary_obama_1.png\", ftp_url)\n\n# OK, alphabetical\n\nper <- arrange(per, desc(as.character(Town)))\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0)\nper$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc)\n\n\ngg <- ggplot(per, aes(x=Clinton, xend=Obama, y=Town, group=Town))\ngg <- gg + geom_dumbbell(color=\"#a3c4dc\", size=0.5, point.colour.l=\"#0e668b\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Clinton and Obama\",\n subtitle= \"Percent of votes in 2012 compared 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\n\ngg <- gg + annotate(\"text\", x = 13.2, y = 150, label = \"Barack Obama\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Hillary Clinton\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"#a3c4dc\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"#0e668b\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nggsave(gg, file=\"output/hillary_obama_2.png\", width=6, height=12, type=\"cairo-png\")\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/hillary_obama_2.png\")\nftpUpload(\"output/hillary_obama_2.png\", ftp_url)\n\n# Trump versus Romney\n\n# start here\n\nper <- arrange(per, Trump)\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0)\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc)\n\ngg <- ggplot()\ngg <- gg + geom_dumbbell(data=per, aes(x=Trump, xend=Romney, y=Town, group=Town), color=\"tomato1\", size=0.5, point.colour.l=\"red4\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Trump and Romney\",\n subtitle= \"Percent of votes in 2012 compared to 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + annotate(\"text\", x = 11.8, y = 150, label = \"Mitt Romney\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Donald Trump\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"tomato1\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"red4\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(data=per, aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nggsave(gg, file=\"output/trump_romney_1.png\", width=6, height=12, type=\"cairo-png\")\n\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/trump_romney_1.png\")\nftpUpload(\"output/trump_romney_1.png\", ftp_url)\n\n# OK, alphabetical\n\nper <- arrange(per, desc(as.character(Town)))\nper$Town <- factor(per$Town, levels=unique(per$Town))\n\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0)\nper$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc)\nper$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc)\n\n\ngg <- ggplot(per, aes(x=Trump, xend=Romney, y=Town, group=Town))\ngg <- gg + geom_dumbbell(color=\"tomato1\", size=0.5, point.colour.l=\"red4\")\n# gg <- gg + scale_x_continuous(label=percent)\ngg <- gg + labs(x=NULL, y=NULL)\ngg <- gg + scale_x_continuous(limits = c(0, 110))\ngg <- gg + theme_bw()\ngg <- gg + theme(axis.title = element_text(family = \"Lato Black\", color=\"#666666\", face=\"bold\", size=6)) \ngg <- gg + theme(plot.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.background=element_rect(fill=\"#f7f7f7\"))\ngg <- gg + theme(panel.grid.minor=element_blank())\ngg <- gg + theme(panel.grid.major.y=element_blank())\ngg <- gg + theme(panel.grid.major.x=element_line())\ngg <- gg+ geom_vline(xintercept = 50)\ngg <- gg+ geom_vline(xintercept = 20, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 40, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 60, linetype=\"dotted\", colour=\"lightgray\")\ngg <- gg+ geom_vline(xintercept = 80, linetype=\"dotted\", colour=\"lightgray\")\n\ngg <- gg + theme(axis.ticks=element_blank())\n#gg <- gg + theme(axis.text = element_text(size = 4))\ngg <- gg + labs(title = \"Support for Trump and Romney\",\n subtitle= \"Percent of votes in 2012 compared 2016\",\n caption=\"Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=19))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\n\ngg <- gg + annotate(\"text\", x = 11.8, y = 150, label = \"Mitt Romney\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"text\", x = 12.8, y = 153, label = \"Donald Trump\", size=3, colour=\"gray30\")\ngg <- gg + annotate(\"point\", x = 3, y = 150, colour = \"tomato1\", size = 2) \ngg <- gg + annotate(\"point\", x = 3, y = 153, colour = \"red4\", size = 2)\ngg <- gg + theme(panel.border=element_blank())\ngg <- gg + theme(axis.text.y = element_text(family=\"Lato Regular\", size=12))\ngg <- gg + theme(legend.position=\"none\")\ngg <- gg + theme(axis.line = element_blank(),\n axis.text.y = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + geom_text(aes(x=arrange_hc, y=Town, family=\"Lato\", label=Town), size=2)\n\nggsave(gg, file=\"output/trump_romney_2.png\", width=6, height=12, type=\"cairo-png\")\nftp_url <- paste0(\"ftp://\", ftp_login, \"@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/trump_romney_2.png\")\nftpUpload(\"output/trump_romney_2.png\", ftp_url)\n\n\n# By town type\n\nurban <- read.csv(\"data/urban_rural.csv\", stringsAsFactors=FALSE)\nurban <- urban[c(\"NAME10\", \"Type\")]\ncolnames(urban) <- c(\"Town\", \"Type\")\n\nper <- left_join(per, urban)\ntotal_results <- left_join(total_results, urban)\n\ntown_type_analysis <-per %>%\n select(Town, Type, Trump, Clinton) %>%\n gather(\"candidate\", \"percent\", 3:4)\n\ngg <- ggplot(town_type_analysis, aes(x = Type, y = percent, fill = candidate)) \ngg <- gg + geom_boxplot() \ngg <- gg + scale_fill_manual(values= c(\"Clinton\" = \"lightskyblue\", \"Trump\"=\"tomato\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Presidential support by town type\",\n subtitle=\"Trump got the rural voters in Connecticut while voters in \\nurban cities supported Clinton\",\n caption=\"Source: Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.text.x = element_text(family=\"Lato Regular\", size=15))\ngg <- gg + theme(axis.line = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg\n\n\n# By whiteness of town\n\nrace_towns <- getCensus(name=\"acs5\",\n vintage=2014,\n key=census_key,\n vars=c(\"NAME\", \"B02001_001E\", \"B02001_002E\", \"B02001_003E\",\n \"B02001_004E\", \"B02001_005E\", \"B03001_003E\"),\n region=\"county subdivision:*\", regionin=\"state:09\")\n\ncolnames(race_towns) <- c(\"town\", \"state\", \"county\", \"countysub\", \"total_pop\", \"White\", \"Black\", \"Indian\", \"Asian\", \"Hispanic\")\nrace_towns <- race_towns[c(\"town\", \"total_pop\", \"White\", \"Black\", \"Indian\", \"Asian\", \"Hispanic\")]\nrace_towns <- subset(race_towns, !grepl(\"County subdivisions\", town))\nrace_towns$town <- gsub(\" town.*\", \"\", race_towns$town)\n\nrace_towns_long <- race_towns %>%\n gather(\"race_ethnicity\", \"population\", 3:7) %>%\n mutate(percent_population=round(population/total_pop*100,2))\n\nrace_towns_white <- race_towns_long %>%\n filter(race_ethnicity==\"White\") %>%\n select(town, percent_population)\n\ncolnames(race_towns_white) <- c(\"Town\", \"Percent_White\")\n\nper <- left_join(per, race_towns_white)\n\ngg <- ggplot(per, aes(Percent_White, Trump))\ngg <- gg + geom_point()\ngg <- gg + labs(x=\"Percent white population\", y=\"Percent votes for Trump\", \n title=\"Trump votes compared to white population\",\n subtitle=\"The whiter a town, the higher likelihood they supported Donald Trump.\",\n caption=\"Source: Office of the Secretary of the State \\n Andrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.ticks = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + theme_bw()\ngg\n\n\n# Swapped?\n\nper$winner2012 <- ifelse(per$Obama>per$Romney, \"Obama\", \"Romney\")\nper$flip <- \"\"\nper$flip <- ifelse(per$winner==\"Clinton\" & per$winner2012==\"Obama\", \"Clinton retained\", per$flip)\nper$flip <- ifelse(per$winner==\"Trump\" & per$winner2012==\"Romney\", \"Trump retained\", per$flip)\nper$flip <- ifelse(per$winner==\"Trump\" & per$winner2012==\"Obama\", \"Flipped to Trump\", per$flip)\nper$flip <- ifelse(per$winner==\"Clinton\" & per$winner2012==\"Romney\", \"Flipped to Clinton\", per$flip)\n\ntown_shape <- readOGR(dsn=\"maps\", layer=\"ctgeo\")\ntown_shape_df <- fortify(town_shape, region=\"NAME10\")\n\nnames(per)[names(per) == 'Town'] <- 'id'\n\n\nvoters_map <- left_join(town_shape_df,per)\n\ngg <- ggplot(voters_map, aes(long,lat, group=group))\ngg <- gg + geom_polygon(aes(fill=flip))\ngg <- gg + geom_path(color=\"white\")\ngg <- gg + scale_fill_manual(values= c(\"Clinton retained\"=\"lightskyblue\", \"Flipped to Clinton\" = \"navyblue\", \"Trump retained\"=\"tomato\", \"Flipped to Trump\"=\"red4\"))\ngg <- gg + labs(x=NULL, y=NULL, \n title=\"Where towns chose or flipped for Clinton or Trump\",\n # title=\"Presidential results by town\",\n # subtitle=\"Where candidates are currently leading in votes. Results are unofficial.\",\n subtitle=\"Compared to whether Obama or Romney won in 2012\",\n caption=\"Source: Office of Secretary of the State \\nAndrew Ba Tran/TrendCT.org\")\ngg <- gg + theme(plot.title=element_text(face=\"bold\", family=\"Lato Regular\", size=24))\ngg <- gg + theme(plot.subtitle=element_text(family=\"Lato Regular\", size=17))\ngg <- gg + theme(plot.caption=element_text(family=\"Lato Regular\", size=12, color=\"gray\", margin=margin(t=10, r=80)))\ngg <- gg + theme(axis.line = element_blank(),\n axis.text = element_blank(),\n axis.ticks = element_blank(),\n panel.grid.major = element_blank(),\n panel.grid.minor = element_blank(),\n panel.border = element_blank(),\n panel.background = element_blank(),\n legend.title=element_blank()) \ngg <- gg + coord_map(\"sinusoidal\") \n\ngg\n\n#ggsave(gg, file=\"output/pres_map.png\", width=8, height=4, type=\"cairo-png\")", - "created" : 1478659647222.000, - "dirty" : false, - "encoding" : "UTF-8", - "folds" : "", - "hash" : "526338730", - "id" : "DFF2A494", - "lastKnownWriteTime" : 1478711451, - "last_content_update" : 1478711451948, - "path" : "~/Documents/Github/trendct-data/2016/11/election-2016-r/analysis2016.R", - "project_path" : "analysis2016.R", - "properties" : { - "tempName" : "Untitled4" - }, - "relative_order" : 7, - "source_on_save" : false, - "source_window" : "", - "type" : "r_source" -} \ No newline at end of file diff --git a/.Rproj.user/6400D2F9/sdb/s-B5824203/lock_file b/.Rproj.user/6400D2F9/sdb/s-B5824203/lock_file deleted file mode 100644 index e69de29..0000000 diff --git a/.Rproj.user/6400D2F9/viewer-cache/2D7F0593.Rdata b/.Rproj.user/6400D2F9/viewer-cache/2D7F0593.Rdata deleted file mode 100644 index beb6667..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/2D7F0593.Rdata and /dev/null differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/4FD8036F.Rdata b/.Rproj.user/6400D2F9/viewer-cache/4FD8036F.Rdata index 24940fa..3aa0190 100644 Binary files a/.Rproj.user/6400D2F9/viewer-cache/4FD8036F.Rdata and b/.Rproj.user/6400D2F9/viewer-cache/4FD8036F.Rdata differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/83B32485.Rdata b/.Rproj.user/6400D2F9/viewer-cache/83B32485.Rdata deleted file mode 100644 index b937bc3..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/83B32485.Rdata and /dev/null differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/866740E.Rdata b/.Rproj.user/6400D2F9/viewer-cache/866740E.Rdata deleted file mode 100644 index 8ad05c8..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/866740E.Rdata and /dev/null differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/AA1B0ACD.Rdata b/.Rproj.user/6400D2F9/viewer-cache/AA1B0ACD.Rdata deleted file mode 100644 index 86b89ae..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/AA1B0ACD.Rdata and /dev/null differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/BE55A3B0.Rdata b/.Rproj.user/6400D2F9/viewer-cache/BE55A3B0.Rdata deleted file mode 100644 index de6b177..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/BE55A3B0.Rdata and /dev/null differ diff --git a/.Rproj.user/6400D2F9/viewer-cache/E74FA5B0.Rdata b/.Rproj.user/6400D2F9/viewer-cache/E74FA5B0.Rdata deleted file mode 100644 index 552ac70..0000000 Binary files a/.Rproj.user/6400D2F9/viewer-cache/E74FA5B0.Rdata and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/paths b/.Rproj.user/shared/notebooks/paths index d39503a..11c366d 100644 --- a/.Rproj.user/shared/notebooks/paths +++ b/.Rproj.user/shared/notebooks/paths @@ -1,5 +1,6 @@ /Users/andrewtran/Documents/Github/trendct-data/2016/10/drought-2016/maps_maker.R="2F806A67" /Users/andrewtran/Documents/Github/trendct-data/2016/11/election-2016-r/.gitignore="DE05CA6A" +/Users/andrewtran/Documents/Github/trendct-data/2016/11/election-2016-r/analysis2.Rmd="72400AFD" /Users/andrewtran/Documents/Github/trendct-data/2016/11/election-2016-r/analysis2016.R="ECBDD892" /Users/andrewtran/Documents/Github/trendct-data/2016/11/election-2016-r/index.Rmd="DC3B7CC4" /Users/andrewtran/Documents/Github/trendct-data/2016/11/election-2016-r/keys.R="2D50E07F" diff --git a/.gitignore b/.gitignore index edb5f60..b7ea467 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .httr-oauth -keys.R \ No newline at end of file +keys.R +.Rproj.user diff --git a/analysis2.Rmd b/analysis2.Rmd new file mode 100644 index 0000000..e60280c --- /dev/null +++ b/analysis2.Rmd @@ -0,0 +1,421 @@ +--- +title: "2016 CT election analysis II" +author: "Andrew Ba Tran" +date: "November 9, 2016" +output: html_document +--- + +```{r setup, echo=FALSE, warning=F, message=F, fig.width=8, fig.height=12} + +y2012 <- read.csv("data/2012_election_results.csv", stringsAsFactors=F) +y2012 <- y2012[1:169,] + +source("keys.R") +library(censusapi) +library(jsonlite) +library(dplyr) +library(purrr) +library(tidyr) +library(scales) +library(ggplot2) +library(ggalt) +library(stringr) +library(rgdal) +require(maptools) +library(RCurl) +library(twitteR) +library(lubridate) +election_data_url <- "https://s3.amazonaws.com/election-data-2016/data/Electiondata_1.json" + +lookup_url <- "https://s3.amazonaws.com/election-data-2016/data/Lookupdata_1.json" + + +election_data <- fromJSON(election_data_url, simplifyVector=F) +#town_results <- data.frame((election_data[[6]])) +town_results <- data.frame((election_data[[7]])) +town_results <- data.frame(t(town_results)) +town_results$details <- rownames(town_results) +rownames(town_results) <- NULL +town_results$type <- "" + +town_results$type <- ifelse(grepl(".TO", town_results$details), "percent_votes", "votes") +town_results$candidate <- town_results$details +town_results$candidate <- gsub(".TO", "", town_results$candidate) +town_results$candidate <- gsub(".V", "", town_results$candidate) +town_results$town <- gsub("\\..*", "", town_results$candidate) +town_results$town <- gsub("X", "", town_results$town) +town_results$candidate <- gsub(".*\\.", "", town_results$candidate) +town_results$details <- NULL +town_results$number <- town_results$t.town_results. +town_results$t.town_results. <- NULL + +lookup_data <- fromJSON(lookup_url, simplifyVector=F) +town_id <- data.frame((lookup_data[[10]])) +town_id <- data.frame(t(town_id)) +town_id$town <- rownames(town_id) +rownames(town_id) <- NULL +town_id$town <- gsub("X", "", town_id$town) + +town_results <- left_join(town_results, town_id) + +candidate_id <- data.frame((lookup_data[[3]])) +candidate_id <- data.frame(t(candidate_id)) +candidate_id$details <- rownames(candidate_id) +candidate_id$cat <- "" +candidate_id$cat <- ifelse(grepl(".CO", candidate_id$details), "county", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".NM", candidate_id$details), "name", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".LN", candidate_id$details), "last", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".MN", candidate_id$details), "middle", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".P", candidate_id$details), "party", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".FN", candidate_id$details), "first", candidate_id$cat) +candidate_id$cat <- ifelse(grepl(".AD", candidate_id$details), "address", candidate_id$cat) +candidate_id$candidate <- gsub("\\..*", "", candidate_id$details) +candidate_id$candidate <- gsub("X", "", candidate_id$candidate) +candidate_id$details <- NULL +candidate_id$text <- candidate_id$t.candidate_id. +candidate_id$t.candidate_id. <- NULL +candidate_id <- spread(candidate_id, cat, text) + +town_results <- left_join(town_results, candidate_id) + + +party_id <- data.frame((lookup_data[[7]])) +party_id <- data.frame(t(party_id)) +party_id$details <- rownames(party_id) +rownames(party_id) <- NULL +party_id$category <- "" +party_id$category <- ifelse(grepl(".P", party_id$details), "party_p", party_id$category) +party_id$category <- ifelse(grepl(".NM", party_id$details), "party_name", party_id$category) +party_id$category <- ifelse(grepl(".CD", party_id$details), "party_abbr", party_id$category) +party_id$party <- gsub("\\..*", "", party_id$details) +party_id$party <- gsub("X", "", party_id$party) + +party_id$details <- NULL +party_id <- spread(party_id, category, t.party_id.) + +town_results <- left_join(town_results, party_id) + + + +pres_results <- filter(town_results, type=="percent_votes") %>% + filter(first=="Donald" | first=="Hillary" | first=="Jill" | name=="Johnson and Weld") %>% + select(number, t.town_id., name) + +colnames(pres_results) <- c("percent", "id", "candidate") +pres_results$percent <- gsub("%", "", pres_results$percent) +pres_results$percent <- as.numeric(pres_results$percent ) +pres_results <- pres_results %>% + spread(candidate, percent) + + +pres_results$winner <- "" +pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` == pres_results$`Trump and Pence`), "Tie", pres_results$winner) +pres_results$winner <- ifelse((pres_results$`Clinton and Kaine`==0 & pres_results$`Trump and Pence`==0), "Results not ready", pres_results$winner) +pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` > pres_results$`Trump and Pence`), "Clinton", pres_results$winner) +pres_results$winner <- ifelse((pres_results$`Clinton and Kaine` < pres_results$`Trump and Pence`), "Trump", pres_results$winner) +pres_results$winner <- factor(pres_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) + + +total_results <- filter(town_results, type=="votes") %>% + filter(first=="Donald" | first=="Hillary" | first=="Jill" | name=="Johnson and Weld") %>% + select(number, t.town_id., name) + +colnames(total_results) <- c("percent", "id", "candidate") +total_results$percent <- gsub("%", "", total_results$percent) +total_results$percent <- as.numeric(total_results$percent ) +total_results <- total_results %>% + spread(candidate, percent) + +total_results$winner <- "" +total_results$winner <- ifelse((total_results$`Clinton and Kaine` == total_results$`Trump and Pence`), "Tie", total_results$winner) +total_results$winner <- ifelse((total_results$`Clinton and Kaine`==0 & total_results$`Trump and Pence`==0), "Results not ready", total_results$winner) +total_results$winner <- ifelse((total_results$`Clinton and Kaine` > total_results$`Trump and Pence`), "Clinton", total_results$winner) +total_results$winner <- ifelse((total_results$`Clinton and Kaine` < total_results$`Trump and Pence`), "Trump", total_results$winner) +total_results$winner <- factor(total_results$winner, levels = c("Clinton", "Trump", "Tie", "Results not ready")) + + +colnames(total_results) <- c("Town", "Clinton", "Johnson16", "Stein", "Trump", "winner") +colnames(pres_results) <- c("Town", "Clinton", "Johnson16", "Stein", "Trump", "winner") + +p2012 <- y2012 +p2012$Romney <- round(y2012$Romney/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Obama <- round(y2012$Obama/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Anderson <- round(y2012$Anderson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) +p2012$Johnson <- round(y2012$Johnson/(y2012$Romney+y2012$Obama+y2012$Anderson+ y2012$Johnson)*100,2) + + +tot <- left_join(y2012, total_results) +per <- left_join(p2012, pres_results) + +per$TCdiff <- per$Trump-per$Clinton +per$ROdiff <- per$Romney-per$Obama + +per$COdiff <- per$Clinton-per$Obama +per$TRdiff <- per$Trump-per$Romney + +# margins +library(knitr) +``` + + + +```{r setup2, echo=FALSE, warning=F, message=F, fig.width=8, fig.height=12} +library(ggalt) + +per$Trump[per$Trump == 0.00] <- NA +per$Clinton[per$Clinton == 0.00] <- NA + +per <- subset(per, !is.na(per$Clinton)) + +# start here + +per <- arrange(per, Clinton) +per$Town <- factor(per$Town, levels=unique(per$Town)) + +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc) + + +# OK, alphabetical + +per <- arrange(per, desc(as.character(Town))) +per$Town <- factor(per$Town, levels=unique(per$Town)) + +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton < per$Obama, per$Clinton - 8, 0) +per$arrange_hc <- ifelse(per$Clinton < 50 & per$Clinton > per$Obama, per$Obama - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton > per$Obama, per$Clinton + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Clinton > 50 & per$Clinton < per$Obama, per$Obama + 8, per$arrange_hc) + +# Trump versus Romney + +# start here + +per <- arrange(per, Trump) +per$Town <- factor(per$Town, levels=unique(per$Town)) + +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc) + + +# OK, alphabetical + +per <- arrange(per, desc(as.character(Town))) +per$Town <- factor(per$Town, levels=unique(per$Town)) + +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump < per$Romney, per$Trump - 8, 0) +per$arrange_hc <- ifelse(per$Trump < 50 & per$Trump > per$Romney, per$Romney - 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump > per$Romney, per$Trump + 8, per$arrange_hc) +per$arrange_hc <- ifelse(per$Trump > 50 & per$Trump < per$Romney, per$Romney + 8, per$arrange_hc) +``` + +```{r part2, echo=FALSE, warning=F, message=F, fig.height=5, fig.width=8} + +# By town type + +urban <- read.csv("data/urban_rural.csv", stringsAsFactors=FALSE) +urban <- urban[c("NAME10", "Type")] +colnames(urban) <- c("Town", "Type") + +per <- left_join(per, urban) +total_results <- left_join(total_results, urban) + +town_type_analysis <-per %>% + select(Town, Type, Trump, Clinton) %>% + gather("candidate", "percent", 3:4) + +gg <- ggplot(town_type_analysis, aes(x = Type, y = percent, fill = candidate)) +gg <- gg + geom_boxplot() +gg <- gg + scale_fill_manual(values= c("Clinton" = "lightskyblue", "Trump"="tomato")) +gg <- gg + labs(x=NULL, y=NULL, + title="Presidential support by town type", + subtitle="Trump got the rural voters in Connecticut while voters in \nurban cities supported Clinton", + caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + theme(axis.text.x = element_text(family="Lato Regular", size=15)) +gg <- gg + theme(axis.line = element_blank(), + axis.ticks = element_blank(), + panel.grid.major = element_blank(), + panel.grid.minor = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + legend.title=element_blank()) +gg + +``` + + +```{r part3, echo=FALSE, warning=F, message=F, fig.height=7, fig.width=7} + +race_towns <- getCensus(name="acs5", + vintage=2014, + key=census_key, + vars=c("NAME", "B02001_001E", "B02001_002E", "B02001_003E", + "B02001_004E", "B02001_005E", "B03001_003E"), + region="county subdivision:*", regionin="state:09") + +colnames(race_towns) <- c("town", "state", "county", "countysub", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic") +race_towns <- race_towns[c("town", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic")] +race_towns <- subset(race_towns, !grepl("County subdivisions", town)) +race_towns$town <- gsub(" town.*", "", race_towns$town) + +race_towns_long <- race_towns %>% + gather("race_ethnicity", "population", 3:7) %>% + mutate(percent_population=round(population/total_pop*100,2)) + +race_towns_white <- race_towns_long %>% + filter(race_ethnicity=="White") %>% + select(town, percent_population) + +colnames(race_towns_white) <- c("Town", "Percent_White") + +per <- left_join(per, race_towns_white) + +gg <- ggplot(per, aes(Percent_White, Trump)) +gg <- gg + geom_point() +gg <- gg + labs(x="Percent white population", y="Percent votes for Trump", + title="Trump votes compared to white population", + subtitle="The whiter a town, the higher likelihood they supported Donald Trump.", + caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=24)) +gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) +gg <- gg + theme(plot.caption=element_text(family="Lato Regular", size=12, color="gray", margin=margin(t=10, r=80))) +gg <- gg + theme(axis.line = element_blank(), + axis.ticks = element_blank(), + panel.border = element_blank(), + panel.background = element_blank(), + legend.title=element_blank()) +gg <- gg + theme_bw() +gg + +per2 <- per %>% + mutate(Minorities=100-Percent_White) %>% + select(Town, Percent_White, Clinton, Trump) %>% + gather("candidate", "percent", 3:4) + +``` + + +```{r part4, echo=FALSE, warning=F, message=F, fig.height=6, fig.width=8} + + +gg <- ggplot() +gg <- gg + geom_point(data=per2, aes(Percent_White, percent, color=candidate)) +gg <- gg + scale_color_manual(values= c("lightskyblue", "tomato")) +gg <- gg + facet_wrap(~candidate) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + scale_y_continuous(limits = c(0, 110)) +gg <- gg + labs(x="Percent white population", y="Percent of votes", + title="Votes compared to white population", + subtitle="The larger the white population, the higher the likelihood they supported Donald Trump.", + caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme_bw(base_family="Lato Regular") +gg <- gg + theme(axis.ticks.y=element_blank()) +#gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(legend.key=element_blank()) +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=22)) +gg <- gg + theme(plot.caption=element_text(face="bold", family="Lato Regular", size=9, color="gray28", margin=margin(t=10, r=80))) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(strip.background=element_blank()) +gg <- gg + theme(strip.text.x = element_text(size = 10, colour = "grey5")) + +gg + + trump_per2 <- subset(per2, candidate=="Trump") + clinton_per2 <- subset(per2, candidate=="Clinton") + + cat("Trump correlation rate: ", cor(trump_per2$percent, trump_per2$Percent_White, use="complete.obs")) + cat("\nClinton correlation rate: ", cor(clinton_per2$percent, clinton_per2$Percent_White, use="complete.obs")) + +``` + + +```{r part5, echo=FALSE, warning=F, message=F, fig.height=6, fig.width=8} + +town_profiles <- read.csv("https://github.com/CT-Data-Haven/WebsiteIndicators/raw/master/TownProfiles/5year2014town_profile_expanded_CWS.csv", stringsAsFactors=F) + +town_profiles <- town_profiles[14:182,] + +selected_tp <- town_profiles %>% + select(Town, X..of.adults..Satisfied.with.area.where.he.she.lives, +X..of.adults..Feel.safe.to.walk.at.night.in.his.her.neighborhood, +Food.Insecurity.Rate, +Housing.Insecurity.Rate, +Transportation.Insecurity.Rate, +X..of.adults..Without.a.bank.account, +X..of.working.adults..Underemployed, +White.Non.Hispanic.Population, +X..of.Total.Population..Foreign.born, +Homeownership.Rate, +X..of.Households..Cost.Burdened, +X..of.Population.Ages.25...without.a.high.school.diploma, +X..of.Population.Ages.25...with.a.Bachelor.s.degree.or.more, +Median.Household.Income, +Poverty.Rate, +Low.Income.Rate) + +colnames(selected_tp) <- c("Town", "adults.satisfied.with.area", "feel.safe.to.walk.at.night", "food.insecurity.rate", "housing.insecurity.rate", "transportation.insecurity.rate", "adults.without.bank.account", "underemployed.adults", "white.population", "foreign.born.population", "homeownership.rate", "households.cost.burdened", "ages25.without.hs.diploma", +"ages25.with.bach.degree", "median.household.income", "poverty.rate", "low.income.rate") + +for (i in 2:ncol(selected_tp)) { + selected_tp[,i] <- as.numeric(gsub("%", "", selected_tp[,i])) +} + +col_names <- c("adults.satisfied.with.area", "feel.safe.to.walk.at.night", "food.insecurity.rate", "housing.insecurity.rate", "transportation.insecurity.rate", "adults.without.bank.account", "underemployed.adults", "white.population", "foreign.born.population", "homeownership.rate", "households.cost.burdened", "ages25.without.hs.diploma", +"ages25.with.bach.degree", "median.household.income", "poverty.rate", "low.income.rate") + +col_names_clean <- c("Rate of adults satisfied with area", "Percent of adults who feel safe to walk area at night", "Food insecurity rate", "Housing insecurity rate", "Transportation insecurity rate", "Adults without a bank ccount", "Underemployed adults", "White population", "Foreign born population", "Home ownership rate", "Household costs burdened", "Adults over 25 without a High School diploma", +"Adults over 25 with a bachelor's degree", "Median household income", "Poverty rate", "Low income rate") + +wellness_columns <- data.frame(col_names, col_names_clean) + +per3 <- left_join(per2, selected_tp) + +for (i in 1:nrow(wellness_columns)) { + column_name_backup <- wellness_columns$col_names[i] + column_title <- wellness_columns$col_names_clean[i] + + names(per3)[names(per3) == wellness_columns$col_names[i]] <- 'column_name' + + + gg <- ggplot() + gg <- gg + geom_point(data=per3, aes(column_name, percent, color=candidate)) + gg <- gg + scale_color_manual(values= c("lightskyblue", "tomato")) + gg <- gg + facet_wrap(~candidate) + #gg <- gg + scale_x_continuous(limits = c(0, 110)) + gg <- gg + scale_y_continuous(limits = c(0, 110)) + gg <- gg + labs(x=column_title, y="Percent of votes", + title=paste0("Votes compared to ", column_title), + #subtitle="The larger the white population, the higher the likelihood they supported Donald Trump.", + caption="Source: Office of the Secretary of the State, DataHaven\n Andrew Ba Tran/TrendCT.org") + gg <- gg + theme_bw(base_family="Lato Regular") + gg <- gg + theme(axis.ticks.y=element_blank()) + #gg <- gg + theme(panel.border=element_blank()) + gg <- gg + theme(legend.key=element_blank()) + gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=22)) + gg <- gg + theme(plot.caption=element_text(face="bold", family="Lato Regular", size=9, color="gray28", margin=margin(t=10, r=80))) + gg <- gg + theme(legend.position="none") + gg <- gg + theme(strip.background=element_blank()) + gg <- gg + theme(strip.text.x = element_text(size = 10, colour = "grey5")) + + print(gg) + + trump_per3 <- subset(per3, candidate=="Trump") + clinton_per3 <- subset(per3, candidate=="Clinton") + + cat("Trump correlation rate: ", cor(trump_per3$percent, trump_per3$Percent_White, use="complete.obs")) + cat("\nClinton correlation rate: ", cor(clinton_per3$percent, clinton_per3$Percent_White, use="complete.obs")) + + colnames(per3) <- c("Town", "Percent_White", "candidate", "percent", "adults.satisfied.with.area", "feel.safe.to.walk.at.night", "food.insecurity.rate", "housing.insecurity.rate", "transportation.insecurity.rate", "adults.without.bank.account", "underemployed.adults", "white.population", "foreign.born.population", "homeownership.rate", "households.cost.burdened", "ages25.without.hs.diploma", +"ages25.with.bach.degree", "median.household.income", "poverty.rate", "low.income.rate") + +} + + +``` + diff --git a/analysis2.html b/analysis2.html new file mode 100644 index 0000000..4080671 --- /dev/null +++ b/analysis2.html @@ -0,0 +1,207 @@ + + + + + + + + + + + + + + +2016 CT election analysis II + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + +

+

+

+
## Trump correlation rate:  0.6626457
+
## 
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+

+
## Trump correlation rate:  0.6626457
+## Clinton correlation rate:  -0.6788967
+ + + + +
+ + + + + + + + diff --git a/analysis2016.R b/analysis2016.R index bfe65c2..0594a4d 100644 --- a/analysis2016.R +++ b/analysis2016.R @@ -265,7 +265,7 @@ gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") gg <- gg + theme(axis.ticks=element_blank()) #gg <- gg + theme(axis.text = element_text(size = 4)) gg <- gg + labs(title = "Support for Clinton and Obama", - subtitle= "Percent of votes in 2012 compared 2016", + subtitle= "Percent of votes in 2012 compared to 2016", caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) @@ -384,7 +384,7 @@ gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") gg <- gg + theme(axis.ticks=element_blank()) #gg <- gg + theme(axis.text = element_text(size = 4)) gg <- gg + labs(title = "Support for Trump and Romney", - subtitle= "Percent of votes in 2012 compared 2016", + subtitle= "Percent of votes in 2012 compared to 2016", caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) @@ -472,6 +472,11 @@ colnames(race_towns_white) <- c("Town", "Percent_White") per <- left_join(per, race_towns_white) +per2 <- per %>% + mutate(Minorities=100-Percent_White) %>% + select(id, Minorities, Clinton, Trump) %>% + gather("candidate", "percent", 3:4) + gg <- ggplot(per, aes(Percent_White, Trump)) gg <- gg + geom_point() gg <- gg + labs(x="Percent white population", y="Percent votes for Trump", @@ -489,9 +494,78 @@ gg <- gg + theme(axis.line = element_blank(), gg <- gg + theme_bw() gg +per2 <- per %>% + mutate(Minorities=100-Percent_White) %>% + select(id, Percent_White, Clinton, Trump) %>% + gather("candidate", "percent", 3:4) +#%>% +# filter(percent > 50) # Swapped? +gg <- ggplot() +gg <- gg + geom_point(data=per2, aes(Percent_White, percent, color=candidate)) +gg <- gg + scale_color_manual(values= c("lightskyblue", "tomato")) +gg <- gg + facet_wrap(~candidate) +gg <- gg + scale_x_continuous(limits = c(0, 110)) +gg <- gg + scale_y_continuous(limits = c(0, 110)) +gg <- gg + labs(x="Percent white population", y="Percent of votes", + title="Votes compared to white population", + subtitle="The larger the white population, the higher the likelihood they supported Donald Trump.", + caption="Source: Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") +gg <- gg + theme_bw(base_family="Lato Regular") +gg <- gg + theme(axis.ticks.y=element_blank()) +#gg <- gg + theme(panel.border=element_blank()) +gg <- gg + theme(legend.key=element_blank()) +gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=22)) +gg <- gg + theme(plot.caption=element_text(face="bold", family="Lato Regular", size=9, color="gray28", margin=margin(t=10, r=80))) +gg <- gg + theme(legend.position="none") +gg <- gg + theme(strip.background=element_blank()) +gg <- gg + theme(strip.text.x = element_text(size = 10, colour = "grey5")) + +gg +ggsave(gg, file="output/white_population.png", width=8, height=5, type="cairo-png") +ftp_url <- paste0("ftp://", ftp_login, "@secure.ctmirror.org/projects.ctmirror.org/content/trend/2016/11/election/analysis/white_population.png") +ftpUpload("output/white_population.png", ftp_url) + +## Income? + + + +race_towns <- getCensus(name="acs5", + vintage=2014, + key=census_key, + vars=c("NAME", "B02001_001E", "B02001_002E", "B02001_003E", + "B02001_004E", "B02001_005E", "B03001_003E"), + region="county subdivision:*", regionin="state:09") + +colnames(race_towns) <- c("town", "state", "county", "countysub", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic") +race_towns <- race_towns[c("town", "total_pop", "White", "Black", "Indian", "Asian", "Hispanic")] +race_towns <- subset(race_towns, !grepl("County subdivisions", town)) +race_towns$town <- gsub(" town.*", "", race_towns$town) + +race_towns_long <- race_towns %>% + gather("race_ethnicity", "population", 3:7) %>% + mutate(percent_population=round(population/total_pop*100,2)) + +race_towns_white <- race_towns_long %>% + filter(race_ethnicity=="White") %>% + select(town, percent_population) + +colnames(race_towns_white) <- c("Town", "Percent_White") + +per <- left_join(per, race_towns_white) + +per2 <- per %>% + mutate(Minorities=100-Percent_White) %>% + select(id, Minorities, Clinton, Trump) %>% + gather("candidate", "percent", 3:4) + +``` + +## Flip map + + per$winner2012 <- ifelse(per$Obama>per$Romney, "Obama", "Romney") per$flip <- "" per$flip <- ifelse(per$winner=="Clinton" & per$winner2012=="Obama", "Clinton retained", per$flip) diff --git a/index.Rmd b/index.Rmd index 3982bb9..4c7e38c 100644 --- a/index.Rmd +++ b/index.Rmd @@ -278,7 +278,7 @@ gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") gg <- gg + theme(axis.ticks=element_blank()) #gg <- gg + theme(axis.text = element_text(size = 4)) gg <- gg + labs(title = "Support for Clinton and Obama", - subtitle= "Percent of votes in 2012 compared 2016", + subtitle= "Percent of votes in 2012 compared to 2016", caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) @@ -392,7 +392,7 @@ gg <- gg+ geom_vline(xintercept = 80, linetype="dotted", colour="lightgray") gg <- gg + theme(axis.ticks=element_blank()) #gg <- gg + theme(axis.text = element_text(size = 4)) gg <- gg + labs(title = "Support for Trump and Romney", - subtitle= "Percent of votes in 2012 compared 2016", + subtitle= "Percent of votes in 2012 compared to 2016", caption="Office of the Secretary of the State \n Andrew Ba Tran/TrendCT.org") gg <- gg + theme(plot.title=element_text(face="bold", family="Lato Regular", size=19)) gg <- gg + theme(plot.subtitle=element_text(family="Lato Regular", size=17)) diff --git a/output/hillary_obama_1.png b/output/hillary_obama_1.png index 79bc1c4..eb58003 100644 Binary files a/output/hillary_obama_1.png and b/output/hillary_obama_1.png differ diff --git a/output/hillary_obama_2.png b/output/hillary_obama_2.png index d58ccd8..89163ca 100644 Binary files a/output/hillary_obama_2.png and b/output/hillary_obama_2.png differ diff --git a/output/trump_romney_1.png b/output/trump_romney_1.png index 5e1dec4..8b82b7a 100644 Binary files a/output/trump_romney_1.png and b/output/trump_romney_1.png differ diff --git a/output/trump_romney_2.png b/output/trump_romney_2.png index e55329a..867c3bc 100644 Binary files a/output/trump_romney_2.png and b/output/trump_romney_2.png differ diff --git a/output/white_population.png b/output/white_population.png new file mode 100644 index 0000000..c9cd14a Binary files /dev/null and b/output/white_population.png differ diff --git a/per.csv b/per.csv new file mode 100644 index 0000000..3b9d198 --- /dev/null +++ b/per.csv @@ -0,0 +1 @@ +id,winner,winner2012,flip,percent1,percent2,town2 Woodstock,Trump,Romney,Trump retained,50.85,50.54,Woodstock Woodbridge,Clinton,Obama,Clinton retained,62.26,58.39,Woodbridge Wolcott,Trump,Romney,Trump retained,68.15,61.18,Wolcott Windsor Locks,Trump,Obama,Flipped to Trump,49.17,55.5,WindsorLocks Windsor,Clinton,Obama,Clinton retained,70.91,71.91,Windsor Windham,Clinton,Obama,Clinton retained,64.1,73.88,Windham Winchester,Trump,Romney,Trump retained,58.45,51.05,Winchester Wilton,Clinton,Romney,Flipped to Clinton,58.29,53.62,Wilton Willington,Clinton,Obama,Clinton retained,47.2,54.35,Willington Wethersfield,Clinton,Obama,Clinton retained,54.58,55.63,Wethersfield Westport,Clinton,Obama,Clinton retained,68.81,56.45,Westport Westbrook,Trump,Obama,Flipped to Trump,51.05,53.56,Westbrook West Haven,Clinton,Obama,Clinton retained,59.5,70.45,WestHaven West Hartford,Clinton,Obama,Clinton retained,71.41,65.99,WestHartford Watertown,Trump,Romney,Trump retained,65.15,60.62,Watertown Waterford,Clinton,Obama,Clinton retained,48.98,56.29,Waterford Waterbury,Clinton,Obama,Clinton retained,58.94,64.92,Waterbury Washington,Clinton,Obama,Clinton retained,59.65,59.41,Washington Warren,Trump,Romney,Trump retained,52.56,49.21,Warren Wallingford,Trump,Obama,Flipped to Trump,48.33,54.83,Wallingford Voluntown,Trump,Obama,Flipped to Trump,62.24,49.27,Voluntown Vernon,Clinton,Obama,Clinton retained,52.25,58.88,Vernon Union,Trump,Obama,Flipped to Trump,55.85,49.81,Union Torrington,Trump,Romney,Trump retained,57.71,50.77,Torrington Tolland,Clinton,Obama,Clinton retained,47.3,51.14,Tolland Thompson,Trump,Romney,Trump retained,58.7,49.21,Thompson Thomaston,Trump,Romney,Trump retained,65.88,56.96,Thomaston Suffield,Trump,Romney,Trump retained,50.13,53.13,Suffield Stratford,Clinton,Obama,Clinton retained,54.37,58.54,Stratford Stonington,Clinton,Obama,Clinton retained,55.4,58.51,Stonington Sterling,Trump,Romney,Trump retained,65.44,50.82,Sterling Stafford,Trump,Obama,Flipped to Trump,55.32,53.66,Stafford Sprague,Trump,Obama,Flipped to Trump,57.33,51.75,Sprague Southington,Trump,Obama,Flipped to Trump,53.1,49.98,Southington Southbury,Trump,Romney,Trump retained,52.98,54.8,Southbury South Windsor,Clinton,Obama,Clinton retained,54.9,56.43,SouthWindsor Somers,Trump,Romney,Trump retained,58.83,57.36,Somers Simsbury,Clinton,Obama,Clinton retained,55.94,49.87,Simsbury Sherman,Trump,Romney,Trump retained,49.86,50.05,Sherman Shelton,Trump,Romney,Trump retained,57.78,54.63,Shelton Sharon,Clinton,Obama,Clinton retained,57.91,62.64,Sharon Seymour,Trump,Romney,Trump retained,58.79,51.29,Seymour Scotland,Trump,Romney,Trump retained,56.99,49.7,Scotland Salisbury,Clinton,Obama,Clinton retained,71.81,69.19,Salisbury Salem,Trump,Obama,Flipped to Trump,50.38,51.36,Salem Roxbury,Clinton,Obama,Clinton retained,51.9,51.32,Roxbury Rocky Hill,Clinton,Obama,Clinton retained,51.74,55.57,RockyHill Ridgefield,Clinton,Romney,Flipped to Clinton,55.32,52.74,Ridgefield Redding,Clinton,Obama,Clinton retained,56.99,52.08,Redding Putnam,Trump,Obama,Flipped to Trump,50.45,53.76,Putnam Prospect,Trump,Romney,Trump retained,66.27,62.83,Prospect Preston,Trump,Romney,Trump retained,54.77,49.56,Preston Portland,Clinton,Obama,Clinton retained,47.47,54.65,Portland Pomfret,Clinton,Obama,Clinton retained,50.33,51.33,Pomfret Plymouth,Trump,Romney,Trump retained,64.84,50.69,Plymouth Plainville,Trump,Obama,Flipped to Trump,51.73,57.04,Plainville Plainfield,Trump,Obama,Flipped to Trump,57.39,54.26,Plainfield Oxford,Trump,Romney,Trump retained,63.8,59.19,Oxford Orange,Trump,Romney,Trump retained,50.45,53.18,Orange Old Saybrook,Clinton,Obama,Clinton retained,51.52,52.42,OldSaybrook Old Lyme,Clinton,Obama,Clinton retained,52.98,56.15,OldLyme Norwich,Clinton,Obama,Clinton retained,55.31,66.09,Norwich Norwalk,Clinton,Obama,Clinton retained,63.82,63.01,Norwalk North Stonington,Trump,Obama,Flipped to Trump,51.49,50.79,NorthStonington North Haven,Trump,Obama,Flipped to Trump,53.68,51,NorthHaven North Canaan,Clinton,Obama,Clinton retained,48.51,55.29,NorthCanaan North Branford,Trump,Obama,Flipped to Trump,58.44,50.13,NorthBranford Norfolk,Clinton,Obama,Clinton retained,35.5,58.96,Norfolk Newington,Clinton,Obama,Clinton retained,53.65,59.65,Newington New London,Clinton,Obama,Clinton retained,72.58,78.94,NewLondon New Haven,Clinton,Obama,Clinton retained,84.33,89.3,NewHaven New Hartford,Trump,Romney,Trump retained,54.57,52.98,NewHartford New Canaan,Clinton,Romney,Flipped to Clinton,52.2,64.13,NewCanaan New Britain,Clinton,Obama,Clinton retained,69.26,76.32,NewBritain Naugatuck,Trump,Obama,Flipped to Trump,55.45,49.7,Naugatuck Morris,Trump,Romney,Trump retained,59.1,57.14,Morris Montville,Trump,Obama,Flipped to Trump,52.13,55.52,Montville Monroe,Trump,Romney,Trump retained,54.29,55.7,Monroe Milford,Clinton,Obama,Clinton retained,48.29,53.65,Milford Middletown,Clinton,Obama,Clinton retained,61,68.41,Middletown Middlefield,Trump,Obama,Flipped to Trump,51.63,52.54,Middlefield Middlebury,Trump,Romney,Trump retained,58.51,62.17,Middlebury Marlborough,Trump,Obama,Flipped to Trump,48.53,51.04,Marlborough Mansfield,Clinton,Obama,Clinton retained,72.51,74.13,Mansfield Madison,Clinton,Obama,Clinton retained,54.38,50.25,Madison Lyme,Clinton,Obama,Clinton retained,60.26,58.11,Lyme Litchfield,Trump,Romney,Trump retained,50.59,51.9,Litchfield Lisbon,Trump,Obama,Flipped to Trump,53.87,54.88,Lisbon Lebanon,Trump,Obama,Flipped to Trump,53.57,52.14,Lebanon Killingworth,Trump,Obama,Flipped to Trump,48.39,50.34,Killingworth Killingly,Trump,Obama,Flipped to Trump,56.85,53.96,Killingly Kent,Clinton,Obama,Clinton retained,63,63.31,Kent Hebron,Clinton,Obama,Clinton retained,47.89,50.67,Hebron Harwinton,Trump,Romney,Trump retained,62.16,59.31,Harwinton Hartland,Trump,Romney,Trump retained,64.94,64.56,Hartland Hartford,Clinton,Obama,Clinton retained,90.25,93.24,Hartford Hampton,Trump,Obama,Flipped to Trump,48.06,58.41,Hampton Hamden,Clinton,Obama,Clinton retained,68.47,71.3,Hamden Haddam,Trump,Obama,Flipped to Trump,51.52,50.5,Haddam Guilford,Clinton,Obama,Clinton retained,59.03,58.39,Guilford Groton,Clinton,Obama,Clinton retained,55.07,60.04,Groton Griswold,Trump,Obama,Flipped to Trump,57.08,53.26,Griswold Greenwich,Clinton,Romney,Flipped to Clinton,56.89,55.24,Greenwich Granby,Clinton,Romney,Flipped to Clinton,47.84,50.5,Granby Goshen,Trump,Romney,Trump retained,58.32,56.91,Goshen Glastonbury,Clinton,Obama,Clinton retained,56.19,52.88,Glastonbury Franklin,Trump,Obama,Flipped to Trump,55.88,49.19,Franklin Farmington,Clinton,Obama,Clinton retained,53.37,50.89,Farmington Fairfield,Clinton,Obama,Clinton retained,57.23,51.05,Fairfield Essex,Clinton,Obama,Clinton retained,55.99,56.1,Essex Enfield,Trump,Obama,Flipped to Trump,48.96,55.92,Enfield Ellington,Trump,Romney,Trump retained,51.19,51.2,Ellington Easton,Clinton,Romney,Flipped to Clinton,48.63,55.79,Easton Eastford,Trump,Romney,Trump retained,53.33,49.1,Eastford East Lyme,Clinton,Obama,Clinton retained,53.98,57.64,EastLyme East Haven,Trump,Obama,Flipped to Trump,54.4,57.8,EastHaven East Hartford,Clinton,Obama,Clinton retained,69.34,75.04,EastHartford East Hampton,Trump,Obama,Flipped to Trump,50.35,52.08,EastHampton East Haddam,Trump,Obama,Flipped to Trump,48.89,53,EastHaddam East Granby,Clinton,Romney,Flipped to Clinton,47.08,50.31,EastGranby Durham,Trump,Romney,Trump retained,50.84,49.67,Durham Derby,Trump,Obama,Flipped to Trump,51.48,55.08,Derby Deep River,Clinton,Obama,Clinton retained,54.18,60.79,DeepRiver Darien,Clinton,Romney,Flipped to Clinton,52.76,65.39,Darien Cromwell,Clinton,Obama,Clinton retained,50.09,55.29,Cromwell Cornwall,Clinton,Obama,Clinton retained,67.39,71.23,Cornwall Columbia,Clinton,Obama,Clinton retained,47.39,55.74,Columbia Colebrook,Trump,Romney,Trump retained,57.58,53.97,Colebrook Colchester,Trump,Obama,Flipped to Trump,47.12,52.56,Colchester Clinton,Clinton,Obama,Clinton retained,48.57,54.72,Clinton Chester,Clinton,Obama,Clinton retained,60.05,65.43,Chester Cheshire,Clinton,Obama,Clinton retained,48.63,49.66,Cheshire Chaplin,Trump,Obama,Flipped to Trump,54.21,54.38,Chaplin Canton,Clinton,Obama,Clinton retained,51.65,51.23,Canton Canterbury,Trump,Romney,Trump retained,59.33,50.88,Canterbury Canaan,Clinton,Obama,Clinton retained,60.2,63.37,Canaan Burlington,Trump,Romney,Trump retained,51.49,52.63,Burlington Brooklyn,Trump,Obama,Flipped to Trump,55.16,51.55,Brooklyn Brookfield,Trump,Romney,Trump retained,53.57,55.11,Brookfield Bristol,Trump,Obama,Flipped to Trump,48.26,57.91,Bristol Bridgewater,Trump,Romney,Trump retained,50.04,54.51,Bridgewater Branford,Clinton,Obama,Clinton retained,54.17,58.2,Branford Bozrah,Trump,Obama,Flipped to Trump,54.96,50.31,Bozrah Bolton,Trump,Romney,Trump retained,47.83,49.26,Bolton Bethlehem,Trump,Romney,Trump retained,60.64,58.3,Bethlehem Bethel,Clinton,Obama,Clinton retained,48.84,51.18,Bethel Bethany,Trump,Obama,Flipped to Trump,48.7,50.63,Bethany Berlin,Trump,Obama,Flipped to Trump,53.53,50.06,Berlin Beacon Falls,Trump,Romney,Trump retained,60.08,54,BeaconFalls Barkhamsted,Trump,Romney,Trump retained,52.59,49.07,Barkhamsted Avon,Clinton,Romney,Flipped to Clinton,53.4,52.9,Avon Ashford,Clinton,Obama,Clinton retained,47.68,61.25,Ashford Ansonia,Trump,Obama,Flipped to Trump,48.38,61.33,Ansonia Andover,Clinton,Obama,Clinton retained,46.86,55.08,Andover \ No newline at end of file diff --git a/per2.csv b/per2.csv new file mode 100644 index 0000000..d24429d --- /dev/null +++ b/per2.csv @@ -0,0 +1 @@ +id,winner,winner2012,flip,percent1,percent2,town2 Woodstock,Trump,Romney,Trump retained,50.85,50.54,Woodstock Woodbury,Trump,Romney,Trump retained,53.89,55.06,Woodbury Woodbridge,Clinton,Obama,Clinton retained,62.26,58.39,Woodbridge Wolcott,Trump,Romney,Trump retained,68.15,61.18,Wolcott Windsor Locks,Trump,Obama,Flipped to Trump,49.17,55.5,WindsorLocks Windsor,Clinton,Obama,Clinton retained,70.91,71.91,Windsor Windham,Clinton,Obama,Clinton retained,64.1,73.88,Windham Winchester,Trump,Romney,Trump retained,58.45,51.05,Winchester Wilton,Clinton,Romney,Flipped to Clinton,58.29,53.62,Wilton Willington,Clinton,Obama,Clinton retained,46.48,54.35,Willington Wethersfield,Clinton,Obama,Clinton retained,54.58,55.63,Wethersfield Westport,Clinton,Obama,Clinton retained,68.81,56.45,Westport Weston,Clinton,Obama,Clinton retained,66.66,53.27,Weston Westbrook,Trump,Obama,Flipped to Trump,51.05,53.56,Westbrook West Haven,Clinton,Obama,Clinton retained,59.5,70.45,WestHaven West Hartford,Clinton,Obama,Clinton retained,71.32,65.99,WestHartford Watertown,Trump,Romney,Trump retained,65.05,60.62,Watertown Waterford,Clinton,Obama,Clinton retained,48.98,56.29,Waterford Waterbury,Clinton,Obama,Clinton retained,58.94,64.92,Waterbury Washington,Clinton,Obama,Clinton retained,59.65,59.41,Washington Warren,Trump,Romney,Trump retained,52.56,49.21,Warren Wallingford,Trump,Obama,Flipped to Trump,48.33,54.83,Wallingford Voluntown,Trump,Obama,Flipped to Trump,62.24,49.27,Voluntown Vernon,Clinton,Obama,Clinton retained,52.25,58.88,Vernon Union,Trump,Obama,Flipped to Trump,55.85,49.81,Union Trumbull,Trump,Romney,Trump retained,49.14,52.92,Trumbull Torrington,Trump,Romney,Trump retained,57.71,50.77,Torrington Tolland,Clinton,Obama,Clinton retained,47.3,51.14,Tolland Thompson,Trump,Romney,Trump retained,58.7,49.21,Thompson Thomaston,Trump,Romney,Trump retained,65.88,56.96,Thomaston Suffield,Trump,Romney,Trump retained,50.13,53.13,Suffield Stratford,Clinton,Obama,Clinton retained,54.37,58.54,Stratford Stonington,Clinton,Obama,Clinton retained,55.4,58.51,Stonington Sterling,Trump,Romney,Trump retained,65.44,50.82,Sterling Stamford,Clinton,Obama,Clinton retained,65.39,62.33,Stamford Stafford,Trump,Obama,Flipped to Trump,55.32,53.66,Stafford Sprague,Trump,Obama,Flipped to Trump,57.26,51.75,Sprague Southington,Trump,Obama,Flipped to Trump,53.1,49.98,Southington Southbury,Trump,Romney,Trump retained,52.98,54.8,Southbury South Windsor,Clinton,Obama,Clinton retained,55.31,56.43,SouthWindsor Somers,Trump,Romney,Trump retained,58.83,57.36,Somers Simsbury,Clinton,Obama,Clinton retained,55.94,49.87,Simsbury Sherman,Trump,Romney,Trump retained,49.84,50.05,Sherman Shelton,Trump,Romney,Trump retained,57.78,54.63,Shelton Sharon,Clinton,Obama,Clinton retained,57.91,62.64,Sharon Seymour,Trump,Romney,Trump retained,58.79,51.29,Seymour Scotland,Trump,Romney,Trump retained,56.99,49.7,Scotland Salisbury,Clinton,Obama,Clinton retained,71.69,69.19,Salisbury Salem,Trump,Obama,Flipped to Trump,50.38,51.36,Salem Roxbury,Clinton,Obama,Clinton retained,51.9,51.32,Roxbury Rocky Hill,Clinton,Obama,Clinton retained,51.74,55.57,RockyHill Ridgefield,Clinton,Romney,Flipped to Clinton,55.32,52.74,Ridgefield Redding,Clinton,Obama,Clinton retained,56.99,52.08,Redding Putnam,Trump,Obama,Flipped to Trump,50.45,53.76,Putnam Prospect,Trump,Romney,Trump retained,66.27,62.83,Prospect Preston,Trump,Romney,Trump retained,54.77,49.56,Preston Portland,Clinton,Obama,Clinton retained,47.47,54.65,Portland Pomfret,Clinton,Obama,Clinton retained,50.33,51.33,Pomfret Plymouth,Trump,Romney,Trump retained,64.84,50.69,Plymouth Plainville,Trump,Obama,Flipped to Trump,51.64,57.04,Plainville Plainfield,Trump,Obama,Flipped to Trump,57.39,54.26,Plainfield Oxford,Trump,Romney,Trump retained,63.8,59.19,Oxford Orange,Trump,Romney,Trump retained,50.45,53.18,Orange Old Saybrook,Clinton,Obama,Clinton retained,51.52,52.42,OldSaybrook Old Lyme,Clinton,Obama,Clinton retained,52.98,56.15,OldLyme Norwich,Clinton,Obama,Clinton retained,55.31,66.09,Norwich Norwalk,Clinton,Obama,Clinton retained,63.82,63.01,Norwalk North Stonington,Trump,Obama,Flipped to Trump,51.49,50.79,NorthStonington North Haven,Trump,Obama,Flipped to Trump,53.68,51,NorthHaven North Canaan,Clinton,Obama,Clinton retained,48.51,55.29,NorthCanaan North Branford,Trump,Obama,Flipped to Trump,58.44,50.13,NorthBranford Norfolk,Clinton,Obama,Clinton retained,35.5,58.96,Norfolk Newington,Clinton,Obama,Clinton retained,53.65,59.65,Newington New London,Clinton,Obama,Clinton retained,72.58,78.94,NewLondon New Haven,Clinton,Obama,Clinton retained,86.02,89.3,NewHaven New Hartford,Trump,Romney,Trump retained,54.57,52.98,NewHartford New Fairfield,Trump,Romney,Trump retained,56.02,55.7,NewFairfield New Canaan,Clinton,Romney,Flipped to Clinton,52.2,64.13,NewCanaan New Britain,Clinton,Obama,Clinton retained,69.26,76.32,NewBritain Naugatuck,Trump,Obama,Flipped to Trump,55.45,49.7,Naugatuck Morris,Trump,Romney,Trump retained,59.1,57.14,Morris Montville,Trump,Obama,Flipped to Trump,52.13,55.52,Montville Monroe,Trump,Romney,Trump retained,54.29,55.7,Monroe Milford,Clinton,Obama,Clinton retained,48.29,53.65,Milford Middletown,Clinton,Obama,Clinton retained,61,68.41,Middletown Middlefield,Trump,Obama,Flipped to Trump,51.63,52.54,Middlefield Middlebury,Trump,Romney,Trump retained,58.51,62.17,Middlebury Meriden,Clinton,Obama,Clinton retained,56.98,67.65,Meriden Marlborough,Trump,Obama,Flipped to Trump,48.53,51.04,Marlborough Mansfield,Clinton,Obama,Clinton retained,72.51,74.13,Mansfield Manchester,Clinton,Obama,Clinton retained,60.87,65.21,Manchester Madison,Clinton,Obama,Clinton retained,54.38,50.25,Madison Lyme,Clinton,Obama,Clinton retained,60.26,58.11,Lyme Litchfield,Trump,Romney,Trump retained,50.59,51.9,Litchfield Lisbon,Trump,Obama,Flipped to Trump,53.87,54.88,Lisbon Ledyard,Trump,Obama,Flipped to Trump,48.37,51.65,Ledyard Lebanon,Trump,Obama,Flipped to Trump,53.57,52.14,Lebanon Killingworth,Trump,Obama,Flipped to Trump,48.39,50.34,Killingworth Killingly,Trump,Obama,Flipped to Trump,56.85,53.96,Killingly Kent,Clinton,Obama,Clinton retained,63,63.31,Kent Hebron,Clinton,Obama,Clinton retained,47.89,50.67,Hebron Harwinton,Trump,Romney,Trump retained,61.84,59.31,Harwinton Hartland,Trump,Romney,Trump retained,64.94,64.56,Hartland Hartford,Clinton,Obama,Clinton retained,90.25,93.24,Hartford Hampton,Trump,Obama,Flipped to Trump,48.06,58.41,Hampton Hamden,Clinton,Obama,Clinton retained,68.47,71.3,Hamden Haddam,Trump,Obama,Flipped to Trump,51.43,50.5,Haddam Guilford,Clinton,Obama,Clinton retained,59.03,58.39,Guilford Groton,Clinton,Obama,Clinton retained,55.07,60.04,Groton Griswold,Trump,Obama,Flipped to Trump,57.08,53.26,Griswold Greenwich,Clinton,Romney,Flipped to Clinton,56.89,55.24,Greenwich Granby,Clinton,Romney,Flipped to Clinton,47.84,50.5,Granby Goshen,Trump,Romney,Trump retained,58.32,56.91,Goshen Glastonbury,Clinton,Obama,Clinton retained,56.21,52.88,Glastonbury Franklin,Trump,Obama,Flipped to Trump,55.88,49.19,Franklin Farmington,Clinton,Obama,Clinton retained,53.37,50.89,Farmington Fairfield,Clinton,Obama,Clinton retained,57.23,51.05,Fairfield Essex,Clinton,Obama,Clinton retained,55.99,56.1,Essex Enfield,Trump,Obama,Flipped to Trump,48.96,55.92,Enfield Easton,Clinton,Romney,Flipped to Clinton,48.69,55.79,Easton Eastford,Trump,Romney,Trump retained,53.33,49.1,Eastford East Lyme,Clinton,Obama,Clinton retained,53.98,57.64,EastLyme East Hartford,Clinton,Obama,Clinton retained,69.34,75.04,EastHartford East Hampton,Trump,Obama,Flipped to Trump,50.35,52.08,EastHampton East Haddam,Trump,Obama,Flipped to Trump,48.89,53,EastHaddam East Granby,Clinton,Romney,Flipped to Clinton,47.08,50.31,EastGranby Durham,Trump,Romney,Trump retained,50.84,49.67,Durham Derby,Trump,Obama,Flipped to Trump,51.48,55.08,Derby Deep River,Clinton,Obama,Clinton retained,54.18,60.79,DeepRiver Darien,Clinton,Romney,Flipped to Clinton,52.75,65.39,Darien Cromwell,Clinton,Obama,Clinton retained,50.09,55.29,Cromwell Coventry,Clinton,Obama,Clinton retained,46.27,56.29,Coventry Cornwall,Clinton,Obama,Clinton retained,67.39,71.23,Cornwall Columbia,Clinton,Obama,Clinton retained,47.44,55.74,Columbia Colebrook,Trump,Romney,Trump retained,58.8,53.97,Colebrook Colchester,Trump,Obama,Flipped to Trump,47.12,52.56,Colchester Clinton,Clinton,Obama,Clinton retained,48.57,54.72,Clinton Chester,Clinton,Obama,Clinton retained,60.05,65.43,Chester Cheshire,Clinton,Obama,Clinton retained,48.63,49.66,Cheshire Chaplin,Trump,Obama,Flipped to Trump,54.21,54.38,Chaplin Canton,Clinton,Obama,Clinton retained,51.65,51.23,Canton Canterbury,Trump,Romney,Trump retained,59.33,50.88,Canterbury Canaan,Clinton,Obama,Clinton retained,60.3,63.37,Canaan Burlington,Trump,Romney,Trump retained,51.49,52.63,Burlington Brooklyn,Trump,Obama,Flipped to Trump,55.16,51.55,Brooklyn Brookfield,Trump,Romney,Trump retained,53.57,55.11,Brookfield Bristol,Trump,Obama,Flipped to Trump,48.26,57.91,Bristol Bridgewater,Trump,Romney,Trump retained,50.04,54.51,Bridgewater Branford,Clinton,Obama,Clinton retained,54.17,58.2,Branford Bozrah,Trump,Obama,Flipped to Trump,54.96,50.31,Bozrah Bolton,Trump,Romney,Trump retained,47.83,49.26,Bolton Bloomfield,Clinton,Obama,Clinton retained,82.8,83.4,Bloomfield Bethlehem,Trump,Romney,Trump retained,60.64,58.3,Bethlehem Bethel,Clinton,Obama,Clinton retained,48.84,51.18,Bethel Bethany,Trump,Obama,Flipped to Trump,48.7,50.63,Bethany Berlin,Trump,Obama,Flipped to Trump,53.53,50.06,Berlin Beacon Falls,Trump,Romney,Trump retained,60.08,54,BeaconFalls Barkhamsted,Trump,Romney,Trump retained,52.59,49.07,Barkhamsted Avon,Clinton,Romney,Flipped to Clinton,53.33,52.9,Avon Ashford,Clinton,Obama,Clinton retained,47.68,61.25,Ashford Ansonia,Clinton,Obama,Clinton retained,51.49,61.33,Ansonia \ No newline at end of file