Description Usage Arguments Details Aesthetics Examples
A stat to turn data into treemaps.
1 2 3 |
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
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?
|
inherit.aes |
If |
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. |
Use with geom_rect
, mostly.
stat_treemap
accepts the following aesthetics:
area: the column used to scale the drawn areas of the treemap
layout_area: an optional column used for areas that will be used for layout but not drawn
subgroup: a column used for grouping of areas
and applies the layout algorithm to produce
x, xmin, xmax
y, ymin, ymax
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()
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.