week 6 treemap activity

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)
library(tidyverse)
library(dslabs)
data(gapminder)
gapminder%>% as_tibble()
# A tibble: 10,545 × 9
   country   year infant_mortality life_expectancy fertility population      gdp
   <fct>    <int>            <dbl>           <dbl>     <dbl>      <dbl>    <dbl>
 1 Albania   1960            115.             62.9      6.19    1636054 NA      
 2 Algeria   1960            148.             47.5      7.65   11124892  1.38e10
 3 Angola    1960            208              36.0      7.32    5270844 NA      
 4 Antigua…  1960             NA              63.0      4.43      54681 NA      
 5 Argenti…  1960             59.9            65.4      3.11   20619075  1.08e11
 6 Armenia   1960             NA              66.9      4.55    1867396 NA      
 7 Aruba     1960             NA              65.7      4.82      54208 NA      
 8 Austral…  1960             20.3            70.9      3.45   10292328  9.67e10
 9 Austria   1960             37.3            68.8      2.7     7065525  5.24e10
10 Azerbai…  1960             NA              61.3      5.57    3897889 NA      
# ℹ 10,535 more rows
# ℹ 2 more variables: continent <fct>, region <fct>
dat1 <- filter(gapminder, year == 2007)
bycontinent <- dat1 |> group_by(continent) |> summarize(total_population = sum(population))
bycontinent
# A tibble: 5 × 2
  continent total_population
  <fct>                <dbl>
1 Africa           948156290
2 Americas         911419886
3 Asia            3911346015
4 Europe           730984863
5 Oceania           34130212
library(RColorBrewer)
library(treemap)

treemap(bycontinent,
        index="continent",
        vSize="total_population",
        palette="RdYlBu", #Use RColorBrewer palette
        title = "World Population by Continent in 2007", # plot title
        title.legend = "Avg Arrival Delay (min)" ) # legend label