-
Notifications
You must be signed in to change notification settings - Fork 0
/
ranking.R
102 lines (77 loc) · 2.3 KB
/
ranking.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
require(ggplot2)
require(RColorBrewer)
require(maps)
require(SDMTools)
partyCode <- "L"
years <- c("2016")
officeCodes <-
c("AA", "BA", "DA", "EA", "FA", "GA", "HA", "IA")
prefixes <- list()
prefixes[[years[1]]] <- "/23-"
countylabels <-
read.csv(
"data/countylabels.csv",
stringsAsFactors = FALSE,
header = TRUE,
colClasses = c(rep("character", 11))
)
countyData <- list()
for (year in years) {
countyData[[year]] <-
read.csv(
file = paste("data/", year, prefixes[[year]], "StateCountyTotals.csv", sep = ""),
stringsAsFactors = FALSE
)
}
ranks <- countylabels[6]
colnames(ranks)[1] <- "County"
countyResults <- countyData[[year]]
countyResults <-
countyResults[countyResults$OfficialResults == "Yes",]
countyResults <- countyResults[countyResults$PartyCode == partyCode, ]
countyResults <- countyResults[countyResults$Type == "County",]
countyResults$Percentage <- as.numeric(substr(countyResults$Percentage, 1, nchar(countyResults$Percentage)-1))
for (officeCode in officeCodes)
{
data <- countyResults[countyResults$OfficeCode == officeCode, ]
if(nrow(data) == 0)
{
next
}
data <- data[order(data$Percentage),]
data$Order <- c(55:1)
data <- data[order(data$CountyName),]
ranks <- cbind(ranks, data$Order)
colnames(ranks)[ncol(ranks)] <- unique(data$Name)
}
ranks$AVG <- apply(X = ranks[,2:8], MARGIN = 1, FUN = mean)
ranks$AVG <- round(x = ranks$AVG, digits = 2)
n <- 5
colorscale <- rev(brewer.pal(n = n, name = "Blues"))
data <- ranks[c(1,9)]
data$Bucket <- 0
data <- data[order(data$AVG), ]
for (i in 1:n)
{
min <- 1 + (i-1)*55/n
max <- 55/n + (i-1)*55/n
data[min:max,]$Bucket <- colorscale[i]
}
data <- data[order(data$County), ]
mypath <- paste("graphs/Graphtotal",
year,
".png",
sep = "")
png(filename = mypath)
data$County <- paste("west virginia,",tolower(data$County))
z <- data.frame(map('county','west virginia')$names)
m <-
map('county',
'west virginia',
col = data[z[,1],]$Bucket,
fill = TRUE)
title <-
paste(year,"Weighted Results")
title(main = title)
dev.off()
write.csv(x = ranks, "data/ranks.csv", row.names = FALSE)