squaRify
is an R package for creating treemaps using base R. Treemaps are an effective way of visualizing hierarchical data by representing nested rectangles within larger rectangles. This package allows users to generate customizable treemaps to visually represent data structures efficiently.
- Base R Implementation: Utilizes native R functions to create treemaps without relying on any external libraries.
- Customizable: Users can modify colors, sizes, labels, and other visual aspects of the treemap.
- Hierarchical Visualization: Effectively displays hierarchical data through nested rectangles.
You can install the package from GitHub using the remotes
package:
remotes::install_github(repo = "poisonalien/squaRify")
library(squaRify)
#example data (GDP of G7 countries - in trillions)
gdp = c(26.85, 4.41, 4.30, 3.15, 2.92, 2.17, 2.09)
gdp_countries = c("U.S.", "Japan", "Germany", "UK", "France", "Italy", "Canada")
squaRify::squarify(X = gdp, labels = gdp_countries)
#example data (GDP of G7 and BRICS countries - in trillions)
g7 = c(26.85, 4.41, 4.30, 3.15, 2.92, 2.17, 2.09)
g7_countries = c("U.S.", "Japan", "Germany", "UK", "France", "Italy", "Canada")
brics = c(19.37, 3.73, 2.08, 2.06, 0.39)
gbrics_countries = c("China", "India", "Brazil", "Russia", "SA")
par(mfrow = c(1, 2), mar = c(0, 0, 3, 0))
squaRify::squarify(
X = g7,
labels = g7_countries,
col = "#273c75",
alpha = 0.9,
text_col = "white",
borderCol = "white",
sub_labels = paste0(g7, "T"),
sub_text_col = "white",
text_font = 2
)
title(main = "G7 $45.89T total")
squarify::squarify(
X = brics,
labels = brics_countries,
col = "#c23616",
alpha = 0.9,
text_col = "white",
borderCol = "white",
sub_labels = paste0(brics, "T"),
sub_text_col = "white",
text_font = 2
)
title(main = "BRICS $27.63T total")
The main algorithm is a direct translation of the squarify python package and, the main credit goes to Uri Laserson.