Learn R Programming

treemap (version 2.0.1)

treemap: Treemap

Description

A treemap is a space-filling visualization of hierarchical structures. This function offers great flexibility to draw treemaps. Required is a data.frame (dtf) that contains one or more hierarchical index columns given by index, a column that determines the rectangle area sizes (vSize), and optionally a column that determines the rectangle colors (vColor). The way how rectangles are colored is determined by the argument type.

Usage

treemap(dtf, index, vSize, vColor = NULL, type = "index",
    title = NA, title.legend = NA, algorithm = "pivotSize",
    sortID = "-size", palette = NA,
    palette.HCL.options = NULL, range = NA,
    fontsize.title = 14, fontsize.labels = 11,
    fontsize.legend = 12, lowerbound.cex.labels = 0.4,
    inflate.labels = FALSE, bg.labels = NULL,
    force.print.labels = FALSE, overlap.labels = 0.5,
    position.legend = NULL, drop.unused.levels = TRUE,
    aspRatio = NA, vp = NULL)

Arguments

dtf
a data.frame. Required.
index
vector of column names in dtf that specify the aggregation indices. It could contain only one column name, which results in a treemap without hierarchy. If multiple column names are provided, the first name is the highest aggregat
vSize
name of the column in dtf that specifies the sizes of the rectangles. Required.
vColor
name of the column that, in combination with type, determines the colors of the rectangles. The variable can be scaled by the addition of "*" or "/".
type
type of the treemap, which determines how the rectangles are colored: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
title
title of the treemap.
title.legend
title of the legend.
algorithm
name of the used algorithm: "squarified" or "pivotSize". The squarified treemap algorithm (Bruls et al., 2000) produces good aspect ratios, but ignores the sorting order of the rectangles (sortID). The or
sortID
name of the variable that determines the order in which the rectangles are placed from top left to bottom right. Only applicable when algorithm=="pivotSize". Also the values "size" and "color" can be used, which refer to vSi
palette
one of the following: [object Object],[object Object],[object Object]
palette.HCL.options
list of advanced options to pick colors from the HCL space (when palette="HCL"). This list contains: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object] For "de
range
range of values that determine the colors. Only applicable for types "value", "comp", and "dens". When omitted, the range of actual values is used. This range is mapped to palette.
fontsize.title
(maximum) font size of the title
fontsize.labels
font size(s) of the data labels, which can be:
  • one number, which specifies the font size for all aggregation levels
  • vector of two numbers, which specific the font sizes for 1) the highest and 2) the other aggregation levels
fontsize.legend
(maximum) font size of the legend
lowerbound.cex.labels
multiplier between 0 and 1 that sets the lowerbound for the data label font sizes: 0 means draw all data labels, and 1 means only draw data labels if they fit (given fontsize.labels).
inflate.labels
logical that determines whether data labels are inflated inside the rectangles. If TRUE, fontsize.labels is does not determine the maximum fontsize, but it does determine in combination with lowerbound.cex.labels the
bg.labels
background color of high aggregation labels. Either a color, or a number between 0 and 255 that determines the transparency of the labels. In the latter case, the color itself is determined by the color of the underlying rectangle. For "value"
force.print.labels
logical that determines whether data labels are being forced to be printed if they don't fit.
overlap.labels
number between 0 and 1 that determines the tolerance of the overlap between labels. 0 means that labels of lower levels are not printed if higher level labels overlap, 1 means that labels are always printed. In-between values, for instance the
position.legend
position of the legend: "bottom", "right", or "none". For "categorical" and "index" treemaps, "right" is the default value, for "index" treemap, "none", and for the other types,
drop.unused.levels
logical that determines whether unused levels (if any) are shown in the legend. Applicable for "categorical" treemap type.
aspRatio
preferred aspect ratio of the main rectangle, defined by width/height. When set to NA, the available window size is used.
vp
viewport to draw in. By default it is not specified, which means that a new plot is created. Useful when drawing small multiples, or when placing a treemap in a custom grid based plot

Value

  • A list is silently returned:
  • tma data.frame containing information about the rectangles
  • typeargument type
  • vSizeargument vSize
  • vColorargument vColor
  • algorithmargument algorithm

References

Bederson, B., Shneiderman, B., Wattenberg, M. (2002) Ordered and Quantum Treemaps: Making Effective Use of 2D Space to Display Hierarchies. ACM Transactions on Graphics, 21(4): 833-854. Bruls, D.M., C. Huizing, J.J. van Wijk. Squarified Treemaps. In: W. de Leeuw, R. van Liere (eds.), Data Visualization 2000, Proceedings of the joint Eurographics and IEEE TCVG Symposium on Visualization, 2000, Springer, Vienna, p. 33-42.

Examples

Run this code
#########################################
### quick example with Gross National Income data
#########################################
data(GNI2010)
treemap(GNI2010,
       index=c("continent", "iso3"),
       vSize="population",
       vColor="GNI",
       type="value")

#########################################
### extended examples with fictive business statistics data
#########################################
data(business)

#########################################
### treemap types
#########################################

# index treemap: colors are determined by the index argument
# large example which takes some time...
treemap(business, 
        index=c("NACE1", "NACE2", "NACE3"), 
        vSize="turnover", 
        type="index")
treemap(business[business$NACE1=="C - Manufacturing",],
        index=c("NACE2", "NACE3"),
        vSize=c("employees"),
        type="index")

# comparisson treemaps: colors indicate change of vSize with respect to vColor
treemap(business, 
	   index=c("NACE1", "NACE2"), 
	   vSize="employees", 
	   vColor="employees.prev",
	   type="comp")

# density treemaps: colors indicate density (like a population density map)
treemap(business,
	   index=c("NACE1", "NACE2"), 
	   vSize="turnover",
	   vColor="employees/1000",
	   type="dens")

# categorical treemap: colors are determined by a categorical variable
business <- transform(business, data.available = factor(!is.na(turnover)), 
                      x = 1)
treemap(business, 
       index=c("NACE1", "NACE2"), 
	   vSize="x", 
	   vColor="data.available",
	   type="categorical")

#########################################
### graphical options: control fontsizes
#########################################

# draw labels at fixed fontsize 12 (if they fit)
treemap(business, 
	   index=c("NACE1", "NACE2"), 
	   vSize="employees", 
	   fontsize.labels=12, 
	   lowerbound.cex.labels=1)

# draw labels at fontsize (.6*12) to 12 (if they fit)
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       fontsize.labels=12, 
	   lowerbound.cex.labels=.6)

# draw all labels at maximal fontsize
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
	   lowerbound.cex.labels=0, 
	   inflate.labels = TRUE)

# draw all labels at fixed fontsize, even if they don't fit
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       fontsize.labels=10, 
	   lowerbound.cex.labels=1, 
	   force.print.labels=TRUE)

#########################################
### graphical options: color palettes
#########################################

## for comp and value typed treemaps all diverging brewer palettes can be chosen
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       vColor="employees.prev",
       type="comp",
       palette="RdBu")

## index treemaps, with palette="HCL":
palette.HCL.options <- list(hue_start=270, hue_end=360+150)
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       type="index",
       palette="HCL",
       palette.HCL.options=palette.HCL.options)

# terrain colors
business$employees.growth <- business$employees - business$employees.prev
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       vColor="employees.growth", 
	   type="value", 
	   palette=terrain.colors(10))

# Brewer's Red-White-Grey palette reversed with predefined range
treemap(business, 
       index=c("NACE1", "NACE2"), 
       vSize="employees", 
       vColor="employees.growth", 
	   type="value", 
	   palette="-RdGy", 
	   range=c(-30000,30000))

Run the code above in your browser using DataLab