# Install packages
# install.packages("tidyverse")
# install.packages("dslabs")
# install.packages("treemap")Treemap Visualization
Treemap Visualization
# Load libraries
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) # Groupping and computing
# Also filtering to include only the year 2007
continent_pop <- gapminder %>%
filter(year == 2007) %>%
group_by(continent) %>%
summarise(total_population = sum(population)) %>%
as.data.frame() # Just looking at this as a data frame. It's convenient this way.
continent_pop continent total_population
1 Africa 948156290
2 Americas 911419886
3 Asia 3911346015
4 Europe 730984863
5 Oceania 34130212
# Treemap visualization
# Area of each rectangle is total population
library(RColorBrewer)
treemap(continent_pop,
index = "continent",
vSize = "total_population",
vColor="total_population",
palette="RdYlBu", #Use RColorBrewer palette
title = "World Population by Continent, 2007",
title.legend = "Avg Arrival Delay (min)" ) # legend label