I downloaded heights data as the CSV file used below from Our World in Data. (Click on Download and then on Full Data (CSV).) It gives heights in centimeters for males and females and for various countries. The page provides the following explanation: “The data shown here is based on a global study, published by NCD Risk Factor Collaboration (NCD-RisC) in 2016. This dataset is based on both demographic and health surveys as well as academic studies. It reports mean height for adults by year of birth, from 1896 to 1996; in other words, people who had reached their eighteenth birthday from 1914 to 2014.”
library(tidyverse)
heights <- read_csv(
file = "average-height-by-year-of-birth.csv"
) %>%
rename(Male = `Mean male height (cm)`, Female = `Mean female height (cm)`)
Here’s a little bit of the data:
head(heights)
## # A tibble: 6 x 5
## Entity Code Year Male Female
## <chr> <chr> <dbl> <dbl> <dbl>
## 1 Afghanistan AFG 1896 161. 149.
## 2 Afghanistan AFG 1897 161. 149.
## 3 Afghanistan AFG 1898 161. 149.
## 4 Afghanistan AFG 1899 161. 150.
## 5 Afghanistan AFG 1900 161. 150.
## 6 Afghanistan AFG 1901 161. 150.
ggplot(mapping = aes(x = Year)) +
geom_line(data = subset(heights, Entity == "Netherlands"), mapping = aes(y = Female), color = "red") +
geom_line(data = subset(heights, Entity == "India"), mapping = aes(y = Male), color = "blue") +
labs(x = NULL, y = "Height (cm)")
The heights of adult Dutch females and adult Indian males are in red and blue, respectively. Note that the average Dutch female caught up with the average Indian male in height at around 1925. For 1996, which is the latest year for which this data set provides information, the height of the average Dutch female was 168.72 cm and the height of the average Indian male was 164.95 cm.
Let’s repeat the above exercise with a different graphing style:
ggplot(mapping = aes(x = Year)) +
geom_line(data = subset(heights, Entity == "Netherlands"), mapping = aes(y = Female, color = "Netherlands Female")) +
geom_line(data = subset(heights, Entity == "India"), mapping = aes(y = Male, color = "India Male")) +
labs(x = NULL, y = "Height (cm)")
readr::read_csv() to import data from a CSV filedplyr::rename() to rename variablesggplot2package to plot multiple time series with specified colorsggplot2 package to plot multiple time series with specified legendThe End