# First, if not installed, install the pacman package
# install.packages("pacman")
pacman::p_load(tidyverse)
Check video at
https://www.youtube.com/watch?v=-khbLbt308o
traffic <- read_csv("https://data.stat.gov.lv:443/sq/13179")
Rows: 32 Columns: 16
── Column specification ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
dbl (16): Year, Road traffic accidents (resulting in death or serious injury), Deaths total, deaths, pedestrians, deaths, passengers, deaths, drivers, deaths, bicycle and...
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
head(traffic)
names(traffic)
[1] "Year" "Road traffic accidents (resulting in death or serious injury)"
[3] "Deaths total" "deaths, pedestrians"
[5] "deaths, passengers" "deaths, drivers"
[7] "deaths, bicycle and moped riders" "Deaths per 100,000 population"
[9] "Injuries total" "injured pedestrians"
[11] "injured passengers" "injured drivers"
[13] "injured bicycle and moped riders" "Injuries per 100,000 population"
[15] "Of the total number of casualties, children (under 14 years of age), deaths" "Of the total number of casualties, children (under 14 years of age), injuries"
glimpse(traffic)
Rows: 32
Columns: 16
$ Year <dbl> 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, …
$ `Road traffic accidents (resulting in death or serious injury)` <dbl> 4325, 4271, 3474, 3389, 3814, 4056, 3711, 3925, 4540, 4442, 4482, 4766, 5083, 5379, …
$ `Deaths total` <dbl> 877, 923, 729, 670, 717, 611, 550, 525, 627, 604, 588, 517, 518, 493, 516, 442, 407,…
$ `deaths, pedestrians` <dbl> 302, 310, 271, 253, 235, 185, 195, 171, 210, 202, 233, 186, 181, 181, 197, 173, 152,…
$ `deaths, passengers` <dbl> 225, 210, 162, 145, 203, 153, 139, 141, 163, 168, 136, 113, 135, 106, 128, 100, 78, …
$ `deaths, drivers` <dbl> 273, 299, 217, 206, 234, 222, 175, 189, 211, 202, 164, 171, 161, 162, 158, 133, 137,…
$ `deaths, bicycle and moped riders` <dbl> 77, 104, 79, 66, 45, 51, 41, 24, 43, 32, 55, 47, 41, 44, 33, 36, 40, 23, 18, 27, 17,…
$ `Deaths per 100,000 population` <dbl> 33, 35, 28, 26, 28, 25, 23, 22, 26, 25, 25, 22, 22, 22, 23, 20, 18, 19, 15, 12, 10, …
$ `Injuries total` <dbl> 4716, 4543, 3766, 3721, 4380, 4903, 4324, 4674, 5414, 5244, 5449, 5852, 6300, 6639, …
$ `injured pedestrians` <dbl> 1405, 1480, 1168, 1195, 1357, 1434, 1409, 1400, 1618, 1543, 1550, 1657, 1698, 1790, …
$ `injured passengers` <dbl> 1362, 1309, 1153, 1143, 1441, 1739, 1443, 1579, 1847, 1855, 1906, 2032, 2134, 2241, …
$ `injured drivers` <dbl> 1633, 1429, 1140, 1112, 1348, 1460, 1210, 1464, 1665, 1537, 1620, 1766, 1960, 2059, …
$ `injured bicycle and moped riders` <dbl> 316, 325, 305, 271, 234, 270, 262, 231, 284, 309, 373, 397, 508, 549, 443, 415, 387,…
$ `Injuries per 100,000 population` <dbl> 177, 171, 144, 145, 174, 197, 177, 193, 226, 220, 230, 250, 273, 290, 284, 250, 244,…
$ `Of the total number of casualties, children (under 14 years of age), deaths` <dbl> 65, 64, 36, 50, 51, 32, 28, 21, 33, 18, 16, 17, 22, 16, 6, 12, 12, 11, 14, 8, 9, 5, …
$ `Of the total number of casualties, children (under 14 years of age), injuries` <dbl> 629, 623, 486, 490, 608, 562, 613, 570, 638, 567, 585, 592, 699, 743, 649, 576, 559,…
traffic_long <- traffic %>%
pivot_longer(-Year,
names_to = "item",
values_to = "value")
traffic_long %>%
filter(str_detect(item, "100,000")) %>%
ggplot(aes(x = Year,
y = value,
color = item)) +
geom_point() +
scale_y_log10()
traffic_long %>%
filter(str_detect(item, "100,000")) %>%
ggplot(aes(x = Year,
y = value,
color = item)) +
geom_line() +
scale_y_log10()
traffic_long %>%
filter(str_detect(item, "100,000")) %>%
ggplot(aes(x = Year,
y = value,
color = item,
# here you tell ggplot that should merge the point by this variable
group = item)) +
geom_line() +
scale_y_log10()
# the same graph as before
traffic_long %>%
filter(str_detect(item, "100,000")) %>%
ggplot(aes(x = Year,
y = value,
color = item,
group = item)) +
geom_line() +
scale_y_log10() +
# but here is the customization
# add a title and labs
labs(title = "Traffic deaths and injuries per 100,000 population, Latvia",
y = "Per 100,000 population",
color = "Variable") +
# change the theme
theme_light() +
# put the legend inside the graph to save space
theme(legend.position = c(0.2, 0.15))