{"id":13788722,"url":"https://github.com/wilkox/treemapify","last_synced_at":"2025-04-09T13:07:10.869Z","repository":{"id":13678281,"uuid":"16372000","full_name":"wilkox/treemapify","owner":"wilkox","description":"🌳 Draw treemaps in ggplot2","archived":false,"fork":false,"pushed_at":"2024-06-15T05:01:26.000Z","size":60776,"stargazers_count":214,"open_issues_count":4,"forks_count":18,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-02T10:14:50.703Z","etag":null,"topics":["data-visualisation","ggplot2","r","treemap"],"latest_commit_sha":null,"homepage":"http://wilkox.org/treemapify","language":"R","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wilkox.png","metadata":{"files":{"readme":"README.Rmd","changelog":"NEWS.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-01-30T08:34:11.000Z","updated_at":"2025-03-22T11:00:42.000Z","dependencies_parsed_at":"2022-09-06T17:41:18.534Z","dependency_job_id":"6a7e50b7-62b6-4444-b929-e9d48a91428a","html_url":"https://github.com/wilkox/treemapify","commit_stats":{"total_commits":273,"total_committers":6,"mean_commits":45.5,"dds":0.04395604395604391,"last_synced_commit":"425e12eb1d03b837552891a190fae82bd78c686e"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilkox%2Ftreemapify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilkox%2Ftreemapify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilkox%2Ftreemapify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wilkox%2Ftreemapify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wilkox","download_url":"https://codeload.github.com/wilkox/treemapify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248045231,"owners_count":21038553,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-visualisation","ggplot2","r","treemap"],"created_at":"2024-08-03T21:00:52.426Z","updated_at":"2025-04-09T13:07:10.853Z","avatar_url":"https://github.com/wilkox.png","language":"R","funding_links":[],"categories":["ggplot","R","Table of Contents"],"sub_categories":["Additional Plot Types","Visualization"],"readme":"---\noutput: \n  github_document:\n    html_preview: true\n    fig_height: 6\n    fig_width: 6\n---\n\n```{r, echo = FALSE}\nlibrary(knitr)\nknitr::opts_chunk$set(\n  collapse = TRUE,\n  comment = \"#\u003e\",\n  fig.path = \"man/figures/README-\"\n)\n```\n\n\u003c!-- badges: start --\u003e\n[![R-CMD-check](https://github.com/wilkox/treemapify/workflows/R-CMD-check/badge.svg)](https://github.com/wilkox/treemapify/actions)\n[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/treemapify)](https://cran.r-project.org/package=treemapify)\n[![lifecycle](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)\n\n# treemapify\n\ntreemapify provides [ggplot2](https://ggplot2.tidyverse.org) geoms for drawing [treemaps](https://en.wikipedia.org/wiki/Treemap).\n\n## Installation\n\nInstall the release version of treemapify from CRAN:\n\n`install.packages(\"treemapify\")`\n\nIf you want the development version, install it from GitHub:\n\n`devtools::install_github(\"wilkox/treemapify\")`\n\n## The G20 dataset\n\ntreemapify includes an example dataset containing statistics about the G-20\ngroup of major world economies.\n\n```{r message = FALSE}\nlibrary(ggplot2)\nlibrary(treemapify)\nG20\n```\n\n## Drawing a simple treemap\n\nIn a treemap, each tile represents a single observation, with the area of the\ntile proportional to a variable. Let's start by drawing a treemap with each tile\nrepresenting a G-20 country. The area of the tile will be mapped to the\ncountry's GDP, and the tile's fill colour mapped to its HDI (Human Development\nIndex). `geom_treemap()` is the basic geom for this purpose.\n\n```{r basic_treemap}\nggplot(G20, aes(area = gdp_mil_usd, fill = hdi)) +\n  geom_treemap()\n```\n\nThis plot isn't very useful without the knowing what country is represented by\neach tile. `geom_treemap_text()` can be used to add a text label to each tile. It\nuses the [ggfittext](https://github.com/wilkox/ggfittext) package to resize\nthe text so it fits the tile. In addition to standard text formatting aesthetics\nyou would use in `geom_text()`, like `fontface` or `colour`, we can pass\nadditional options specific for ggfittext. For example, we can place the text\nin the centre of the tile with `place = \"centre\"`, and expand it to fill as much\nof the tile as possible with `grow = TRUE`.\n\n```{r geom_treemap_text}\nggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country)) +\n  geom_treemap() +\n  geom_treemap_text(fontface = \"italic\", colour = \"white\", place = \"centre\",\n                    grow = TRUE)\n```\n\n## Subgrouping tiles\n\n`geom_treemap()` supports subgrouping of tiles within a treemap by passing a\n`subgroup` aesthetic. Let's subgroup the countries by region, draw a border\naround each subgroup with `geom_treemap_subgroup_border()`, and label each\nsubgroup with `geom_treemap_subgroup_text()`. `geom_treemap_subgroup_text()`\ntakes the same arguments for text placement and resizing as\n`geom_treemap_text()`.\n\n```{r subgrouped_treemap}\nggplot(G20, aes(area = gdp_mil_usd, fill = hdi, label = country,\n                subgroup = region)) +\n  geom_treemap() +\n  geom_treemap_subgroup_border() +\n  geom_treemap_subgroup_text(place = \"centre\", grow = T, alpha = 0.5, colour =\n                             \"black\", fontface = \"italic\", min.size = 0) +\n  geom_treemap_text(colour = \"white\", place = \"topleft\", reflow = T)\n```\n\nNote that Argentina is not labelled. `geom_treemap_text()` will hide text\nlabels that cannot fit a tile without being shrunk below a minimum size, by\ndefault 4 points. This can be adjusted with the `min.size` argument.\n\nUp to three nested levels of subgrouping are supported with the `subgroup2` and\n`subgroup3` aesthetics. Borders and text labels for these subgroups can be\ndrawn with `geom_treemap_subgroup2_border()`, etc. Note that ggplot2 draws plot\nlayers in the order that they are added. This means it is possible to\naccidentally hide one layer of subgroup borders with another. Usually, it's\nbest to add the border layers in order from deepest to shallowest, i.e.\n`geom_treemap_subgroup3_border()` then `geom_treemap_subgroup2_border()` then\n`geom_treemap_subgroup_border()`.\n\n```{r multiple_subgrouped_treemap}\nggplot(G20, aes(area = 1, label = country, subgroup = hemisphere,\n                subgroup2 = region, subgroup3 = econ_classification)) +\n  geom_treemap() +\n  geom_treemap_subgroup3_border(colour = \"blue\", size = 1) +\n  geom_treemap_subgroup2_border(colour = \"white\", size = 3) +\n  geom_treemap_subgroup_border(colour = \"red\", size = 5) +\n  geom_treemap_subgroup_text(place = \"middle\", colour = \"red\", alpha = 0.5, grow = T) +\n  geom_treemap_subgroup2_text(colour = \"white\", alpha = 0.5, fontface = \"italic\") +\n  geom_treemap_subgroup3_text(place = \"top\", colour = \"blue\", alpha = 0.5) +\n  geom_treemap_text(colour = \"white\", place = \"middle\", reflow = T)\n```\n\nAs demonstrated, there is no assurance that the resulting plot will look good.\n\nLike any ggplot2 plot, treemapify plots can be faceted, scaled, themed, etc.\n\n```{r complex_treemap}\nggplot(G20, aes(area = gdp_mil_usd, fill = region, label = country, subgroup = region)) +\n  geom_treemap() +\n  geom_treemap_text(grow = T, reflow = T, colour = \"black\") +\n  facet_wrap( ~ hemisphere) +\n  scale_fill_brewer(palette = \"Set1\") +\n  theme(legend.position = \"bottom\") +\n  labs(\n    title = \"The G-20 major economies by hemisphere\",\n    caption = \"The area of each tile represents the country's GDP as a\n      proportion of all countries in that hemisphere\",\n    fill = \"Region\"\n  )\n```\n\n## Animated treemaps\n\nThe default algorithm for laying out the tiles is the 'squarified' algorithm.\nThis tries to minimise the tiles' aspect ratios, making sure there are no long\nand flat or tall and skinny tiles. While 'squarified' treemaps are aesthetically\npleasing, the downside is that the position of tiles within the plot area can\nchange dramatically with even small changes to the dataset. This makes it\ndifficult to compare treemaps side-by-side, or create animated treemaps.\n\nBy providing the `layout = \"fixed\"` option to treemapify geoms, an alternative\nlayout algorithm is used that will always position the tiles based on the order\nof observations in the data frame. It's very important that the same value for\n`layout` is passed to all treemapify geoms, otherwise different layers of the\nplot might not share the same layout.\n\nWith the help of `layout = \"fixed\"`, and with the\n[`gganimate`](https://github.com/thomasp85/gganimate) package, it becomes possible\nto create animated treemaps showing e.g. change over time.\n\n```{r animated treemap, message = FALSE, results = FALSE}\nlibrary(gganimate)\nlibrary(gapminder)\n\np \u003c- ggplot(gapminder, aes(\n    label = country,\n    area = pop,\n    subgroup = continent,\n    fill = lifeExp\n  )) +\n  geom_treemap(layout = \"fixed\") +\n  geom_treemap_text(layout = \"fixed\", place = \"centre\", grow = TRUE, colour = \"white\") +\n  geom_treemap_subgroup_text(layout = \"fixed\", place = \"centre\") +\n  geom_treemap_subgroup_border(layout = \"fixed\") +\n  transition_time(year) +\n  ease_aes('linear') +\n  labs(title = \"Year: {frame_time}\")\n\nanim_save(\"man/figures/animated_treemap.gif\", p, nframes = 48)\n```\n\n![An example of an animated treemap](man/figures/animated_treemap.gif)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilkox%2Ftreemapify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwilkox%2Ftreemapify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwilkox%2Ftreemapify/lists"}