-
Notifications
You must be signed in to change notification settings - Fork 0
/
Statebins.Rmd
84 lines (72 loc) · 2.46 KB
/
Statebins.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
---
title: "socviz chapter 7 section 3 Statebins"
author: "Yaqiong Li"
date: "Dec 8, 2018"
output: pdf_document
---
Instead of using *ggplot2* or *chloroplethr* packages to achieve chloropelth maps in the United States, *statebins* is another choice.
Install the book's package from github.
```{r}
# devtools::install_github("kjhealy/socviz")
```
Several arguements:
* state_data refers to the data frame
* state_col refers to a vector of state names
* value_col refers to the value to be shown
* statebins_continuous() refers to continuous variables
```{r message=FALSE}
library(statebins)
library(socviz) # this package has election as a data frame
library(tidyverse)
head(election)
```
```{r}
election %>% select(state, total_vote, r_points, pct_trump, party, census) %>% sample_n(5)
```
```{r}
statebins_continuous(state_data = election,
state_col = "state",
text_color = "white",
value_col = "pct_trump",
brewer_pal = "Reds",
font_size = 3,
legend_title = "Percent Trump")
```
```{r}
statebins_continuous(state_data = subset(election, st %nin% "DC"),
state_col = "state",
text_color = "black",
value_col = "pct_clinton",
brewer_pal = "Blues",
font_size = 3,
legend_title = "Percent Clinton")
```
For categorical data, use statebins_manual(). Here a variable called _color_ is created to represent the party.
```{r}
election <- election %>% mutate(color = recode(party,
Republican = "red",
Democrat = "blue"))
head(election)
```
```{r}
statebins_manual(state_data = election,
state_col = "st",
color_col = "color",
text_color = "black",
font_size = 3,
legend_title = "Winner",
labels = c("Trump", "Clinton"),
legend_position = "right")
```
Alternatively, use statebins() cut the data by the _breaks_ argument.
```{r}
statebins(state_data = election,
state_col = "state",
value_col = "pct_trump",
text_color = "white",
breaks = 4,
labels = c("4-21", "21-37", "37-53", "53-70"),
brewer_pal = "Reds",
font_size = 3,
legend_title = "Percent Trump")
```