Week 6

Loading libraries

library(tidyverse)
library(dslabs)
library(treemap)
library(RColorBrewer)
data(gapminder)
head(gapminder)
              country year infant_mortality life_expectancy fertility
1             Albania 1960           115.40           62.87      6.19
2             Algeria 1960           148.20           47.50      7.65
3              Angola 1960           208.00           35.98      7.32
4 Antigua and Barbuda 1960               NA           62.97      4.43
5           Argentina 1960            59.87           65.39      3.11
6             Armenia 1960               NA           66.86      4.55
  population          gdp continent          region
1    1636054           NA    Europe Southern Europe
2   11124892  13828152297    Africa Northern Africa
3    5270844           NA    Africa   Middle Africa
4      54681           NA  Americas       Caribbean
5   20619075 108322326649  Americas   South America
6    1867396           NA      Asia    Western Asia

Filtering and grouping continent.

cont_2007 <- gapminder |>
  filter(year == 2007) |>
  group_by(continent) |>
  summarise(total_pop = sum(population, na.rm = TRUE))
 cont_2007
# A tibble: 5 × 2
  continent  total_pop
  <fct>          <dbl>
1 Africa     948156290
2 Americas   911419886
3 Asia      3911346015
4 Europe     730984863
5 Oceania     34130212

Creating treemap

treemap(cont_2007,
        index = "continent",
        vSize = "total_pop",
        title = "Total Population in Each Continent",
        title.legend = "Average Population"
        )