-
Notifications
You must be signed in to change notification settings - Fork 31
/
Connin_607_tidyverse.Rmd
118 lines (84 loc) · 4.8 KB
/
Connin_607_tidyverse.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
---
title: "Tidyverse_Connin"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Polling Results: support for gun control
Feature article: https://projects.fivethirtyeight.com/guns-parkland-polling-quiz/
All polls were taken after February 14, 2018, the date of the school shooting in Parkland, Florida
```{r warning = FALSE}
#load our libraries
library(tidyverse)
library(lubridate)
library(magrittr)
library(anytime)
library(ggforce)
# use readr to import/read csv file
guns<-read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/poll-quiz-guns/guns-polls.csv")
glimpse(guns)
# use (lubridate), mutate (dplyr), relocate (dplyr), str_extract (stringr) to calculate the difference between polling start and end dates and put in numeric form.
guns$Start<-mdy(guns$Start)
guns$End<-mdy(guns$End)
guns%<>%mutate(Diff_Date = difftime(End, Start))%>%
relocate(Diff_Date, .after = End)%>%
mutate(Diff_Date = str_extract(Diff_Date, "^."))%>%
mutate(Diff_Date = as.numeric(as.character(Diff_Date )))
glimpse(guns)
```
Extend possibility - accomplish the same task without using lubridate. A hint: if you are not using lubridate, you will need to resolve the following error: x character string is not in a standard unambiguous format.
```{r}
# Create boxplot encoding percent support by party for each question and population
guns%<>%mutate%>%pivot_longer(c("Republican Support", "Democratic Support"), names_to = 'Party', values_to = 'Percent_by_Party')%>%rename(Percent_Total_Support = Support)
plot<-guns%>%
ggplot(aes(x=Party, y=Percent_by_Party))+
geom_boxplot(alpha=0.7, fill="blue")+
facet_grid_paginate(Population~Question, ncol=2, page=1, space="free_y")+
theme_bw()
```
Extend possibility - rework labels for Question and Party categories so that they are legible.
### Extending to rework the labels (Henry Owens)
I am going to try and fix the labels here. In ggplot2 you can use themes to manipulate the non-data components of plots, such as axes, gridlines, and labels.
There is a long list of all the [theme elements here](https://ggplot2.tidyverse.org/reference/theme.html).
It took a little guess work to figure out that the relevant theme element for this case is `strip.text.x.top`. Since it is `text` we can use `element_text()` and the `angle` argument to rotate the text.
```{r}
guns%>%
ggplot(aes(x=Party, y=Percent_by_Party))+
geom_boxplot(alpha=0.7, fill="blue")+
facet_grid_paginate(Population~Question, ncol=2, page=1, space="free_y")+
theme(strip.text.x.top = element_text(angle = 90, hjust = 0))
```
One problem with this new plot is the rotated text at the top crowds out the actual plots. Obviously the bottom labels are still unreadable.
Replacing the dashes in the "Question" column with line breaks (using `str_replace_all`), will solve the crowding problem.
I will also create a new column for abbreviated party designations using `str_sub` to deal with the messy labels along the bottom. I set `xlab()` to "Party", so it looks more polished.
Using the different arguments in `theme()`, you can manipulate the superficial features of the plot.
* `strip.text.x.top` is the text for the top row in this case it is the `Question` column.
* `strip.background.x` and `strip.background.y` refers to the boxes on the top and right hand sides of the plot.
* `panel.background` refers to the plot background.
* `panel.grid.major` sets the gridlines (in this case major) for the plot.
* `plot.background` is the area behind the plot, facets, legends, etc.
The theme elements each have [`element_` functions](https://ggplot2.tidyverse.org/reference/element.html):
* `element_text` for text objects
* `element_rect` for rectangle objects
* `element_line` for lines
```{r}
# replace dashes with line breaks in question
guns$Question <- str_replace_all(guns$Question, "-", "\n")
# abbreviate party affiliation to D or R
guns$Party_abbrev <- str_sub(guns$Party, 1, 1)
# fancy plot
guns%>%
ggplot(aes(x=Party_abbrev, y=Percent_by_Party))+
geom_boxplot(alpha=0.7, fill="blue")+
facet_grid_paginate(Population~Question, ncol=2, page=1, space="free_y")+
theme(strip.text.x.top = element_text(angle = 90, hjust = 0, face = "italic"),
strip.background.x = element_rect(fill = "azure"),
strip.background.y = element_rect(fill = "antiquewhite"),
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(linetype = 2, colour = "gray", size = 0.1),
plot.background = element_rect(fill = "gray99")) +
xlab("Party")
```
This isn't the slickest plot, but knowing how to tweak these components is very helpful!!
Thanks to Sean for the original post! --Henry