-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_portfolio.Rmd
433 lines (381 loc) · 14 KB
/
example_portfolio.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
---
title: "PS811 Portfolio"
author: "Devin Judge-Lord"
output:
html_document:
toc: true ## table of contents
code_folding: hide
---
```{r setup, include=FALSE}
## Defaults for R chunks
knitr::opts_chunk$set(echo = TRUE, ## echo = TRUE means code will show
warning=FALSE, ## supress warnings and messages from R
message=FALSE,
fig.path='Figs/', ## where to save figures
fig.height = 3, ## default figure size (can differ in each chunk)
fig.width = 3)
## Add any R packages you require.
## Here are some we will use in 811:
requires <- c("tidyverse", ## tidyverse includes dplyr and ggplot2
"magrittr",
"here")
## Install any you don't have
to_install <- c(requires %in% rownames(installed.packages()) == FALSE)
install.packages(c(requires[to_install], "NA"), repos = "https://cloud.r-project.org/" )
## Load all required R packages
library(tidyverse)
library(ggplot2); theme_set(theme_minimal())
library(magrittr)
library(here)
```
<!-- The above header sets everything up. -->
<!-- Replace the below with bit about you. -->
### Research Interests
I am interested in policy made by unelected institutions within democracies, especially how activists and social movements influence bureaucratic policymaking in the United States.
Methodologically, this means that I am trying to figure out how to measure policy change and identify its causes.
### Questions I hope to answer
How do we distinguish grassroots and astroturf activism? Does either affect policy? When and why do Members of Congress lobby federal agencies? How do we measure policy change and attribute influence?
---
### Methods
Text analysis (reuse, topic models and other Bayesian classifiers, content analysis), various regression models including duration models, field experiments, and process tracing.
---
### Relevent data
- Adam Bonica's DIME database of campaign contributions ( [data](https://data.stanford.edu/dime_) )
- Jennifer Selin's data on agency inpendence ( [article](https://onlinelibrary.wiley.com/doi/abs/10.1111/ajps.12161) )
- Charles Stewart's congressional committee membership data ( [data](http://web.mit.edu/17.251/www/data_page.html) )
- U.S. Census
- Mark Richards and Bert Kritzer's data on supreme court decisionmaking ( [article](https://www.cambridge.org/core/journals/american-political-science-review/article/jurisprudential-regimes-in-supreme-court-decision-making/E6367C1260F08FBD798DE09992E56534) )
---
# Example data viz:
## How much ideological diversity is there in U.S. state governments? Which state governments are all conservative? Which are all liberal. Which are mixed?
Using DIME ideology scores (based on campaign donations):
```{r DIME_data}
## load data from a web address
load(url("https://github.com/judgelord/state-level-DIME/raw/master/data/BonicaStateLevel.Rdata"))
d %<>%
## naming things!
rename(Governor = gov_cf) %>%
rename(Lower = l_cf_median) %>%
rename(Upper = u_cf_median) %>%
mutate(state_ideology = ifelse(dist_1_x_zero == 0 & Governor < 0, "All Liberal", "Mixed")) %>%
mutate(state_ideology = ifelse(dist_1_x_zero == 0 & Governor > 0, "All Conservative", state_ideology)) %>%
mutate(state_ideology = ifelse(is.na(dist_1_x_zero) & Governor > 0, "All Conservative", state_ideology)) %>%
## identify min and max DIME scores for each state (each row is a state)
mutate(min = pmin(Governor, Lower, Upper, na.rm = T),
max = pmax(Governor, Lower, Upper, na.rm = T) )
```
And a function for plotting them:
```{r DIME_plot_function}
## Define a function for plotting state-level DIME data with ggplot
DIMEplot <- function(d){
d %>%
# define y as state for all layers
ggplot(aes(y = reorder(state, distance_1_gov_leg_med))) +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = reorder(state, distance_1_gov_leg_med)),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3) +
# x = points for each actor
geom_point(aes(x = Governor, color = Governor), shape = "G") +
geom_point(aes(x = Lower, color = Lower), shape = "L") +
geom_point(aes(x = Upper, color = Upper), shape = "U") +
# labels
labs(y = "States by Preference Divergence",
x = "Mean Ideology Score",
title = paste("Preference Divergence of Political Principals,", unique(d$year) ), color = "") +
# scales
scale_x_continuous(breaks = c(-1, 0, 1)) +
scale_colour_gradient2(low = "blue", mid = "black", high = "red") +
# facet by liberal, conservative, or mixed
facet_grid(state_ideology ~ ., scales = "free_y", space = "free_y") +
# adjust text size and drop legend
theme(axis.text.y = element_text(size = 5),
title = element_text(size = 7),
legend.position = "none")
}
```
We can compare alignment among governors (G) and upper (U) and lower (L) chambers of state legislatures between 2004 and 2008:
```{r DIME-2003-2008-plots, fig.height = 5, fig.width = 3.1, fig.show='hold'}
d %>%
filter(year == 2004) %>%
DIMEplot()
d %>%
filter(year == 2008) %>%
DIMEplot()
```
## Step by step
### 1. Load trimmed-down DIME data from URL:
```{r data}
## load data from a web address
load(url("https://github.com/judgelord/state-level-DIME/raw/master/data/BonicaStateLevel.Rdata"))
```
### 2. Give variables better names
```{r namethings}
## with `rename()`
d %<>%
## naming things!
rename(Governor = gov_cf) %>%
rename(Lower = l_cf_median) %>%
rename(Upper = u_cf_median)
```
### 3. Make new variables
```{r minandmax}
## with `mutate()`
d %<>%
## identify min and max DIME scores for each state (each row is a state)
mutate(min = pmin(Governor, Lower, Upper, na.rm = T),
max = pmax(Governor, Lower, Upper, na.rm = T),
distance = pmax(
abs(Governor-Lower),
abs(Governor-Upper),
abs(Upper-Lower)
) )
```
### 4. Make a new variable indicating "All Liberal," "All Conservative," or "Mixed"
```{r state_ideologyvar}
## with `mutate()` and `ifelse()`
d %<>%
mutate(state_ideology = ifelse(min < 0 & max < 0,
"All Liberal",
ifelse(min > 0 & max > 0,
"All Conservative",
"Mixed")
)) %>%
mutate(state_ideology = ifelse(
(min > 0 & max < 0) | (min > 0 & max < 0),
"Mixed", state_ideology))
```
### 5. Plot
A `geom_point()` layer (x = Governor, y = State):
```{r point, fig.height = 5, fig.width = 3.1}
d %>%
ggplot() +
geom_point() +
aes(y = state, x = Governor)
```
`aes()` = "aesthetic" mapping --- i.e. how to show some kind of *variation*
- position (e.g. along the x or y-axis)
- color
- shape
- size
- opacity
### Add the positions of legislatures:
```{r points, fig.height = 5, fig.width = 3.1}
## with more layers of `geom_point`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point( aes(x = Governor) ) +
geom_point( aes(x = Lower) ) +
geom_point( aes(x = Upper) )
```
### Add axis labels:
```{r labs, fig.height = 5, fig.width = 3.1}
## with `labs()`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point( aes(x = Governor) ) +
geom_point( aes(x = Lower) ) +
geom_point( aes(x = Upper) ) +
# axis labels!
labs(y = "States",
x = "Mean Ideology Score")
```
### Adjust shape of points:
```{r shape, fig.height = 5, fig.width = 3.1}
## with `shape =`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor), shape = "G") +
geom_point(aes(x = Lower), shape = "L") +
geom_point(aes(x = Upper), shape = "U") +
# axis labels!
labs(y = "States",
x = "Mean Ideology Score")
```
### Add a distance line:
```{r segment, fig.height = 5, fig.width = 3.1}
## with `geom_segment`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor), shape = "G") +
geom_point(aes(x = Lower), shape = "L") +
geom_point(aes(x = Upper), shape = "U") +
# axis labels!
labs(y = "States",
x = "Mean Ideology Score",
color = "") +
# x = lines for the distance between max and min actors' DIME scores
geom_segment(aes(x = min,
xend = max,
y = state,
yend = state))
```
### Adjust the color, size, line end shape, and opacity of the distance line:
```{r linetyle, fig.height = 5, fig.width = 3.1}
## `color`, `size`, line end shape (`lineend`), and opacity (`alpha`)
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor), shape = "G") +
geom_point(aes(x = Lower), shape = "L") +
geom_point(aes(x = Upper), shape = "U") +
# labels
labs(y = "States",
x = "Mean Ideology Score") +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = state),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3)
```
### Break out ("facet") by groups of interest:
```{r facet, fig.height = 5, fig.width = 3.1}
### with `facet_grid()` or `facet_wrap()`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor), shape = "G") +
geom_point(aes(x = Lower), shape = "L") +
geom_point(aes(x = Upper), shape = "U") +
# labels
labs(y = "States",
x = "Mean Ideology Score") +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = state),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3) +
# facet by liberal, conservative, or mixed
facet_grid(state_ideology ~ ., scales = "free_y", space = "free_y")
```
### Adjust x axis and color scales:
```{r scales, fig.height = 5, fig.width = 3.1}
### with `scale_x` and `scale_color`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor, color = Governor), shape = "G") +
geom_point(aes(x = Lower, color = Lower), shape = "L") +
geom_point(aes(x = Upper, color = Upper), shape = "U") +
# labels
labs(y = "States",
x = "Mean Ideology Score",
color = "") +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = state),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3) +
# facet by liberal, conservative, or mixed
facet_grid(state_ideology ~ ., scales = "free_y", space = "free_y") +
# scales
scale_x_continuous(breaks = c(-1, 0, 1)) +
scale_color_gradient2(low = "blue", mid = "black", high = "red")
```
### Adjust axis text size and legend:
```{r theme, fig.height = 5, fig.width = 3.1}
## with `theme()`
d %>%
ggplot() +
# define y = state for all layers
aes(y = state) +
# x = points for each actor
geom_point(aes(x = Governor, color = Governor), shape = "G") +
geom_point(aes(x = Lower, color = Lower), shape = "L") +
geom_point(aes(x = Upper, color = Upper), shape = "U") +
# labels
labs(y = "States",
x = "Mean Ideology Score",
color = "") +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = state),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3) +
# scales
scale_x_continuous(breaks = c(-1, 0, 1)) +
scale_color_gradient2(low = "blue", mid = "black", high = "red") +
# facet by liberal, conservative, or mixed
facet_grid(state_ideology ~ ., scales = "free_y", space = "free_y") +
# adjust text size and drop legend
theme(axis.text.y = element_text(size = 5),
legend.position = "none")
```
### Make all of that a function:
```{r function, fig.height = 5, fig.width = 3.1}
## Define a function for plotting state-level DIME data with ggplot
DIMEplot <- function(d){
d %>%
# define y as state for all layers
ggplot(aes(y = reorder(state, distance))) +
# x = points for each actor
geom_point(aes(x = Governor, color = Governor), shape = "G") +
geom_point(aes(x = Lower, color = Lower), shape = "L") +
geom_point(aes(x = Upper, color = Upper), shape = "U") +
# x = lines for the distance between max and min actors
geom_segment(aes(x = min,
xend = max,
yend = reorder(state, distance)),
color = "grey",
size = 2.1,
lineend = "round",
alpha = .3) +
# labels
labs(y = "States by Preference Divergence",
x = "Mean Ideology Score",
title = paste("Preference Divergence of Political Principals,", unique(d$year) ),
color = "") +
# scales
scale_x_continuous(breaks = c(-1, 0, 1)) +
scale_colour_gradient2(low = "blue", mid = "black", high = "red") +
# facet by liberal, conservative, or mixed
facet_grid(state_ideology ~ ., scales = "free_y", space = "free_y") +
# adjust text size and drop legend
theme(axis.text.y = element_text(size = 5),
title = element_text(size = 7),
legend.position = "none")
}
```
So we can easily can compare alignment among governors (G) and upper (U) and lower (L) chambers of state legislatures between 2004 and 2008:
```{r plotfinal, fig.height = 5, fig.width = 3.1, fig.show='hold'}
d %>%
filter(year == 2004) %>%
DIMEplot()
d %>%
filter(year == 2008) %>%
DIMEplot()
```