-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXpress)
1140 lines (900 loc) · 47.4 KB
/
Xpress)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
treemap package:treemap R Documentation
_C_r_e_a_t_e _a _t_r_e_e_m_a_p
_D_e_s_c_r_i_p_t_i_o_n:
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’.
_U_s_a_g_e:
treemap(dtf, index, vSize, vColor = NULL, stdErr = NULL, type = "index",
fun.aggregate = "sum", title = NA, title.legend = NA,
algorithm = "pivotSize", sortID = "-size", mirror.x = FALSE,
mirror.y = FALSE, palette = NA, palette.HCL.options = NULL,
range = NA, mapping = NA, n = 7, fontsize.title = 14,
fontsize.labels = 11, fontsize.legend = 12, fontcolor.labels = NULL,
fontface.labels = c("bold", rep("plain", length(index) - 1)),
fontfamily.title = "sans", fontfamily.labels = "sans",
fontfamily.legend = "sans", border.col = "black",
border.lwds = c(length(index) + 1, (length(index) - 1):1),
lowerbound.cex.labels = 0.4, inflate.labels = FALSE, bg.labels = NULL,
force.print.labels = FALSE, overlap.labels = 0.5,
align.labels = c("center", "center"), xmod.labels = 0, ymod.labels = 0,
eval.labels = FALSE, position.legend = NULL, format.legend = NULL,
drop.unused.levels = TRUE, aspRatio = NA, vp = NULL, draw = TRUE, ...)
_A_r_g_u_m_e_n_t_s:
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 aggregation level,
the second name the second-highest aggregation level, and so
on. Required.
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 "*<scale factor>" or "/<scale
factor>". Note: when omitted for ‘"value"’ treemaps, a
contant value of 1 is taken.
stdErr: name of the column that contains standard errors. These are
not used for the treemaps, but only aggregated accordingly
and returned as item of the output list.
type: type of the treemap, which determines how the rectangles are
colored:
‘"index"’: colors are determined by the ‘index’ variables.
Different branches in the hierarchical tree get different
colors. For this type, ‘vColor’ is not needed.
‘"value"’: the numeric ‘vColor’-column is directly mapped to
a color palette. This palette is diverging, so that
values of 0 are assigned to the mid color (white or
yellow), and negative and positive values are assigned to
color based on two different hues colors (by default reds
for negative and greens for positive values). For more
freedom, see ‘"manual"’.
‘"comp"’: colors indicate change of the ‘vSize’-column with
respect to the numeric ‘vColor’-column in percentages.
Note: the negative scale may be different from the
positive scale in order to compensate for the ratio
distribution.
‘"dens"’: colors indicate density. This is analogous to a
population density map where ‘vSize’-values are area
sizes, ‘vColor’-values are populations per area, and
colors are computed as densities (i.e. population per
squared km).
‘"depth"’: each aggregation level (defined by ‘index’) has a
distinct color. For this type, ‘vColor’ is not needed.
‘"categorical"’: ‘vColor’ is a factor column that determines
the color.
‘"color"’: ‘vColor’ is a vector of colors in the hexadecimal
(#RRGGBB) format
‘"manual"’: The numeric ‘vColor’-column is directly mapped to
a color palette. Both palette and range should be
provided. The palette is mapped linearly to the range.
fun.aggregate: aggregation function, only used in ‘"value"’ treemaps.
This function determines how values of the lowest aggregation
level are aggregated. By default, it takes the ‘sum’. Other
sensible functions are ‘mean’ and ‘weighted.mean’. In the
latter case, the weights are determined by the ‘vSize’
variable. Other arguments can be passed on. For
‘weighted.mean’, it is possible to assign a variable name for
its ‘w’ argument.
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 ordered treemap,
pivot-by-size, algorithm (Bederson et al., 2002) takes the
sorting order (‘sortID’) into account while aspect ratios are
still acceptable.
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 ‘vSize’ and
‘vColor’ respectively. To inverse the sorting order, use "-"
in the prefix. By default, large rectangles are placed top
left.
mirror.x: logical that determines whether the rectangles are mirrored
horizontally
mirror.y: logical that determines whether the rectangles are mirrored
vertically
palette: one of the following:
a color palette: i.e., a vector of hexadecimal colors
(#RRGGBB)
a name of a Brewer palette: See
‘RColorBrewer::display.brewer.all()’ for the options. The
palette can be reversed by prefixing with a "-". For
treemap types "value" and "comp", a diverging palette
should be chosen (default="RdYlGn"), for type "dens" a
sequential (default="OrRd"). The default value for
"depth" is "Set2".
"HCL": Tree Colors are color schemes derived from the
Hue-Chroma-Luminance color space model. This is only
applicable for qualitative palettes, which are applied to
the treemap types "index", "depth", and "categorical".
For "index" and "categorical" this is the default value.
palette.HCL.options: list of advanced options to obtain Tree Colors
from the HCL space (when ‘palette="HCL"’). This list
contains:
‘hue_start’: number between 0 and 360 that determines the
starting hue value (default: 30)
‘hue_end’: number between ‘hue_start’ and ‘hue_start + 360’
that determines the ending hue value (default: 390)
‘hue_perm’: boolean that determines whether the colors are
permuted such that adjacent levels get more
distinguishable colors. If ‘FALSE’, then the colors are
equally distributed from ‘hue_start’ to ‘hue_end’
(default: TRUE)
‘hue_rev’: boolean that determines whether the colors of
even-numbered branched are reversed (to increase
discrimination among branches)
‘hue_fraction’: number between 0 and 1 that determines the
fraction of the hue circle that is used for recursive
color picking: if 1 then the full hue circle is used,
which means that the hue of the colors of lower-level
nodes are spread maximally. If 0, then the hue of the
colors of lower-level nodes are identical of the hue of
their parents. (default: .5)
‘chroma’: chroma value of colors of the first-level nodes,
that are determined by the first index variable (default:
60)
‘luminance’: luminance value of colors of the first-level
nodes, i.e. determined by the first index variable
(default: 70)
‘chroma_slope’: slope value for chroma of the non-first-level
nodes. The chroma values for the second-level nodes are
‘chroma+chroma_slope’, for the third-level nodes
‘chroma+2*chroma_slope’, etc. (default: 5)
‘luminance_slope’: slope value for luminance of the
non-first-level nodes (default: -10)
For "depth" and "categorical" types, only the first two items
are used. Use ‘treecolors’ to experiment with these
parameters.
range: range of values (so vector of two) that correspond to the
color legend. By default, the range of actual values,
determined by ‘vColor’, is used. Only applicable for numeric
types, i.e. "value", "comp", "dens", and "manual". Note that
the range doesn't affect the colors in the treemap itself for
"value" and "manual" types; this is controlled by ‘mapping’.
mapping: vector of three values that specifies the mapping of the
actual values, determined by ‘vColor’, to ‘palette’. The
three values are respectively the minimum value, the mid
value, and the maximum value. The mid value is particularly
useful for diverging color palettes, where it defined the
middle, neutral, color which is typically white or yellow.
The ‘mapping’ should cover the ‘range’. By default, for
"value" treemaps, it is ‘c(-max(abs(values)), 0,
max(abs(values)))’, where values are the actual values
defined by ‘vColor’. For "manual" treemaps, the default
setting is ‘c(min(values), mean(range(values)),
max(values))’. A vector of two can also be specified. In that
case, the mid value will be the average of those. Only
applicable for "value" and "manual" type treemaps.
n: preferred number of categories by which numeric variables are
discretized.
fontsize.title: font size of the title
fontsize.labels: font size(s) of the data labels, which is either a
single number that specifies the font size for all
aggregation levels, or a vector that specifies the font size
for each aggregation level. Use value ‘0’ to omit the labels
for the corresponding aggregation level.
fontsize.legend: font size for the legend
fontcolor.labels: Specifies the label colors. Either a single color
value, or a vector of color values one for each aggregation
level. By default, white and black colors are used, depending
on the background (‘bg.labels’).
fontface.labels: either a single value, or a vector of values one for
each aggregation level. Values can be integers If an integer,
following the R base graphics standard: 1 = plain, 2 = bold,
3 = italic, 4 = bold italic, or characters: ‘"plain"’,
‘"bold"’, ‘"italic"’, ‘"oblique"’, and ‘"bold.italic"’.
fontfamily.title: font family of the title. Standard values are
"serif", "sans", "mono", "symbol". Mapping is device
dependent.
fontfamily.labels: font family of the labels in each rectangle.
Standard values are "serif", "sans", "mono", "symbol".
Mapping is device dependent.
fontfamily.legend: font family of the legend. Standard values are
"serif", "sans", "mono", "symbol". Mapping is device
dependent.
border.col: color of borders drawn around each rectangle. Either one
color for all rectangles or a vector of colors, or one for
each aggregation level
border.lwds: thicknesses of border lines. Either one number specifies
the line thicknesses (widths) for all rectangles or a vector
of line thicknesses for each aggregation level.
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’
does not determine the fontsize anymore, but it still
determines the minimum fontsize in combination with
‘lowerbound.cex.labels’.
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" and "categorical" treemaps, the
default is (slightly) transparent grey (‘"#CCCCCCDC"’), and
for the other types slightly transparent: ‘220’.
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 default value .5, means that lower level labels
are printed if other labels do not overlap with more than .5
times their area size.
align.labels: object that specifies the alignment of the labels. Either
a character vector of two values specifying the horizontal
alignment (‘"left"’, ‘"center"’, or ‘"right"’) and the
vertical alignment (‘"top"’, ‘"center"’, or ‘"bottom"’), or a
list of sush character vectors, one for each aggregation
level.
xmod.labels: the horizontal position modification of the labels in
inches. Either a single value, or a vector that specifies the
modification for each aggregation level.
ymod.labels: the vertical position modification of the labels in
inches. Either a single value, or a vector that specifies the
modification for each aggregation level.
eval.labels: should the text labels, i.e. the factor labels of the
‘index’ variables, be evaluated as expressions? Useful for
printing mathematical symbols or equations.
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, ‘"bottom"’.
format.legend: a list of additional arguments for the formatting of
numbers in the legend to pass to ‘format()’; only applies if
‘type’ is ‘"value"’, ‘"dens"’ or ‘"manual"’.
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.
draw: logical that determines whether to draw the treemap.
...: arguments to be passed to other functions. Currently, only
‘fun.aggregate’ takes optional arguments.
_V_a_l_u_e:
A list is silently returned:
tm: a ‘data.frame’ containing information about the rectangles:
indices, sizes, original color values, derived color values,
depth level, position (x0, y0, w, h), and color.
type: argument type
vSize: argument vSize
vColor: argument vColor
stdErr: standard errors
algorithm: argument algorithm
vpCoorX: x-coordinates of the treemap within the whole plot
vpCoorY: y-coordinates of the treemap within the whole plot
aspRatio: aspect ratio of the treemap
range: range of the color values scale
_R_e_f_e_r_e_n_c_e_s:
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.
_E_x_a_m_p_l_e_s:
#########################################
### quick example with Gross National Income data
#########################################
data(GNI2014)
treemap(GNI2014,
index=c("continent", "iso3"),
vSize="population",
vColor="GNI",
type="value",
format.legend = list(scientific = FALSE, big.mark = " "))
#########################################
### extended examples with fictive business statistics data
#########################################
data(business)
#########################################
### treemap types
#########################################
# index treemap: colors are determined by the index argument
## Not run:
# large example which takes some time...
treemap(business,
index=c("NACE1", "NACE2", "NACE3"),
vSize="turnover",
type="index")
## End(Not run)
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")
## Not run:
# depth treemap: show depth
treemap(business,
index=c("NACE1", "NACE2", "NACE3"),
vSize="turnover",
type="depth")
## End(Not run)
# 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")
## Not run:
# 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))
## End(Not run)
#########################################
### graphical options: control fontsizes
#########################################
## Not run:
# 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
## End(Not run)
treemap package:treemap R Documentation
_C_r_e_a_t_e _a _t_r_e_e_m_a_p
_D_e_s_c_r_i_p_t_i_o_n:
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’.
_U_s_a_g_e:
treemap(dtf, index, vSize, vColor = NULL, stdErr = NULL, type = "index",
fun.aggregate = "sum", title = NA, title.legend = NA,
algorithm = "pivotSize", sortID = "-size", mirror.x = FALSE,
mirror.y = FALSE, palette = NA, palette.HCL.options = NULL,
range = NA, mapping = NA, n = 7, fontsize.title = 14,
fontsize.labels = 11, fontsize.legend = 12, fontcolor.labels = NULL,
fontface.labels = c("bold", rep("plain", length(index) - 1)),
fontfamily.title = "sans", fontfamily.labels = "sans",
fontfamily.legend = "sans", border.col = "black",
border.lwds = c(length(index) + 1, (length(index) - 1):1),
lowerbound.cex.labels = 0.4, inflate.labels = FALSE, bg.labels = NULL,
force.print.labels = FALSE, overlap.labels = 0.5,
align.labels = c("center", "center"), xmod.labels = 0, ymod.labels = 0,
eval.labels = FALSE, position.legend = NULL, format.legend = NULL,
drop.unused.levels = TRUE, aspRatio = NA, vp = NULL, draw = TRUE, ...)
_A_r_g_u_m_e_n_t_s:
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 aggregation level,
the second name the second-highest aggregation level, and so
on. Required.
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 "*<scale factor>" or "/<scale
factor>". Note: when omitted for ‘"value"’ treemaps, a
contant value of 1 is taken.
stdErr: name of the column that contains standard errors. These are
not used for the treemaps, but only aggregated accordingly
and returned as item of the output list.
type: type of the treemap, which determines how the rectangles are
colored:
‘"index"’: colors are determined by the ‘index’ variables.
Different branches in the hierarchical tree get different
colors. For this type, ‘vColor’ is not needed.
‘"value"’: the numeric ‘vColor’-column is directly mapped to
a color palette. This palette is diverging, so that
values of 0 are assigned to the mid color (white or
yellow), and negative and positive values are assigned to
color based on two different hues colors (by default reds
for negative and greens for positive values). For more
freedom, see ‘"manual"’.
‘"comp"’: colors indicate change of the ‘vSize’-column with
respect to the numeric ‘vColor’-column in percentages.
Note: the negative scale may be different from the
positive scale in order to compensate for the ratio
distribution.
‘"dens"’: colors indicate density. This is analogous to a
population density map where ‘vSize’-values are area
sizes, ‘vColor’-values are populations per area, and
colors are computed as densities (i.e. population per
squared km).
‘"depth"’: each aggregation level (defined by ‘index’) has a
distinct color. For this type, ‘vColor’ is not needed.
‘"categorical"’: ‘vColor’ is a factor column that determines
the color.
‘"color"’: ‘vColor’ is a vector of colors in the hexadecimal
(#RRGGBB) format
‘"manual"’: The numeric ‘vColor’-column is directly mapped to
a color palette. Both palette and range should be
provided. The palette is mapped linearly to the range.
fun.aggregate: aggregation function, only used in ‘"value"’ treemaps.
This function determines how values of the lowest aggregation
level are aggregated. By default, it takes the ‘sum’. Other
sensible functions are ‘mean’ and ‘weighted.mean’. In the
latter case, the weights are determined by the ‘vSize’
variable. Other arguments can be passed on. For
‘weighted.mean’, it is possible to assign a variable name for
its ‘w’ argument.
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 ordered treemap,
pivot-by-size, algorithm (Bederson et al., 2002) takes the
sorting order (‘sortID’) into account while aspect ratios are
still acceptable.
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 ‘vSize’ and
‘vColor’ respectively. To inverse the sorting order, use "-"
in the prefix. By default, large rectangles are placed top
left.
mirror.x: logical that determines whether the rectangles are mirrored
horizontally
mirror.y: logical that determines whether the rectangles are mirrored
vertically
palette: one of the following:
a color palette: i.e., a vector of hexadecimal colors
(#RRGGBB)
a name of a Brewer palette: See
‘RColorBrewer::display.brewer.all()’ for the options. The
palette can be reversed by prefixing with a "-". For
treemap types "value" and "comp", a diverging palette
should be chosen (default="RdYlGn"), for type "dens" a
sequential (default="OrRd"). The default value for
"depth" is "Set2".
"HCL": Tree Colors are color schemes derived from the
Hue-Chroma-Luminance color space model. This is only
applicable for qualitative palettes, which are applied to
the treemap types "index", "depth", and "categorical".
For "index" and "categorical" this is the default value.
palette.HCL.options: list of advanced options to obtain Tree Colors
from the HCL space (when ‘palette="HCL"’). This list
contains:
‘hue_start’: number between 0 and 360 that determines the
starting hue value (default: 30)
‘hue_end’: number between ‘hue_start’ and ‘hue_start + 360’
that determines the ending hue value (default: 390)
‘hue_perm’: boolean that determines whether the colors are
permuted such that adjacent levels get more
distinguishable colors. If ‘FALSE’, then the colors are
equally distributed from ‘hue_start’ to ‘hue_end’
(default: TRUE)
‘hue_rev’: boolean that determines whether the colors of
even-numbered branched are reversed (to increase
discrimination among branches)
‘hue_fraction’: number between 0 and 1 that determines the
fraction of the hue circle that is used for recursive
color picking: if 1 then the full hue circle is used,
which means that the hue of the colors of lower-level
nodes are spread maximally. If 0, then the hue of the
colors of lower-level nodes are identical of the hue of
their parents. (default: .5)
‘chroma’: chroma value of colors of the first-level nodes,
that are determined by the first index variable (default:
60)
‘luminance’: luminance value of colors of the first-level
nodes, i.e. determined by the first index variable
(default: 70)
‘chroma_slope’: slope value for chroma of the non-first-level
nodes. The chroma values for the second-level nodes are
‘chroma+chroma_slope’, for the third-level nodes
‘chroma+2*chroma_slope’, etc. (default: 5)
‘luminance_slope’: slope value for luminance of the
non-first-level nodes (default: -10)
For "depth" and "categorical" types, only the first two items
are used. Use ‘treecolors’ to experiment with these
parameters.
range: range of values (so vector of two) that correspond to the
color legend. By default, the range of actual values,
determined by ‘vColor’, is used. Only applicable for numeric
types, i.e. "value", "comp", "dens", and "manual". Note that
the range doesn't affect the colors in the treemap itself for
"value" and "manual" types; this is controlled by ‘mapping’.
mapping: vector of three values that specifies the mapping of the
actual values, determined by ‘vColor’, to ‘palette’. The
three values are respectively the minimum value, the mid
value, and the maximum value. The mid value is particularly
useful for diverging color palettes, where it defined the
middle, neutral, color which is typically white or yellow.
The ‘mapping’ should cover the ‘range’. By default, for
"value" treemaps, it is ‘c(-max(abs(values)), 0,
max(abs(values)))’, where values are the actual values
defined by ‘vColor’. For "manual" treemaps, the default
setting is ‘c(min(values), mean(range(values)),
max(values))’. A vector of two can also be specified. In that
case, the mid value will be the average of those. Only
applicable for "value" and "manual" type treemaps.
n: preferred number of categories by which numeric variables are
discretized.
fontsize.title: font size of the title
fontsize.labels: font size(s) of the data labels, which is either a
single number that specifies the font size for all
aggregation levels, or a vector that specifies the font size
for each aggregation level. Use value ‘0’ to omit the labels
for the corresponding aggregation level.
fontsize.legend: font size for the legend
fontcolor.labels: Specifies the label colors. Either a single color
value, or a vector of color values one for each aggregation
level. By default, white and black colors are used, depending
on the background (‘bg.labels’).
fontface.labels: either a single value, or a vector of values one for
each aggregation level. Values can be integers If an integer,
following the R base graphics standard: 1 = plain, 2 = bold,
3 = italic, 4 = bold italic, or characters: ‘"plain"’,
‘"bold"’, ‘"italic"’, ‘"oblique"’, and ‘"bold.italic"’.
fontfamily.title: font family of the title. Standard values are
"serif", "sans", "mono", "symbol". Mapping is device
dependent.
fontfamily.labels: font family of the labels in each rectangle.
Standard values are "serif", "sans", "mono", "symbol".
Mapping is device dependent.
fontfamily.legend: font family of the legend. Standard values are
"serif", "sans", "mono", "symbol". Mapping is device
dependent.
border.col: color of borders drawn around each rectangle. Either one
color for all rectangles or a vector of colors, or one for
each aggregation level
border.lwds: thicknesses of border lines. Either one number specifies
the line thicknesses (widths) for all rectangles or a vector
of line thicknesses for each aggregation level.
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’
does not determine the fontsize anymore, but it still
determines the minimum fontsize in combination with
‘lowerbound.cex.labels’.
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" and "categorical" treemaps, the
default is (slightly) transparent grey (‘"#CCCCCCDC"’), and
for the other types slightly transparent: ‘220’.
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 default value .5, means that lower level labels
are printed if other labels do not overlap with more than .5
times their area size.
align.labels: object that specifies the alignment of the labels. Either
a character vector of two values specifying the horizontal
alignment (‘"left"’, ‘"center"’, or ‘"right"’) and the
vertical alignment (‘"top"’, ‘"center"’, or ‘"bottom"’), or a
list of sush character vectors, one for each aggregation
level.
xmod.labels: the horizontal position modification of the labels in
inches. Either a single value, or a vector that specifies the
modification for each aggregation level.
ymod.labels: the vertical position modification of the labels in
inches. Either a single value, or a vector that specifies the
modification for each aggregation level.
eval.labels: should the text labels, i.e. the factor labels of the
‘index’ variables, be evaluated as expressions? Useful for
printing mathematical symbols or equations.
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, ‘"bottom"’.
format.legend: a list of additional arguments for the formatting of
numbers in the legend to pass to ‘format()’; only applies if
‘type’ is ‘"value"’, ‘"dens"’ or ‘"manual"’.
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.
draw: logical that determines whether to draw the treemap.
...: arguments to be passed to other functions. Currently, only
‘fun.aggregate’ takes optional arguments.
_V_a_l_u_e:
A list is silently returned:
tm: a ‘data.frame’ containing information about the rectangles:
indices, sizes, original color values, derived color values,
depth level, position (x0, y0, w, h), and color.
type: argument type
vSize: argument vSize
vColor: argument vColor
stdErr: standard errors
algorithm: argument algorithm
vpCoorX: x-coordinates of the treemap within the whole plot
vpCoorY: y-coordinates of the treemap within the whole plot
aspRatio: aspect ratio of the treemap
range: range of the color values scale
_R_e_f_e_r_e_n_c_e_s:
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.
_E_x_a_m_p_l_e_s:
#########################################
### quick example with Gross National Income data
#########################################
data(GNI2014)
treemap(GNI2014,
index=c("continent", "iso3"),
vSize="population",
vColor="GNI",
type="value",
format.legend = list(scientific = FALSE, big.mark = " "))
#########################################
### extended examples with fictive business statistics data
#########################################
data(business)
#########################################
### treemap types
#########################################
# index treemap: colors are determined by the index argument
## Not run:
# large example which takes some time...
treemap(business,
index=c("NACE1", "NACE2", "NACE3"),
vSize="turnover",
type="index")
## End(Not run)
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)