-
Notifications
You must be signed in to change notification settings - Fork 145
/
2021_01_05_transit_costs.Rmd
169 lines (141 loc) · 4.67 KB
/
2021_01_05_transit_costs.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
---
title: "TidyTemplate"
date: 2021-01-05
output: html_output
---
# TidyTuesday
Join the R4DS Online Learning Community in the weekly #TidyTuesday event!
Every week we post a raw dataset, a chart or article related to that dataset, and ask you to explore the data.
While the dataset will be “tamed”, it will not always be tidy! As such you might need to apply various R for Data Science techniques to wrangle the data into a true tidy format.
The goal of TidyTuesday is to apply your R skills, get feedback, explore other’s work, and connect with the greater #RStats community!
As such we encourage everyone of all skills to participate!
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(tidytuesdayR)
library(scales)
library(glue)
theme_set(theme_light())
```
# Load the weekly Data
Dowload the weekly data and make available in the `tt` object.
```{r Load}
tt <- tt_load("2021-01-05")
library(countrycode)
transit_cost <- tt$transit_cost %>%
filter(!is.na(e)) %>%
mutate_at(vars(start_year, end_year, real_cost), as.numeric) %>%
mutate(country_code = ifelse(country == "UK", "GB", country),
country = countrycode(country_code, "iso2c", "country.name"),
tunnel_per = tunnel / length,
rr = ifelse(rr, "Railroad", "Not Railroad"))
```
```{r}
transit_cost %>%
count(city, country, sort = TRUE)
transit_cost %>%
filter(country == "United States") %>%
mutate(line = fct_reorder(line, year)) %>%
ggplot(aes(xmin = start_year, xmax = end_year, y = line,
color = city,
size = real_cost)) +
geom_errorbarh(height = .1) +
labs(x = "Year",
y = "",
color = "City")
transit_cost %>%
ggplot(aes(cost_km_millions)) +
geom_histogram() +
scale_x_continuous(labels = dollar) +
labs(x = "Cost / KM (Millions of USD)")
transit_cost %>%
filter(!is.na(cost_km_millions),
tunnel_per == 1) %>%
mutate(country = fct_lump(country, 10)) %>%
add_count(country) %>%
mutate(country = glue("{ country } ({ n })"),
country = fct_reorder(country, cost_km_millions)) %>%
ggplot(aes(cost_km_millions, country)) +
geom_boxplot() +
scale_x_continuous(labels = dollar) +
labs(x = "Cost / KM (Millions of USD)",
y = "")
```
```{r}
transit_cost %>%
filter(!is.na(cost_km_millions),
tunnel_per == 1,
country == "China") %>%
mutate(city = fct_lump(city, 10)) %>%
add_count(city) %>%
mutate(city = glue("{ city } ({ n })"),
city = fct_reorder(city, cost_km_millions)) %>%
ggplot(aes(cost_km_millions, city)) +
geom_boxplot() +
scale_x_continuous(labels = dollar) +
labs(x = "Cost / KM (Millions of USD)",
y = "") +
expand_limits(x = 0)
transit_cost %>%
filter(!is.na(cost_km_millions),
tunnel_per == 1,
country == "China")
```
```{r}
transit_cost %>%
filter(country == "China",
city == "Shanghai",
!is.na(start_year),
!is.na(end_year)) %>%
mutate(city = fct_lump(city, 5)) %>%
mutate(line = fct_reorder(line, year)) %>%
ggplot(aes(xmin = start_year, xmax = end_year, y = line,
color = city,
size = real_cost)) +
geom_errorbarh(height = .1) +
labs(x = "Year",
y = "",
color = "City")
transit_cost %>%
filter(tunnel_per == 1,
end_year <= 2020,
country == "China") %>%
group_by(year = (year %/% 5) * 5) %>%
summarize(median_cost_km = median(cost_km_millions),
n = n()) %>%
ggplot(aes(year, median_cost_km)) +
geom_line() +
geom_point(aes(size = n))
transit_cost %>%
filter(tunnel_per == 1,
end_year <= 2020,
country == "China") %>%
mutate(year = (year %/% 5) * 5,
city = fct_lump(city, 5)) %>%
ggplot(aes(year, cost_km_millions, group = year)) +
geom_boxplot(outlier.size = -1) +
geom_jitter(aes(color = city), height = 0, width = 1) +
expand_limits(y = 0) +
labs(y = "Cost / km (Real USD, Millions)",
x = "Year",
title = "Cost distribution / km in China")
transit_cost %>%
filter(tunnel_per == 1,
end_year <= 2020,
country == "China") %>%
mutate(city = fct_lump(city, 4)) %>%
ggplot(aes(stations / length, cost_km_millions, size = length,
color = city)) +
geom_point() +
expand_limits(x = 0, y = 0) +
labs(x = "Stations / km", "Cost / kilometer",
y = "Cost / km")
```
Conclusions: Prices in China ranged from slightly above 200M/KM to a bit under 200M/KM from 2000 to 2015, and have since risen a bit.
cost_km_millions comes from real_cost / length (NOT completed length)
No strong relationship between stations/km and cost/km
```{r}
transit_cost %>%
ggplot(aes(cost_km_millions, real_cost / length)) +
geom_point()
```