<Data Visualization>3 TreeMap

  • 使用包 TreeMap
  • 語法:

    • treemap(data, index=c("item","subitem"), vSize="time1", vColor="time2", type="comp", title='aaa',palette='RdBu')
    • index:表示一組表示層次結構的分類變量,而且必須按照由大到小的順序排列
    • vSize:代表樹狀圖最底層的單個矩形面積大小,也即index中最後一層次(最底層次)的指標數據大小。
    • vColor:控制最底層(也即單個矩形)的顏色範圍,是樹狀圖所表達的第二個維度數據。
    • type: 顏色映射方式,該參數控制顏色在樹狀圖中的映射方式(如單色漸變、雙色漸變或者在獨立的層級結構內使用各自的單色漸變等)。
    • title:圖表標題;
    • palette=顏色風格,也就是所用到的調色板。
  • 基礎版:
      -
# library
library(treemap)

# Build Dataset
group=c(rep("group-1",4),rep("group-2",2),rep("group-3",3))
subgroup=paste("subgroup" , c(1,2,3,4,1,2,1,2,3), sep="-")
value=c(13,5,22,12,11,7,3,1,23)
data=data.frame(group,subgroup,value)

# treemap
treemap(data,index=c("group","subgroup"), vSize="value", type="index")  

這裏寫圖片描述

  • 進階版:
    • Customize labels:
# Custom labels:
treemap(data, index=c("group","subgroup"),     vSize="value", type="index",
    fontsize.labels=c(15,12), # size of labels. Give the size per level of aggregation: size for group, size for subgroup, sub-subgroups...
    fontcolor.labels=c("white","orange"), # Color of labels
    fontface.labels=c(2,1),  # Font of labels: 1,2,3,4 for normal, bold, italic, bold-italic...
    bg.labels=c("transparent"), # Background color of labels
    align.labels=list(
        c("center", "center"), 
        c("right", "bottom")
        ),                # Where to place labels in the rectangle?
    overlap.labels=0.5,   # 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.
    inflate.labels=F,    # If true, labels are bigger when rectangle is bigger.
)
  • Custom the borders:
treemap(data, index=c("group","subgroup"), vSize="value", type="index",
    border.col=c("black","white"),    # Color of borders of groups, of subgroups, of subsubgroups ...
    border.lwds=c(7,2)    # Width of colors
    )
  • Interactive Treemap:
    • 使用包:d3treeR
# library
library(treemap)
library(d3treeR)

# basic treemap
p=treemap(data,
            index=c("group","subgroup"),
            vSize="value",
            type="index"
            )            

# make it interactive ("rootname" becomes the title of the plot):
inter=d3tree2( p ,  rootname = "General" )

Reference:
https://mp.weixin.qq.com/s?__biz=MzA3Njc0NzA0MA==&mid=2653189857&idx=1&sn=a039539c936393ead41b289759518715&scene=21#wechat_redirect
http://www.r-graph-gallery.com/portfolio/treemap/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章