This code through explores creating a treemap in R and some of the arguments that will help develop the ideal visual. Treemaps are a visualization of often hierarchical data, that are composed of tiles. Where the size of each tile is proportional to a quantity (i.e. the greater the quantity the larger the tile). These tiles then compose parts of a whole category.
Treemaps are valuable because they can often take the place of the much despised pie chart. They work well to show ratios of parts to a whole by using size and color coding.
This example shows the packages required and runs a default treemap, using US arrest data to show murder arrests by state.
install.packages(“treemapify”)
data("USArrests")
library(ggplot2)
library(treemapify)ggplot(USArrests, aes(area = Murder, fill = Murder, label = row.names(USArrests) )) +
geom_treemap() The default arguments left out a few elements for a basic readable treemap. In the next step labels will be added to each tile and the orientation will be changed to display the smallest value in the lower right hand corner (typical treemap orientation).
Key Notes:
Any changes made to the arguments ‘layout’ and ‘start’ need to be uniform with any additional ‘geoms’, or they will not share a common layout.
ggplot(USArrests, aes(area = Murder, fill = Murder, label = row.names(USArrests))) +
geom_treemap(layout = "scol",
start = "topleft") +
geom_treemap_text(layout = "scol",
start = "topleft",
fontface = "bold",
color = "white",
place = "topleft",
grow = FALSE,
reflow = TRUE)This step creates an additional sub group. The treemap now displays murder by state grouped by urban population.
Each state tile has a proportional hierarchy of murder arrests to other states, and each group of states by urban population has a proportional hierarchy with other urban population groups.
ggplot(USArrests, aes(area = Murder, fill = Murder, label = row.names(USArrests), subgroup = round(UrbanPop, -1))) +
geom_treemap(layout = "scol",
start = "topleft") +
geom_treemap_subgroup_border(layout = "scol",
start = "topleft",
color = "grey",
size = 5)+
geom_treemap_text(layout = "scol",
start = "topleft",
fontface = "bold",
color = "white",
place = "topleft",
grow = FALSE,
reflow = TRUE) +
geom_treemap_subgroup_text(layout = "scol",
start = "topleft",
place = "middle",
color = "grey",
alpha = 0.5,
grow = T)
Learn more about treemaps with the following:
Resource I Treemaps in ggplot2 with treemapify
Resource II Introduction to ‘treemapify’
Resource III treemapify
This code through references and cites the following sources:
David Wilkins (N/A). Source I. treemapify
David Wilkins (2021). Source II. Introduction to ‘treemapify’
UNK (N/A). Source III. anychart.com
UNK (N/A). Source III. datavizcatalogue.com