Title: | Draw Treemaps in 'ggplot2' |
---|---|
Description: | Provides 'ggplot2' geoms for drawing treemaps. |
Authors: | David Wilkins [aut, cre] |
Maintainer: | David Wilkins <[email protected]> |
License: | GPL (>=3) |
Version: | 2.5.6.9004 |
Built: | 2024-08-14 05:17:54 UTC |
Source: | https://github.com/wilkox/treemapify |
Round rect key glyph for legend
draw_key_rrect(data, params, size)
draw_key_rrect(data, params, size)
data |
A single row data frame containing the scaled aesthetics to display in this key |
params |
A list of additional parameters supplied to the geom. |
size |
Width and height of key in mm. |
Bob Rudis ([email protected])
A dataset containing economic and demographic statistics about members of the G-20 group of major world economies.
G20
G20
A data frame with 20 rows and five variables:
the country's region
the country
the country's GDP, in millions of US dollars
the country's Human Development Index
the country's economic classification
the hemisphere in which the majority of the country's landmass lies
https://en.wikipedia.org/wiki/G-20_major_economies
A treemap is a rectangular plot divided into tiles, each of which represents a single observation. The relative area of each tile expresses a continuous variable.
geom_treemap( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, fixed = NULL, layout = "squarified", start = "bottomleft", radius = grid::unit(0, "pt"), ... )
geom_treemap( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, fixed = NULL, layout = "squarified", start = "bottomleft", radius = grid::unit(0, "pt"), ... )
mapping , data , stat , position , na.rm , show.legend , inherit.aes , ...
|
Standard
geom arguments as for |
fixed |
Deprecated. Use |
layout |
The layout algorithm, one of either 'squarified' (the default), 'scol', 'srow' or 'fixed'. See Details for full details on the different layout algorithms. |
start |
The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. |
radius |
corner radius (default 0pt) |
geom_treemap()
requires an area
aesthetic. It will ignore any aesthetics
relating to the x and y axes (e.g. xmin
or y
), as the x and y axes are
not meaningful in a treemap. Several other standard 'ggplot2' aesthetics are
supported (see Aesthetics). To add text labels to tiles, see
geom_treemap_text()
.
An optional subgroup
aesthetic will cause the tiles to be clustered in
subgroups within the treemap. See geom_treemap_subgroup_border()
and
geom_treemap_subgroup_text()
to draw borders around subgroups and label
them, respectively. Up to three nested levels of subgrouping are supported,
with subgroup2
and subgroup3
aesthetics and respective
geom_treemap_subgroup2_border()
etc. geoms.
Four layout algorithms are provided. With the default 'squarified' algorithm
(layout = "squarified"
), the priority is ensuring the tiles have an
aesthetically pleasing aspect ratio; that is, they are not too narrow or too
short. In this algorithm, tile placement proceeds from one corner, placing
the tiles in either rows or columns until all the tiles are placed. See
Bruls et al. (1999) for the full algorithm.
There are two variants on the 'squarified' algorithm. 'scol' forces tile placement to begin with a column, regardless of the effect on aspect ratio; 'srow' forces tile placement to been with a row. This will also apply to all subgroups. After the first row or column, the remaining tiles will be placed so as to optimise aspect ratios, as with the default algorithm.
With the 'fixed' layout algorithm (layout = "fixed"
), the plot area is
divided into vertical columns, which are each filled with an equal number
of tiles beginning at the starting corner. Unlike the 'squarified'
algorithm, with the 'fixed' algorithm the relative positions of the tiles
are fixed by their order in the input data frame. This can result in
aesthetically unpleasing layouts, but it allows side-by-side comparisons or
animations to be created.
All 'treemapify' geoms added to a plot should have the same value for
layout
and start
, or they will not share a common layout.
area (required)
alpha
colour
fill
linetype
subgroup
subgroup2
subgroup3
David Wilkins ([email protected])
Bob Rudis ([email protected])
Bruls, M., Huizing, K., & van Wijk, J. (1999). Squarified Treemaps (pp. 33-42). Proceedings of the Joint Eurographics and IEEE TCVG Symposium on Visualization. https://www.win.tue.nl/~vanwijk/stm.pdf
geom_treemap_text()
, geom_treemap_subgroup_border()
,
geom_treemap_subgroup_text()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = region)) + geom_treemap()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = region)) + geom_treemap()
When geom_treemap()
is used with a subgroup
, subgroup2
or subgroup3
aesthetic to subgroup treemap tiles, geom_treemap_subgroup_border
,
geom_treemap_subgroup2_border()
or geom_treemap_subgroup3_border()
can
be used to draw a border around each subgroup at the appropriate level.
geom_treemap_subgroup_border( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, fixed = NULL, layout = "squarified", start = "bottomleft", level = "subgroup", ... ) geom_treemap_subgroup2_border(...) geom_treemap_subgroup3_border(...)
geom_treemap_subgroup_border( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, fixed = NULL, layout = "squarified", start = "bottomleft", level = "subgroup", ... ) geom_treemap_subgroup2_border(...) geom_treemap_subgroup3_border(...)
mapping , data , stat , position , na.rm , show.legend , inherit.aes , ...
|
Standard
geom arguments as for |
fixed |
Deprecated. Use |
layout |
The layout algorithm, one of either 'squarified' (the default), 'scol', 'srow' or 'fixed'. See Details for full details on the different layout algorithms. |
start |
The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. |
level |
One of 'subgroup', 'subgroup2' or 'subgroup3', giving the
subgrouping level for which to draw borders. It is recommended to use the
aliases |
geom_treemap_subgroup_border()
geoms require area
and subgroup
(or
subgroup2,
subgroup3‘) aesthetics. Several other standard ’ggplot2'
aesthetics are supported (see Aesthetics).
Note that 'ggplot2' draws plot layers in the order they are added to the
plot. This means that if you add a geom_treemap_subgroup_border()
layer
followed by a geom_treemap_subgroup2_border()
layer, the second layer will
be drawn on top of the first and may hide it.
The layout
argument is used to set the treemap layout algorithm. All
'treemapify' geoms added to a plot should have the same value for layout
and start
, or they will not share a common layout (see geom_treemap()
for
details on the layout algorithms).
area (required)
subgroup, subgroup2 or subgroup3 (required)
colour
size
linetype
alpha
geom_treemap()
, geom_treemap_subgroup_text()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = hdi, subgroup = hemisphere, subgroup2 = region)) + geom_treemap() + geom_treemap_subgroup2_border(colour = "white") + geom_treemap_subgroup_border()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = hdi, subgroup = hemisphere, subgroup2 = region)) + geom_treemap() + geom_treemap_subgroup2_border(colour = "white") + geom_treemap_subgroup_border()
When geom_treemap()
is used with the subgroup
, subgroup2
or
subgroup3
aesthetic to subgroup treemap tiles,
geom_treemap_subgroup_text()
, geom_treemap_subgroup2_text()
or
geom_treemap_subgroup3_text()
can be used to add a text label to each
subgroup at the appropriate level.
geom_treemap_subgroup_text( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, padding.x = grid::unit(1, "mm"), padding.y = grid::unit(1, "mm"), place = "bottom", min.size = 4, grow = FALSE, reflow = FALSE, fixed = NULL, layout = "squarified", start = "bottomleft", level = "subgroup", ... ) geom_treemap_subgroup2_text(...) geom_treemap_subgroup3_text(...)
geom_treemap_subgroup_text( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, padding.x = grid::unit(1, "mm"), padding.y = grid::unit(1, "mm"), place = "bottom", min.size = 4, grow = FALSE, reflow = FALSE, fixed = NULL, layout = "squarified", start = "bottomleft", level = "subgroup", ... ) geom_treemap_subgroup2_text(...) geom_treemap_subgroup3_text(...)
mapping , data , stat , position , na.rm , show.legend , inherit.aes , ...
|
Standard
geom arguments as for |
padding.x , padding.y
|
|
place |
Where inside the box to place the text. Default is |
min.size |
Minimum font size, in points. If provided, text that would need to be shrunk below this size to fit the box will not be drawn. Defaults to 4 pt. |
grow |
If |
reflow |
If |
fixed |
Deprecated. Use |
layout |
The layout algorithm, one of either 'squarified' (the default), 'scol', 'srow' or 'fixed'. See Details for full details on the different layout algorithms. |
start |
The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. |
level |
One of 'subgroup', 'subgroup2' or 'subgroup3', giving the
subgrouping level for which to draw text labels. It is recommended to use
the aliases |
geom_treemap_subgroup_text()
geoms require area
, label
and subgroup
(or subgroup2
, subgroup3
) aesthetics. Several other standard 'ggplot2'
aesthetics are supported (see Aesthetics).
geom_treemap_subgroup_text()
geoms use the 'ggfittext' package to fit text
to the subgroup. All text drawing options available in
ggfittext::geom_fit_text()
(growing, reflowing, etc.) are also available
here. For full details on how these options work, see the documentation for
ggfittext::geom_fit_text()
.
The layout
argument is used to set the treemap layout algorithm. All
'treemapify' geoms added to a plot should have the same value for layout
and start
, or they will not share a common layout (see geom_treemap()
for details on the layout algorithms).
area (required)
subgroup, subgroup2 or subgroup3 (required; the value of this variable will be the text label)
colour
size
alpha
family
fontface
angle
geom_treemap()
, geom_treemap_subgroup_border()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = hdi, subgroup = hemisphere, subgroup2 = region)) + geom_treemap() + geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5) + geom_treemap_subgroup2_text()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = hdi, subgroup = hemisphere, subgroup2 = region)) + geom_treemap() + geom_treemap_subgroup_text(place = "centre", grow = TRUE, alpha = 0.5) + geom_treemap_subgroup2_text()
geom_treemap_text()
can be used to add a text label to each tile in a
treemap created with geom_treemap()
.
geom_treemap_text( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, padding.x = grid::unit(1, "mm"), padding.y = grid::unit(1, "mm"), place = "topleft", min.size = 4, grow = FALSE, reflow = FALSE, fixed = NULL, layout = "squarified", start = "bottomleft", ... )
geom_treemap_text( mapping = NULL, data = NULL, stat = "identity", position = "identity", na.rm = FALSE, show.legend = FALSE, inherit.aes = TRUE, padding.x = grid::unit(1, "mm"), padding.y = grid::unit(1, "mm"), place = "topleft", min.size = 4, grow = FALSE, reflow = FALSE, fixed = NULL, layout = "squarified", start = "bottomleft", ... )
mapping , data , stat , position , na.rm , show.legend , inherit.aes , ...
|
Standard
geom arguments as for |
padding.x , padding.y
|
|
place |
Where inside the box to place the text. Default is 'bottom'; other options are 'topleft', 'top', 'topright', etc. |
min.size |
Minimum font size, in points. If provided, text that would need to be shrunk below this size to fit the box will not be drawn. Defaults to 4 pt. |
grow |
If |
reflow |
If |
fixed |
Deprecated. Use |
layout |
The layout algorithm, one of either 'squarified' (the default), 'scol', 'srow' or 'fixed'. See Details for full details on the different layout algorithms. |
start |
The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. |
geom_treemap_text()
requires area
and label
aesthetics. Several other
standard 'ggplot2' aesthetics are supported (see Aesthetics).
geom_treemap_text()
uses the 'ggfittext' package to fit text to tiles. All
text drawing options available in ggfittext::geom_fit_text()
(growing,
reflowing, etc.) are also available here. For full details on how these
options work, see the documentation for ggfittext::geom_fit_text()
.
The layout
argument is used to set the treemap layout algorithm. All
'treemapify' geoms added to a plot should have the same value for layout
and start
, or they will not share a common layout (see geom_treemap()
for details on the layout algorithms).
area (required)
label (required)
subgroup, subgroup2 or subgroup3
colour
size
alpha
family
fontface
angle
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = econ_classification, label = country)) + geom_treemap() + geom_treemap_text()
ggplot2::ggplot(G20, ggplot2::aes(area = gdp_mil_usd, fill = econ_classification, label = country)) + geom_treemap() + geom_treemap_text()
treemapify()
returns a data frame of tile coordinates for a treemap layout
of a set of observations. This is only useful if you wish to draw the
treemap without the help of the ggplot2
geoms, or for some edge cases such
as creating interactive treemaps with 'R Shiny' (see e.g.
https://stackoverflow.com/q/45021775). The easiest way to draw a treemap
with the 'treemapify' package is to use the provided 'ggplot2' geoms, such
as geom_treemap()
.
data
must be a tidy data frame, i.e. each row must represent a single
observation and each column a single variable. You must provide the name of
the variable that will be represented by the area of each treemap tile with
area
. Optionally, you can also select up to three variables (with
subgroup
, subgroup2
and subgroup3
) to generate a layout in which the
tiles are clustered into subgroups nested up to three levels deep.
Four layout algorithms are provided. With the default 'squarified' algorithm
(layout = "squarified"
), the priority is ensuring the tiles have an
aesthetically pleasing aspect ratio; that is, they are not too narrow or too
short. In this algorithm, tile placement proceeds from one corner, placing
the tiles in either rows or columns until all the tiles are placed. See
Bruls et al. (1999) for the full algorithm.
There are two variants on the 'squarified' algorithm. 'scol' forces tile placement to begin with a column, regardless of the effect on aspect ratio; 'srow' forces tile placement to been with a row. This will also apply to all subgroups. After the first row or column, the remaining tiles will be placed so as to optimise aspect ratios, as with the default algorithm.
With the 'fixed' layout algorithm (layout = "fixed"
), the plot area is
divided into vertical columns, which are each filled with an equal number
of tiles beginning at the starting corner. Unlike the 'squarified'
algorithm, with the 'fixed' algorithm the relative positions of the tiles
are fixed by their order in the input data frame. This can result in
aesthetically unpleasing layouts, but it allows side-by-side comparisons or
animations to be created.
treemapify_fixed
is an alias for treemapify(layout = "fixed")
.
treemapify( data, area, subgroup, subgroup2, subgroup3, layout = "squarified", start = "bottomleft", fill = NULL, label = NULL, group = NULL, fixed = NULL, xlim = c(0, 1), ylim = c(0, 1) ) treemapify_fixed(...)
treemapify( data, area, subgroup, subgroup2, subgroup3, layout = "squarified", start = "bottomleft", fill = NULL, label = NULL, group = NULL, fixed = NULL, xlim = c(0, 1), ylim = c(0, 1) ) treemapify_fixed(...)
data |
A tidy data frame. |
area |
Name of the variable (a column in |
subgroup , subgroup2 , subgroup3
|
Optionally, names of variables (columns
in |
layout |
The layout algorithm, one of either 'squarified' (the default), 'scol', 'srow' or 'fixed'. See Details for full details on the different layout algorithms. |
start |
The corner in which to start placing the tiles. One of 'bottomleft' (the default), 'topleft', 'topright' or 'bottomright'. |
label , fill
|
Deprecated. Will be removed in later versions. |
group |
Deprecated. Use |
fixed |
Deprecated. Use |
xlim , ylim
|
The boundaries of the treemap in the x and y dimensions.
Must be a numeric vector of length two; both default to |
... |
Additional arguments to be passed to |
treemapify
is for generating a data frame of raw treemap coordinates. If
you want to draw a treemap with 'ggplot2', use geom_treemap()
instead.
Bruls, M., Huizing, K., & van Wijk, J. (1999). Squarified Treemaps (pp. 33-42).Proceedings of the Joint Eurographics and IEEE TCVG Symposium on Visualization. https://www.win.tue.nl/~vanwijk/stm.pdf
treemapify(G20, area = "gdp_mil_usd")
treemapify(G20, area = "gdp_mil_usd")