<Data Visualization>3 TreeMap

本文介绍如何使用R语言中的treemap包绘制基础和进阶的树状图,并通过定制标签、边框及利用d3treeR包实现交互式树状图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 使用包 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值