stat_treemap: Calculate a treemap from data

Description Usage Arguments Details Aesthetics Examples

View source: R/stat-treemap.R

Description

A stat to turn data into treemaps.

Usage

1
2
3
stat_treemap(mapping = NULL, data = NULL, geom = "rect",
  position = "identity", na.rm = FALSE, show.legend = NA,
  inherit.aes = TRUE, label.position = c(0.5, 0.5), aspect.ratio = 1, ...)

Arguments

mapping

Set of aesthetic mappings created by aes() or aes_(). If specified and inherit.aes = TRUE (the default), it is combined with the default mapping at the top level of the plot. You must supply mapping if there is no plot mapping.

data

The data to be displayed in this layer. There are three options:

If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot().

A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame. See fortify() for which variables will be created.

A function will be called with a single argument, the plot data. The return value must be a data.frame., and will be used as the layer data.

geom

The geometric object to use display the data

position

Position adjustment, either as a string, or the result of a call to a position adjustment function.

show.legend

logical. Should this layer be included in the legends? NA, the default, includes if any aesthetics are mapped. FALSE never includes, and TRUE always includes. It can also be a named logical vector to finely select the aesthetics to display.

inherit.aes

If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders().

label.position

If used with a geom that uses a single x, y location ( most likely geom_text), this determines where in each treemap box that location is set to, using a two element numeric vector. These are used to locate the point within the box.

aspect.ratio

(width / height) The standard 'squarified' algorithm aims to produce square-ish treemap boxes, and this aspect ratio determines what 'square-ish' is. If you set it to the actual aspect ratio of the output device, you will ensure square-ishness.

Details

Use with geom_rect, mostly.

Aesthetics

stat_treemap accepts the following aesthetics:

and applies the layout algorithm to produce

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggtreemap)

df <- data.frame(
  label = c("a1", "a2", "b1", "c1", "c2"),
  group = c("a", "a", "b", "c", "c"),
  size = c(0.5,4,2,1,0.5)
)

ggplot(df, aes(area = size, fill = group, subgroup = group)) +
  geom_rect(stat = "treemap", mapping = aes(layout_area = size * 2), color = "white") +
  geom_rect(stat = "treemap", alpha = 0.5, color = "white") +
  theme_minimal() +
  theme(axis.text = element_blank(), panel.grid = element_blank())

ggplot(df, aes(area = size, fill = group, subgroup = group)) +
  geom_rect(stat = "treemap", color = "white") +
  geom_text(aes(label = label), stat = "treemap") +
  geom_rect(data = aggregate(size ~ group, df, sum), stat="treemap", fill = NA, color = "white", size=2) +
  geom_label(
    data = aggregate(size ~ group, df, sum),
    aes(label = group),
    label.position = c(0, 0),
    hjust = 0, vjust = 1,
    nudge_x = 0.025, nudge_y = -0.025,
    fill = "white",
    stat = "treemap") +
  scale_y_reverse() +
  theme_minimal()

econandrew/ggtreemap documentation built on May 28, 2019, 8:35 p.m.