-
Notifications
You must be signed in to change notification settings - Fork 1
/
report.Rmd
172 lines (124 loc) · 3.85 KB
/
report.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
---
title: "New Motor Vehicle Sales and Registrations in Canada"
subtitle: ''
# author: "Name"
date: "`r Sys.Date()`"
# geometry: margin=1cm
output:
html_document
# pdf_document
params:
nmvs_data: NA
nmvr_data: NA
province: NA
---
```{r setup, echo=FALSE, include=FALSE, warning=FALSE, message=FALSE}
# load libraries
library(ggrepel)
library(ggthemes)
library(janitor)
library(knitr)
library(skimr)
library(scales)
library(tidyverse)
# Set some options
knitr::opts_chunk$set(echo = FALSE, cache = TRUE, linewidth = 45, fig.height = 6, fig.width = 10, tidy = TRUE)
skimr::skim_with(numeric = list(hist = NULL))
# Load the data
nmvs_data <- params$nmvs_data
nmvr_data <- params$nmvr_data
province <- params$province
```
## EV Registrations by Province
```{r}
max_year <- nmvr_data$year %>% max()
other_provinces <- c("Newfoundland and Labrador",
"Prince Edward Island",
"Nova Scotia",
"New Brunswick",
"Manitoba",
"Saskatchewan",
"Alberta")
ev_fuel_types <- c('Battery electric', 'Plug-in hybrid electric')
ev_nmvr_data <-
nmvr_data %>%
dplyr::filter(fuel_type %in% ev_fuel_types)
# Pie chart
ev_plot_data <- ev_nmvr_data %>%
dplyr::filter(year == max_year) %>%
dplyr::mutate(geo = ifelse(geo %in% other_provinces, 'Other provinces', geo)) %>%
group_by(geo) %>%
dplyr::summarise(amount = sum(amount)) %>%
ungroup()
ev_plot_data %>%
dplyr::arrange(desc(geo)) %>%
dplyr::mutate(prop = amount / sum(ev_plot_data$amount) * 100) %>%
dplyr::mutate(prop = round(prop, 2)) %>%
dplyr::mutate(ypos = cumsum(prop) - 0.5*prop) %>%
ggplot(aes(x='', y=prop, fill=geo)) +
geom_bar(stat='identity', width=1, color='white') +
coord_polar('y', start=0) +
theme_void() +
geom_text(aes(y=ypos, label=paste0(prop, '%')), color='white') +
labs(
title = paste0('EV Registrations By Province - ', max_year),
fill = ''
)
# Table
ev_nmvr_data %>%
dplyr::arrange(year, geo) %>%
dplyr::group_by(year, geo) %>%
dplyr::summarise(amount = sum(amount)) %>%
dplyr::ungroup() %>%
dplyr::group_by(geo) %>%
dplyr::mutate(approx_total = cumsum(amount)) %>%
dplyr::filter(year == max_year) %>%
dplyr::select(-year) %>%
adorn_totals('row', name = 'Canada') %>%
dplyr::rename('Province/Territory' = geo, !!paste(max_year) := amount, 'Approx. EV Total' = approx_total) %>%
kable()
```
## BEV vs PHEV
```{r}
n_years <- nmvr_data$year %>% unique() %>% length()
nmvr_data %>%
dplyr::filter(fuel_type %in% c('Battery electric', 'Plug-in hybrid electric')) %>%
dplyr::group_by(year, fuel_type) %>%
dplyr::summarise(amount = sum(amount)) %>%
ungroup() %>%
ggplot(aes(x=year, y=amount, color=fuel_type, label=amount)) +
geom_line() +
geom_point() +
geom_text_repel() +
labs(
x = 'Year',
y = 'Vehicle sales',
title = 'Yearly EV Registrations by Type',
color = 'Fuel Type'
) +
scale_x_continuous(breaks = scales::pretty_breaks(n = n_years)) +
theme_hc()
```
```{r}
nmvr_data %>%
dplyr::filter(geo == province) %>%
dplyr::filter(fuel_type %in% c('Battery electric', 'Plug-in hybrid electric')) %>%
dplyr::group_by(year, fuel_type) %>%
dplyr::summarise(amount = sum(amount)) %>%
ungroup() %>%
ggplot(aes(x=year, y=amount, color=fuel_type, label=amount)) +
geom_line() +
geom_point() +
geom_text_repel() +
labs(
x = 'Year',
y = 'Vehicle sales',
title = paste0('Yearly EV Registrations by Type in ', province),
color = 'Fuel Type'
) +
scale_x_continuous(breaks = scales::pretty_breaks(n = n_years)) +
theme_hc()
```
## Sources
[Statistics Canada, New motor vehicle registrations](https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=2010002101)
[Statistics Canada, New motor vehicle sales](https://www150.statcan.gc.ca/t1/tbl1/en/tv.action?pid=2010000101)