youtube video link with explanations for these examples https://youtu.be/Aw9WkpziVbI
Easy step by step guide to create a Treemap and customise the look and feel of the chart.
Simple, practical and to the point explanation.
First we get the latest COVID19 data online. Then we create a basic TREEMAP. Then we customise the text which gets displayed in the treemap sections. Then we create a palette of colours so that each section of the treemap can be filled in a different colour.
Lastly we add the title, subtitle to our treemap to make it ready for use in your documents.
# How to plot a treemap in ggplot
#install.packages("treemapify")
#install.packages("ggplot2")
# We will need the following packages (install them using the above code if needed)
library(ggplot2)
library(treemapify)
## Warning: package 'treemapify' was built under R version 4.1.2
# We will following packages to manage our data
library(reshape2)
library(dplyr)
# data to be used for the chart
d <- read.csv("https://covid.ourworldindata.org/data/owid-covid-data.csv")
d <- data.frame(d)
dToday <- d%>%
dplyr::filter(date %in% max(date))%>%
dplyr::filter(! grepl("^OWID",iso_code))%>%
dplyr::arrange(-total_cases)
str(dToday)
## 'data.frame': 215 obs. of 67 variables:
## $ iso_code : chr "USA" "IND" "BRA" "FRA" ...
## $ continent : chr "North America" "Asia" "South America" "Europe" ...
## $ location : chr "United States" "India" "Brazil" "France" ...
## $ date : chr "2022-06-23" "2022-06-23" "2022-06-23" "2022-06-23" ...
## $ total_cases : num 86757621 43362294 31962782 30494968 27681775 ...
## $ new_cases : num 121315 17336 72049 79852 108190 ...
## $ new_cases_smoothed : num 99913 13102 50145 57957 79584 ...
## $ total_deaths : num 1015342 524954 669895 149337 140650 ...
## $ new_deaths : num 507 13 365 40 90 76 10 67 75 0 ...
## $ new_deaths_smoothed : num 329.7 19.6 171.7 38.9 76 ...
## $ total_cases_per_million : num 260600 31120 149363 452300 329936 ...
## $ new_cases_per_million : num 364.4 12.4 336.7 1184.4 1289.5 ...
## $ new_cases_smoothed_per_million : num 300.1 9.4 234.3 859.6 948.5 ...
## $ total_deaths_per_million : num 3050 377 3130 2215 1676 ...
## $ new_deaths_per_million : num 1.523 0.009 1.706 0.593 1.073 ...
## $ new_deaths_smoothed_per_million : num 0.99 0.014 0.802 0.576 0.906 0.815 0.228 0.422 0.828 0 ...
## $ reproduction_rate : num NA NA NA NA NA NA NA NA NA NA ...
## $ icu_patients : num NA NA NA 841 810 NA NA NA 216 NA ...
## $ icu_patients_per_million : num NA NA NA 12.47 9.65 ...
## $ hosp_patients : num NA NA NA 14499 NA ...
## $ hosp_patients_per_million : num NA NA NA 215 NA ...
## $ weekly_icu_admissions : num NA NA NA 431 NA NA NA NA 175 NA ...
## $ weekly_icu_admissions_per_million : num NA NA NA 6.39 NA ...
## $ weekly_hosp_admissions : num NA NA NA 4699 4323 ...
## $ weekly_hosp_admissions_per_million : num NA NA NA 69.7 51.5 ...
## $ total_tests : num NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests : num NA NA NA NA NA NA NA NA NA NA ...
## $ total_tests_per_thousand : num NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_per_thousand : num NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_smoothed : num NA NA NA NA NA NA NA NA NA NA ...
## $ new_tests_smoothed_per_thousand : num NA NA NA NA NA NA NA NA NA NA ...
## $ positive_rate : num NA NA NA NA NA NA NA NA NA NA ...
## $ tests_per_case : num NA NA NA NA NA NA NA NA NA NA ...
## $ tests_units : chr "" "" "" "" ...
## $ total_vaccinations : num NA 1.97e+09 4.51e+08 NA NA ...
## $ people_vaccinated : num NA 1.02e+09 1.85e+08 NA NA ...
## $ people_fully_vaccinated : num NA 9.09e+08 1.69e+08 NA NA ...
## $ total_boosters : num NA 4.14e+07 1.03e+08 NA NA ...
## $ new_vaccinations : num NA 1637306 428440 NA NA ...
## $ new_vaccinations_smoothed : num NA 1354173 320246 NA NA ...
## $ total_vaccinations_per_hundred : num NA 141 211 NA NA ...
## $ people_vaccinated_per_hundred : num NA 72.9 86.2 NA NA ...
## $ people_fully_vaccinated_per_hundred : num NA 65.2 78.8 NA NA ...
## $ total_boosters_per_hundred : num NA 2.97 48.36 NA NA ...
## $ new_vaccinations_smoothed_per_million : num NA 972 1497 NA NA ...
## $ new_people_vaccinated_smoothed : num NA 154293 17104 NA NA ...
## $ new_people_vaccinated_smoothed_per_hundred: num NA 0.011 0.008 NA NA NA 0.001 NA 0.001 0.001 ...
## $ stringency_index : num NA NA NA NA NA NA NA NA NA NA ...
## $ population : num 3.33e+08 1.39e+09 2.14e+08 6.74e+07 8.39e+07 ...
## $ population_density : num 35.6 450.4 25 122.6 237 ...
## $ median_age : num 38.3 28.2 33.5 42 46.6 40.8 43.4 39.6 47.9 31.6 ...
## $ aged_65_older : num 15.41 5.99 8.55 19.72 21.45 ...
## $ aged_70_older : num 9.73 3.41 5.06 13.08 15.96 ...
## $ gdp_per_capita : num 54225 6427 14103 38606 45229 ...
## $ extreme_poverty : num 1.2 21.2 3.4 NA NA 0.2 0.2 0.1 2 0.2 ...
## $ cardiovasc_death_rate : num 151.1 282.3 178 86.1 156.1 ...
## $ diabetes_prevalence : num 10.79 10.39 8.11 4.77 8.31 ...
## $ female_smokers : num 19.1 1.9 10.1 30.1 28.2 20 6.2 23.4 19.8 14.1 ...
## $ male_smokers : num 24.6 20.6 17.9 35.6 33.1 24.7 40.9 58.3 27.8 41.1 ...
## $ handwashing_facilities : num NA 59.5 NA NA NA ...
## $ hospital_beds_per_thousand : num 2.77 0.53 2.2 5.98 8 ...
## $ life_expectancy : num 78.9 69.7 75.9 82.7 81.3 ...
## $ human_development_index : num 0.926 0.645 0.765 0.901 0.947 0.932 0.916 0.824 0.892 0.82 ...
## $ excess_mortality_cumulative_absolute : num NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality_cumulative : num NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality : num NA NA NA NA NA NA NA NA NA NA ...
## $ excess_mortality_cumulative_per_million : num NA NA NA NA NA NA NA NA NA NA ...
#When to use the treemap
# good alternative to a pie chart
Top20 <- dToday%>%dplyr::arrange(-total_cases)%>%head(20)
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases))
pl <- pl + geom_treemap()
pl <- pl + theme(legend.position = "none")
pl
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases, label = total_cases))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + theme(legend.position = "none")
pl
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases, label = paste0(location,"\n", total_cases)))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + theme(legend.position = "none")
pl
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases, label = paste0(location,"\n", prettyNum(total_cases,big.mark=","))))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + theme(legend.position = "none")
pl
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases, label = paste0(location,"\n", prettyNum(total_cases,big.mark=","))))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + labs(title ="Treemap of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + theme(legend.position = "none")
#install.packages("randomcoloR")
library(randomcoloR)
n <- 20
palette <- distinctColorPalette(n)
pl <- ggplot(data = Top20, aes(fill = location, area = total_cases, label = paste0(location,"\n", prettyNum(total_cases,big.mark=","))))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + labs(title ="Treemap of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 20 countries by total cases")
pl <- pl + labs(caption ="#techanswers88 on youtube")
pl <- pl + theme(legend.position = "none")
pl <- pl + scale_fill_manual(values = palette)
pl
mypalette <- c('#BB25DA','#DA259E','#6125DA','#4F6FB0','#65B847','#BDE916','#E9AB16','#C43B3C','#C43B80','#82906F')
Top10 <- dToday%>%
head(10)
pl <- ggplot(data = Top10, aes(fill = location, area = total_cases, label = paste0(location,"\n", prettyNum(total_cases,big.mark=","))))
pl <- pl + geom_treemap()
pl <- pl + geom_treemap_text(colour ="white", place = "centre")
pl <- pl + labs(title ="Treemap of Covid-19 cases")
pl <- pl + labs(subtitle ="Top 10 countries by total cases")
pl <- pl + labs(caption ="#techanswers88 on youtube")
pl <- pl + theme(legend.position = "none")
pl <- pl + scale_fill_manual(values = mypalette)
pl