Activity: Treemap Visualization
Load libraries and packages
── 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)
Filter data
data(gapminder)
gapminder <- filter(gapminder, year == 2007)
Group data
gapminder <- group_by(gapminder,continent)
Compute total population for each continent
s <- gapminder |> summarize(total=sum(population))
head(s)
# A tibble: 5 × 2
continent total
<fct> <dbl>
1 Africa 948156290
2 Americas 911419886
3 Asia 3911346015
4 Europe 730984863
5 Oceania 34130212
Create treemap
treemap(gapminder,
index="continent",
vSize="total",
vColor="continent",
title="Population by Continent")