-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.Rmd
164 lines (127 loc) · 5.09 KB
/
Markdown.Rmd
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---
title: "Final Project: Rough Draft"
output:
html_document: default
pdf_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(verbose=FALSE, echo=FALSE, warning=FALSE, message=FALSE)
library(tidyverse)
library(ggmap)
library(maps)
library(mapdata)
library(rvest)
library(stringr)
library(tilegramsR)
library(sf)
library(leaflet)
library(leaflet.extras)
library(colormap)
```
1966 - 1968 Congressional Data Set
The data I am trying to visualize comes from my thesis data set that The general idea behind this visualization is to produce an interactive visualization in order to perform some exploratory data analysis.
```{r}
#Reading in My Data Set
congress <- readxl::read_excel("Congress Data for R.xlsx")
state <- map_data("state")
#Allows for Zoom Options
getLeafletOptions <- function(minZoom, maxZoom, ...) {
leafletOptions(
crs = leafletCRS("L.CRS.Simple"),
minZoom = minZoom, maxZoom = maxZoom,
dragging = TRUE, zoomControl = TRUE,
tap = TRUE,
attributionControl = TRUE , ...)
}
```
```{r}
#1966 Congressional Representatives
demo1 <- congress %>%
filter(`ELECTED 67` == 0) %>%
group_by(STATE2) %>%
count(PARTYN) %>%
mutate(perc = (n/sum(n)))%>%
spread(key= PARTYN, value = perc)%>%
select(STATE2, ds, dn, rr)
#Get Rid of the NAs
demo1[is.na(demo1)] <- 0
#Merge Southern Democrats and Northern Democrats
demo1 <- demo1 %>%
mutate(dd = ds + dn) %>%
select(STATE2, dd, rr)
#My Leaf Color Function (Change Bin Size to Show More Moderates)
pal <- colorBin(c("red", "purple", "blue"),0:1, bins = 2, 0:1, reverse = TRUE, pretty = FALSE)
#Remove DC from the Polygon Set
sf_NPR1to1 <- sf_NPR1to1%>%
filter(state != "DC")
sf_NPR1to1.centers <- sf_NPR1to1.centers %>%
filter(state != "DC")
leaflet(
sf_NPR1to1, #the dataset for the Map Outline and State Position
options= getLeafletOptions(-1.5, -.5)) %>% #Zoom Options
addPolygons( #Creating the Polygons and the Features of the Polygons
weight=1.5,color="black", group = 'states', #separation between states
fillOpacity = .8, opacity = 1, fillColor = pal(demo1$dd), #aesthetics of polygons
highlightOptions = highlightOptions(weight = 4)) %>% #not100%sure
addLabelOnlyMarkers( #adding the labels
data=sf_NPR1to1.centers, #need the labels to be in the center of the polygons
label = ~as.character(state), #what is being put in the center of the polygon
labelOptions = labelOptions( #label options, need to review
noHide = 'T', textOnly = T,
offset=c(-4,-10), textsize = '11px')) %>%
setMapWidgetStyle() #sets the maps CSS key/value properties
```
1967 visualization
```{r}
demo2 <- congress %>%
filter(`ELECTED 67` == 0) %>%
group_by(STATE2) %>%
count(PARTYN) %>%
mutate(perc = (n/sum(n)))%>%
spread(key= PARTYN, value = perc)%>%
select(STATE2, ds, dn, rr)
#Get Rid of the NAs
demo2[is.na(demo2)] <- 0
#Merge Southern Democrats and Northern Democrats
demo2 <- demo2 %>%
mutate(dd = ds + dn) %>%
select(STATE2, dd, rr)
#My Leaf Color Function (Change Bin Size to Show More Moderates)
pal <- colorBin(c("red", "purple", "blue"),0:1, bins = 3, 0:1, reverse = TRUE, pretty = FALSE)
#Bin ChangeD
#Remove DC from the Polygon Set
sf_NPR1to1 <- sf_NPR1to1%>%
filter(state != "DC")
sf_NPR1to1.centers <- sf_NPR1to1.centers %>%
filter(state != "DC")
leaflet(
sf_NPR1to1, #the dataset for the Map Outline and State Position
options= getLeafletOptions(-1.5, -.5)) %>% #Zoom Options
addPolygons( #Creating the Polygons and the Features of the Polygons
weight=1.5,color="black", group = 'states', #separation between states
fillOpacity = .8, opacity = 1, fillColor = pal(demo2$dd), #aesthetics of polygons
highlightOptions = highlightOptions(weight = 4)) %>% #not100%sure
addLabelOnlyMarkers( #adding the labels
data=sf_NPR1to1.centers, #need the labels to be in the center of the polygons
label = ~as.character(state), #what is being put in the center of the polygon
labelOptions = labelOptions( #label options, need to review
noHide = 'T', textOnly = T,
offset=c(-4,-10), textsize = '11px')) %>%
setMapWidgetStyle() #sets the maps CSS key/value properties
```
The general direction that I am planning on taking this visualization is to make it more interactive. There are the general hover features and shiny input features that allow for the audience to filter the dataset. I am currently working on trying to figure out if it is possible for a person to click on a state polygon and for that to be a method for the audience to filter by state which would then provide more polygons that represent individual representatives and provide voting behavior(?) or general demographics. I would greatly appreciate any feedback or suggestions you have to offer.
```{r}
#LOL
st <- state %>%
group_by(region) %>%
mutate(ymin = min(lat)) %>%
mutate(ymax = max(lat)) %>%
mutate(xmin = min(long)) %>%
mutate(xmax = max(long)) %>%
select(region, ymin, ymax, xmin, xmax) %>%
unique()
ggplot(data = st) +
geom_rect(aes(ymin = ymin, ymax = ymax, xmin= xmin, xmax= xmax, fill = region), color = "white")+
coord_fixed(1.3)+
guides(fill = FALSE)
```