I am looking to make some kind of proportional squares (by lack of a better name) visualization in R. Example:
Any advice on how to do this in R (preferably ggplot2)?
This type of visualization is called a treemap. Appropriately, you can use the treemap
package. You can find a detailed tutorial for treemap
here but I'll show you the basics. Below I show you how to create a treemap in ggplot2
as well.
library(treemap)
cars <- mtcars
cars$carname <- rownames(cars)
treemap(
cars,
index = "carname",
vSize = "disp",
vColor = "cyl",
type = "value",
format.legend = list(scientific = FALSE, big.mark = " ")
)
There's also a developmental package on github for creating treemaps using ggplot2
. Here's the repo for installing the package.
library(tidyverse)
library("ggfittext")
library("treemapify")
cars <- mtcars
cars$carname <- rownames(cars)
cars <- mutate(cars, cyl = factor(cyl))
ggplot(cars, aes(area = disp, fill = cyl, label = carname)) +
geom_treemap() +
geom_treemap_text(
fontface = "italic",
colour = "white",
place = "centre",
grow = TRUE
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With