-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2020_11_10_tidy_tuesday.Rmd
169 lines (138 loc) · 4.94 KB
/
2020_11_10_tidy_tuesday.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
---
title: 'Tidy Tuesday 11/10'
author: "Lisa Lendway"
output:
html_document:
df_print: paged
code_download: true
theme: cerulean
---
```{r setup, include=FALSE}
# You may want to comment this out at first so you see important messages and warnings
knitr::opts_chunk$set(echo = TRUE, error=TRUE, message=FALSE, warning=FALSE)
```
```{r libraries}
library(tidyverse) # for graphing and data cleaning
library(lubridate) # for date manipulation
library(ggthemes) # for even more plotting themes
library(ggtext) # for coloring text in labels
library(naniar) # for exploring missing values
library(gghighlight) # for highlighting certain values
library(maps) # for map data
library(ggthemes) # for themes, including theme_map()
library(gganimate) # for animation
theme_set(theme_minimal()) # My favorite ggplot() theme :)
```
```{r}
# Read in the data for the week
mobile <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/mobile.csv')
landline <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-11-10/landline.csv')
```
This week's data is all about phones!
<center>
![](https://images-na.ssl-images-amazon.com/images/I/61i8xYfGsFL._SL1500_.jpg){width=200} ![](https://i.pcmag.com/imagery/reviews/03xdTO0Ka4H4KvEgtSPg4c2-12..1569479325.jpg){width=200}
</center>
Read more about it, including definitions of variables, [here](https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-11-10).
### Code I worked on during the 30 minute screencast
```{r}
landline %>%
add_n_miss() %>%
arrange(desc(n_miss_all))
```
```{r}
landline %>%
drop_na(landline_subs) %>%
group_by(entity) %>%
fill(total_pop, gdp_per_cap, .direction = "up") %>%
ggplot(aes(x = year,
y = landline_subs,
group = entity,
color = continent)) +
geom_line() +
facet_wrap(vars(continent)) +
gghighlight(max(landline_subs>100)) +
theme(legend.position = "none")
```
# animated graph over time
Added some details after 30 minute video.
```{r, eval=FALSE}
world_map <- map_data("world")
landline %>%
drop_na(landline_subs) %>%
group_by(entity) %>%
fill(total_pop, gdp_per_cap, .direction = "up") %>%
ggplot() +
geom_map(map = world_map,
aes(map_id = entity,
fill = landline_subs,
group = year)) +
#This assures the map looks decently nice:
expand_limits(x = world_map$long, y = world_map$lat) +
theme_map() +
theme(legend.background = element_blank(),
plot.background = element_rect("gray70")) +
transition_states(year) +
labs(title = "Landlines subscriptions per 100 people",
subtitle = "Year: {closest_state}",
caption = "Data from Ourworldindata.org, viz by @lisalendway")
anim_save("landline_over_time.gif", path = "images/")
```
![](images/landline_over_time.gif)
In the previous plot, there are missing countries. This code investigates which countries are missing.
```{r}
world_map %>%
distinct(region) %>%
anti_join(landline,
by = c("region"="entity"))
```
# code after 30 minutes
create similar animated map for mobile
```{r, eval=FALSE}
world_map <- map_data("world")
mobile %>%
drop_na(mobile_subs) %>%
group_by(entity) %>%
fill(total_pop, gdp_per_cap, .direction = "up") %>%
ggplot() +
geom_map(map = world_map,
aes(map_id = entity,
fill = mobile_subs,
group = year)) +
#This assures the map looks decently nice:
expand_limits(x = world_map$long, y = world_map$lat) +
theme_map() +
theme(legend.background = element_blank(),
plot.background = element_rect("#F8F3ED")) +
transition_states(year) +
labs(title = "Mobile subscriptions per 100 people",
subtitle = "Year: {closest_state}",
caption = "Data from Ourworldindata.org, viz by @lisalendway")
anim_save("mobile_over_time.gif", path = "images/")
```
![](images/mobile_over_time.gif)
Using `geom_polygon()` instead of `geom_map()` - this kept crashing my computer and I don't have time to debug.
```{r, eval=FALSE}
world_map <- map_data("world")
landline %>%
drop_na(landline_subs) %>%
group_by(entity) %>%
fill(total_pop, gdp_per_cap, .direction = "up") %>%
inner_join(world_map,
by = c("entity"="region")) %>%
# filter(year == 2010) %>%
ggplot() +
geom_polygon(aes(x = long,
y = lat,
group = group,
fill = landline_subs)) +
#This assures the map looks decently nice:
expand_limits(x = world_map$long,
y = world_map$lat) +
theme_map() +
theme(legend.background = element_blank(),
plot.background = element_rect("gray70")) +
transition_time(year) +
labs(title = "Landlines subscriptions per 100 people",
subtitle = "Year: {frame_time}",
caption = "Data from Ourworldindata.org, viz by @lisalendway")
```