/
treemap.R
181 lines (158 loc) · 5.25 KB
/
treemap.R
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#########################################
### quick example with Gross National Income data
#########################################
data(GNI2014)
treemap(GNI2014,
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
\dontrun{
# 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")
# value treemap: colors are derived from a numeric variable given by vColor
# (when omited, all values are set to 1 as in the following example)
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
title.legend="number of NACE4 categories",
type="value")
# 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")
\dontrun{
# depth treemap: show depth
treemap(business,
index=c("NACE1", "NACE2", "NACE3"),
vSize="turnover",
type="depth")
}
# 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")
\dontrun{
# color treemap
business$color <- rainbow(nlevels(business$NACE2))[business$NACE2]
treemap(business,
index=c("NACE1", "NACE2"),
vSize="x",
vColor="color",
type="color")
# manual
business$color <- rainbow(nlevels(business$NACE2))[business$NACE2]
treemap(business,
index=c("NACE1", "NACE2"),
vSize="turnover",
vColor="employees",
type="manual",
palette=terrain.colors(10))
}
#########################################
### graphical options: control fontsizes
#########################################
\dontrun{
# draw labels of first index at fontsize 12 at the center,
# and labels of second index at fontsize 8 top left
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
fontsize.labels=c(12, 8),
align.labels=list(c("center", "center"), c("left", "top")),
lowerbound.cex.labels=1)
# draw all labels at fontsize 12 (only if they fit)
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
fontsize.labels=12,
lowerbound.cex.labels=1)
# draw all labels at fontsize 12, and if they don't fit, reduce to a minimum of .6*12
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")
## draw warm-colored index treemap
palette.HCL.options <- list(hue_start=270, hue_end=360+150)
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
type="index",
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 legend range
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
vColor="employees.growth",
type="value",
palette="-RdGy",
range=c(-20000,30000))
# More control over the color palette can be achieved with mapping
treemap(business,
index=c("NACE1", "NACE2"),
vSize="employees",
vColor="employees.growth",
type="value",
palette="RdYlGn",
range=c(-20000,30000), # this is shown in the legend
mapping=c(-30000, 10000, 40000)) # Rd is mapped to -30k, Yl to 10k, and Gn to 40k
}