STAA 566 - ggplot2

Author

Bogarth Hernandez

Data Source

United Nations, Department of Economic and Social Affairs, Population Division (2022). World Population Prospects 2022, Online Edition.

World Population Prospects - Population Division - United Nations

R Code

library(ggplot2)
library(tidyverse)
── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.2      ✔ forcats 0.5.2 
✔ purrr   0.3.4      
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
library(ggthemes)
library(viridis)
Loading required package: viridisLite
dem <- read_csv('dem.csv', show_col_types = FALSE)

tfr <- select(dem, LocTypeName, Location, Time, TFR) %>% filter( LocTypeName=="Geographic region" & Location != "Africa" & Time <= 2020 & Time >= 1970)

gtfr <- ggplot(data = tfr, mapping = aes(x = Time, y = TFR, color=Location))

gtfr <- gtfr + geom_line()

gtfr <- gtfr + theme_tufte(base_size=12, base_family = "sans") + labs(title=
"Children per Woman by Region", subtitle= "Evolution of the Fertility Rate from 1970 to 2020", caption= "United Nations, Department of Economic and Social Affairs") + guides(color=guide_legend(title="Region"))

gtfr <- gtfr + scale_color_viridis(discrete = TRUE)




gtfr <- gtfr + ylab(NULL) + xlab("Year") + scale_x_continuous(breaks = seq(1970,2020,10))  + theme(legend.position = "bottom")

gtfr