In-class Activity Week 4

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dslabs)
library(treemap)
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
filter1 <- filter(gapminder, year == "2007")
continent_grp <- filter1 %>% group_by(continent)
continent_grp
# A tibble: 185 × 9
# Groups:   continent [5]
   country   year infant_mortality life_expectancy fertility population      gdp
   <fct>    <int>            <dbl>           <dbl>     <dbl>      <dbl>    <dbl>
 1 Albania   2007             16.7            76.6      1.8     3010849  5.33e 9
 2 Algeria   2007             26.4            75.3      2.66   34261971  7.31e10
 3 Angola    2007            117.             56.2      6.52   19183907  2.17e10
 4 Antigua…  2007              9.2            75.3      2.18      84397  1.01e 9
 5 Argenti…  2007             14.1            75.2      2.25   39969903  3.70e11
 6 Armenia   2007             18.8            72.3      1.4     2988117  4.38e 9
 7 Aruba     2007             NA              74.6      1.74     101218 NA      
 8 Austral…  2007              4.5            81.5      1.92   20975949  5.23e11
 9 Austria   2007              3.8            80.1      1.38    8301290  2.24e11
10 Azerbai…  2007             39.3            69.1      1.99    8763359  1.67e10
# ℹ 175 more rows
# ℹ 2 more variables: continent <fct>, region <fct>
total_pop <- continent_grp %>% summarize(total_population = sum(population))
treemap(total_pop,
        index="continent",
        vSize= "total_population",
        title = "World Population By Continent in 2007")