%是一种表示树形数据的树形关系及各个分类的占比的图形,适合展现具有层级关系的数据,能够直观体现同级之间的比较。
安装、加载所需R包
#安装包
install.packages("treemap")
#加载包
library(treemap)
数据
#数据——随机生成绘图数据
set.seed(12)#种子
df <- data.frame(
samples=LETTERS[1:10],
group=rep(c('x','y'), 5),
value=sample(1:100, 10, replace = FALSE))
绘图
1、查看绘图参数:
#查看参数
??treemap
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,na.rm = TRUE,na.color = "#DDDDDD",na.text = "Missing",
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,
reverse.legend = FALSE,format.legend = NULL,drop.unused.levels = TRUE,
aspRatio = NA,vp = NULL,draw = TRUE,...)
下面简单介绍常见的几种图形绘制,如果大家感兴趣可以根据treemap()函数中的参数绘制自己喜欢的Treemap!
2、单一分组变量——只通过“samples”单一分类变量进行绘图:
1)根据值大小填色
treemap(df, #数据
index = "samples",#分类变量
vSize = "value",#分类变量对应数据值
vColor="value",#颜色深浅的对应列
type = "value",#"颜色映射方式,"index"、"value"、"comp"、"dens"、"depth"、"categorical"、"color"、"manual"
title = 'Treemap',#标题
border.col = "grey",#边框颜色
border.lwds = 4,#边框线宽度
fontsize.labels = 12,#标签大小
fontcolor.labels = 'red',#标签颜色
align.labels = list(c("center", "center")),#标签位置
fontface.labels = 2)#标签字体:1,2,3,4 表示正常、粗体、斜体、粗斜体
2)根据分类填色
treemap(df, #数据
index = "samples",#分类变量
vSize = "value",#分类变量对应数据值
vColor="index",#颜色深浅的对应列
type = "index",#"颜色映射方式,"index"、"value"、"comp"、"dens"、"depth"、"categorical"、"color"、"manual"
title = 'Treemap',#标题
border.col = "grey",#边框颜色
border.lwds = 4,#边框线宽度
fontsize.labels = 12,#标签大小
fontcolor.labels = 'white',#标签颜色
align.labels = list(c("center", "center")),#标签位置
fontface.labels = 2)#标签字体:1,2,3,4 表示正常、粗体、斜体、粗斜体
3、多个分类变量——基于“group”、“samples”两个分类变量进行绘图:
1)根据值大小填色
treemap(df, #数据
index = c("group","samples"),#分类变量
vSize = "value",#分类变量对应数据值
vColor="value",#颜色深浅的对应列
type = "value",#"颜色映射方式,"index"、"value"、"comp"、"dens"、"depth"、"categorical"、"color"、"manual"
title = 'Treemap',#标题
border.col = c("black","white"),#边框颜色
border.lwds = c(4,1),#边框线宽度
fontsize.labels = c(18,10),#标签大小
bg.labels=c("transparent"),#标题背景色
fontcolor.labels = c('white',"orange"),#标签颜色
align.labels = list(c("left", "top"),
c("center", "center")),#标签位置
fontface.labels = c(2,3))#标签字体:1,2,3,4 表示正常、粗体、斜体、粗斜体
2)根据分类填色
treemap(df, #数据
index = c("group","samples"),#分类变量
vSize = "value",#分类变量对应数据值
palette = "Set1",#自定义调色
vColor="index",#颜色深浅的对应列
type = "index",#"颜色映射方式,"index"、"value"、"comp"、"dens"、"depth"、"categorical"、"color"、"manual"
title = 'Treemap',#标题
border.col = c("black","white"),#边框颜色
border.lwds = c(4,1),#边框线宽度
fontsize.labels = c(18,10),#标签大小
bg.labels=c("transparent"),#标题背景色
fontcolor.labels = c('white',"orange"),#标签颜色
align.labels = list(c("left", "top"),
c("center", "center")),#标签位置
fontface.labels = c(2,3))#标签字体:1,2,3,4 表示正常、粗体、斜体、粗斜体