-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab_2_demo.qmd
More file actions
113 lines (87 loc) · 2.14 KB
/
Copy pathLab_2_demo.qmd
File metadata and controls
113 lines (87 loc) · 2.14 KB
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
---
title: "Lab 2 Walkthrough"
format: html
editor: visual
---
```{r}
library(tidyverse)
library(here)
library(janitor)
library(readxl)
```
```{r}
dat <- read_excel("wgm2018-dataset-crosstabs-all-countries.xlsx",
sheet = 1,
skip = 2)
```
```{r}
dat_clean <- dat %>%
janitor::clean_names() %>%
select(country:column_n_percent_4) %>%
fill(question, .direction = "down")
```
```{r}
dat_q25 <- dat_clean %>%
filter(str_detect(question, "Q25 "))
```
```{r}
dat_q25 %>%
ggplot(aes(x = country, y = column_n_percent_4, fill = response)) +
geom_col()
```
```{r}
library(rvest)
regions <- read_html("https://meta.wikimedia.org/wiki/List_of_countries_by_regional_classification") %>%
html_table() %>%
.[[1]]
```
```{r}
dat_q25 <- dat_q25 %>%
left_join(regions,
by = c(country = "Country")) %>%
drop_na(Region)
```
```{r}
dat_q25 %>%
ggplot(aes(x = Region, y = column_n_percent_4, fill = response)) +
geom_col()
```
```{r}
dat_vax <- dat_q25 %>%
filter(str_detect(response, " agree")) %>%
group_by(country, Region) %>%
summarize(pct = sum(column_n_percent_4))
```
```{r}
dat_vax %>%
ggplot(aes(x = Region, y = pct, fill = Region)) +
geom_boxplot(alpha = 0.5) +
geom_jitter(aes(color = Region), width = 0.1) +
theme_classic() +
theme(legend.position = "none",
axis.text.x = element_text(angle = 45),
axis.line.x = element_blank())+
labs(
x = "",
y = "",
title = "Percent of surveyed residents who say vaccines are safe."
)
```
```{r}
library(ggiraph)
my_plot <- dat_vax %>%
ggplot(aes(x = Region, y = pct, fill = Region)) +
geom_boxplot(alpha = 0.5) +
geom_jitter_interactive(aes(color = Region,
tooltip = country), width = 0.1) +
theme_classic() +
theme(legend.position = "none",
axis.text.x = element_text(angle = 45),
axis.line.x = element_blank())+
labs(
x = "",
y = "",
title = "Percent of surveyed residents who say vaccines are safe."
)
girafe(ggobj = my_plot)
```