Learn R Programming

treemap (version 1.1-1)

tmPlot: Create treemap

Description

Create treemap

Usage

tmPlot(dtf, index, vSize, vColor = NULL, type = "value",
    title = NA, subtitle = NA, algorithm = "pivotSize",
    sortID = "-size", palette = NA, range = NA,
    vColorRange = NULL, fontsize.title = 14,
    fontsize.labels = 11, fontsize.legend = 12,
    lowerbound.cex.labels = 0.4, inflate.labels = FALSE,
    bg.labels = ifelse(type %in% c("value", "linked", "categorical"), "#CCCCCCAA", NA),
    force.print.labels = FALSE,
    position.legend = ifelse(type %in% c("categorical", "index"), "right", ifelse(type == "linked", "none", "bottom")),
    aspRatio = NA, na.rm = FALSE)

Arguments

dtf
a data.frame. Required.
index
vector containing the column names in dtf that contain the aggregation indices. Required.
vSize
name of the variable that determines the sizes of the rectangles. For small multiples, a vector of variable names (one for each treemap) should be given. Required.
vColor
name of the variable that, in combination with type, determines the colors of the rectangles. The variable can be scaled by the addition of "*" or "/". For small multiples, a vector of variable names (o
type
the type of the treemap: [object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
title
title of the treemap. For small multiples, a vector of titles should be given. Titles are used to describe the sizes of the rectangles.
subtitle
subtitle of the treemap. For small multiples, a vector of subtitles should be given. Subtitles are used to describe the colors of the rectangles.
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. Also the values "size" and "color" can be used. To inverse the sorting order, use "-" in the prefix. By default, large rectangles
palette
either a color palette or a name of a Brewer palette (see display.brewer.all()). A Brewer palette can be reversed by prefixing its name with a "-".
range
range of values that determine the colors. When omitted, the range of actual values is used. This range is mapped to palette.
vColorRange
deprecated, use range instead.
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 at fontsize.data.
inflate.labels
logical that determines whether data labels are inflated inside the rectangles.
bg.labels
background color of labels of high aggregation levels. If set to NA, the color is determined by the color of the underlying rectangle. For value, categorical and linked treemaps, the default is transparent grey ("#CCCCCCAA"<
force.print.labels
logical that determines whether data labels are being forced to be printed (also when they don't fit).
position.legend
position of the legend: "bottom", "right", or "none". For categorical and index treemaps, "right" is the default value, for linked treemap, "none", and for the other types,
aspRatio
preferred aspect ratio of the main rectangle, defined by width/height. When set to NA, the available window size is used.
na.rm
logical that determines whether missing values are omitted during aggregation

Value

  • A list is silently returned:
  • tmlist with for each treemap a data.frame containing information about the rectangles
  • nRownumber of rows in the treemap grid
  • nColnumber of rows in the treemap grid
  • vSizeargument vSize
  • vColorargument vColor
  • This list can be used to locate a mouse click (see tmLocate).

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
###
### data: Gross national income data
#########################################

# load Gross national income data
data(GNI2010)

# create treemap
tmPlot(GNI2010,
       index=c("continent", "iso3"),
       vSize="population",
       vColor="GNI",
       type="value")



#########################################
### extended examples
###
### data: fictive structural business statistics data
#########################################

### load fictive structural business statistics data
data(sbsData)
sbsData$employees.growth <- sbsData$employees09 - sbsData$employees08


#########################################
### types
#########################################

# value treemap: the color variable is directly mapped to the colors
tmPlot(sbsData, 
       index=c("section", "subsection"), 
       vSize="employees09", 
       vColor="employees.growth",
       type="value")

# comparisson treemaps: colors indicate change of vSize with respect to vColor
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees08",
	   type="comp")

# four comparisson treemaps
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize=c("employees09", "value added09", "turnover09", "salaries09"), 
	   vColor=c("employees08", "value added08", "turnover08", "salaries08"),
	   type="comp")

# density treemaps: colors indicate density (like a population density map)
tmPlot(sbsData,
	   index=c("section", "subsection"), 
	   vSize="turnover09",
	   vColor="employees09*1000",
	   type="dens")

tmPlot(sbsData,
	   index=c("section", "subsection"), 
	   vSize="employees09",
	   vColor="turnover09",
	   type="dens")

# linked treemaps: objects are linked by color over different treemaps
tmPlot(sbsData[sbsData$section=="Manufacturing",],
	   index="subsection",
	   vSize=c("income09", "employees09", "expenditures09", "salaries09"),
	   type="linked")

# index treemap: each aggregation index has distinct color
tmPlot(sbsData,
	   index=c("section", "subsection"), 
	   vSize="employees09",
	   type="index")

# categorical treemap: colors are determined by a categorical variable
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="section",
	   type="categorical")

#########################################
### layout algorithm
#########################################

tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees.growth",
	   type="value",
	   algorithm="squarified")

#########################################
### graphical options: fontsize
#########################################

# draw labels at fixed fontsize (fit only)
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees08",
	   type="comp",
	   fontsize.labels=12, 
	   lowerbound.cex.labels=1)

# draw labels at flexible fontsize (skip tiny rectangles)
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees08", 
	   type="comp",
	   fontsize.labels=12, 
	   lowerbound.cex.labels=.6)

# draw labels at maximal fontsize
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees08", 
	   type="comp",
	   fontsize.labels=10, 
	   lowerbound.cex.labels=1, 
	   inflate.labels = TRUE)

# draw all labels at fixed fontsize
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees08", 
	   type="comp",
	   fontsize.labels=10, 
	   lowerbound.cex.labels=1, 
	   force.print.labels=TRUE)

#########################################
### graphical options: color palette
#########################################

# terrain colors
sbsData$employees.growth <- sbsData$employees09 - sbsData$employees08
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees.growth", 
	   type="value", 
	   palette=terrain.colors(10))

# Brewer's Red-White-Grey palette reversed with predefined range
tmPlot(sbsData, 
	   index=c("section", "subsection"), 
	   vSize="employees09", 
	   vColor="employees.growth", 
	   type="value", 
	   palette="-RdGy", 
	   range=c(-20000,20000))

Run the code above in your browser using DataLab