5

I am looking to make some kind of proportional squares (by lack of a better name) visualization in R. Example:

Billion Dollar O-gram

Any advice on how to do this in R (preferably ggplot2)?

  • After learning how to use the tree map package it may be helpful to convert your plot into ggplot2, for ease with formatting. See: https://stackoverflow.com/questions/42866566/converting-treemap-to-ggplot – jesstme Aug 17 '17 at 14:35

1 Answers1

10

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.

Treemap package

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 = " ")
)

The output of the treemap function

ggplot2

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
  )

enter image description here

Community
  • 1
  • 1
Andrew Brēza
  • 7,705
  • 3
  • 34
  • 40