Class activity 2

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
str(gapminder)
'data.frame':   10545 obs. of  9 variables:
 $ country         : Factor w/ 185 levels "Albania","Algeria",..: 1 2 3 4 5 6 7 8 9 10 ...
 $ year            : int  1960 1960 1960 1960 1960 1960 1960 1960 1960 1960 ...
 $ infant_mortality: num  115.4 148.2 208 NA 59.9 ...
 $ life_expectancy : num  62.9 47.5 36 63 65.4 ...
 $ fertility       : num  6.19 7.65 7.32 4.43 3.11 4.55 4.82 3.45 2.7 5.57 ...
 $ population      : num  1636054 11124892 5270844 54681 20619075 ...
 $ gdp             : num  NA 1.38e+10 NA NA 1.08e+11 ...
 $ continent       : Factor w/ 5 levels "Africa","Americas",..: 4 1 1 2 2 3 2 5 4 3 ...
 $ region          : Factor w/ 22 levels "Australia and New Zealand",..: 19 11 10 2 15 21 2 1 22 21 ...
colSums(is.na(gapminder))
         country             year infant_mortality  life_expectancy 
               0                0             1453                0 
       fertility       population              gdp        continent 
             187              185             2972                0 
          region 
               0 
df1<- gapminder %>% group_by(continent) %>% summarise(total_population = sum(population, na.rm = TRUE))
head(df1)
# A tibble: 5 × 2
  continent total_population
  <fct>                <dbl>
1 Africa         34945904509
2 Americas       39375328377
3 Asia          165009400333
4 Europe         39076006443
5 Oceania         1464709191
treemap1<- treemap(df1,
                   index = "continent",
                   vSize = "total_population",
                   vColor = "total_population",
                   type="manual",
                   palette = "Reds",
                   title = "Total population per continent",
                   title.legend = "Total population" 
                   )

treemap1
$tm
  continent        vSize       vColor       stdErr  vColorValue level        x0
1    Africa  34945904509  34945904509  34945904509  34945904509     1 0.5895902
2  Americas  39375328377  39375328377  39375328377  39375328377     1 0.5895902
3      Asia 165009400333 165009400333 165009400333 165009400333     1 0.0000000
4    Europe  39076006443  39076006443  39076006443  39076006443     1 0.7955780
5   Oceania   1464709191   1464709191   1464709191   1464709191     1 0.9834902
         y0          w         h   color
1 0.0000000 0.39390004 0.3169946 #FCC9B4
2 0.3169946 0.20598784 0.6830054 #FCC0A8
3 0.0000000 0.58959018 1.0000000 #67000D
4 0.3169946 0.20442197 0.6830054 #FCC3AC
5 0.0000000 0.01650977 0.3169946 #FFF5F0

$type
[1] "manual"

$vSize
[1] "total_population"

$vColor
[1] "total_population"

$stdErr
[1] "total_population"

$algorithm
[1] "pivotSize"

$vpCoorX
[1] 0.02812148 0.97187852

$vpCoorY
[1] 0.171685 0.910315

$aspRatio
[1] 1.788798

$range
[1] 0.0e+00 1.6e+11

$mapping
[1] NA NA NA

$draw
[1] TRUE