-
更多详细的数据可视化教程,可订阅我们的店铺课程:
在上次推出树形图绘制教程之后,有小伙伴私信说有没有啥方法绘制如下所示的图表:
网友提供样例
(说实话,我第一眼看到之后就感觉R或者Python绘制此图会比较难,这种图应该事交互式图表,感觉像D3绘制的哈),果然,在查阅资料之后知道这种图叫做Voronoi treemap,也查到了给的样图来自D3官网~我当时就在想“干嘛不直接学D3绘制呢?”,后来转念一想,我的读者要么是学R的,要么是学Python的,再学习一种新语言去绘制图表,好多人是不太愿意的,所以,我们今天的推文还是尽量使用R或者Python绘制这种图·····,扯得有点多了,我们直接进入正文。在R绘图体系中,有SysbioTreemaps和voronoiTreemap包可以绘制类似图形,接下来,小编一一介绍。
SysbioTreemaps包绘制
-
官网
https://github.com/m-jahn/SysbioTreemaps
-
官网样例
-
导入数据及简单处理
library(SysbioTreemaps)
#导入样例数据
data(mtcars)
#构造新列(划分等级依据)
mtcars$car_name = gsub(" ", "\n", row.names(mtcars))
-
使用voronoiTreemap函数构造绘图数据
tm01 <- SysbioTreemaps::voronoiTreemap(
data = mtcars,
levels = c("gear", "car_name"),
cell_size = "wt",
shape = "rounded_rect",
seed = 123
)
-
使用drawTreemap将结果展示
drawTreemap(tm01, label_size = 2.5, label_color = "white")
drawTreemap example
-
定制化绘制 drawTreemap()函数提供以下多种参数用于更改绘制结果:
drawTreemap(
treemap,
levels = 1:length(treemap@call$levels),
color_type = "categorical",
color_level = NULL,
color_palette = NULL,
border_level = levels,
border_size = 6,
border_color = grey(0.9),
label_level = max(levels),
label_size = 1,
label_color = grey(0.9),
title = NULL,
title_size = 1,
title_color = grey(0.5),
legend = FALSE,
custom_range = NULL,
width = 0.9,
height = 0.9,
layout = c(1, 1),
position = c(1, 1),
add = FALSE
)
接下来我们更改部分参数绘制结果:
# 设置形状(shape)为圆形
tm2 <- SysbioTreemaps::voronoiTreemap(
data = mtcars,
levels = c("gear", "car_name"),
cell_size = "wt",
shape = "circle",
seed = 123
)
#绘制
drawTreemap(tm2, color_palette = c("#30A9DE","#EFDC05","#E53A40"),
label_size = 4, label_color = "white",border_color="black",border_size = 6)
drawTreemap custom set
-
一次绘制多个图形 小伙伴们可能也看到layout、position和add等参数了,这些参数可以让你一次绘制多个图形,具体例子图下:
drawTreemap(tm2, title = "treemap 1", label_size = 3,color_palette = c("#30A9DE","#EFDC05","#E53A40"),
color_type = "categorical", color_level = 1,border_color="black",border_size = 5,add=TRUE,
layout = c(1, 3), position = c(1, 1),title_color = "black",title_size=2)
drawTreemap(tm2, title = "treemap 2", label_size = 3,color_palette = c("#30A9DE","#EFDC05","#E53A40"),
color_type = "categorical", color_level = 2, border_color="black",border_size = 4,
add = TRUE, layout = c(1, 3), position = c(1, 2),title_color = "black",title_size=2)
drawTreemap(tm2, title = "treemap 3", label_size = 3,color_palette = c("#30A9DE","#EFDC05","#E53A40"),
color_type = "both", color_level = 1,border_color="black",border_size = 4,
add = TRUE, layout = c(1, 3), position = c(1, 3),title_color = "black",title_size=2)
mult charts
-
其他函数及熟悉设置 SysbioTreemaps包提供了多种绘图函数和对应的图层熟悉设置,这里就不一一介绍了,感兴趣的小伙伴可自行阅读官网进行探索哈~~
voronoiTreemap包绘制
介绍完静态绘制方法,小编接下里给大家推荐一个基于D3的绘制包(哈哈~还是得靠D3),详细介绍如下:
-
官网
https://github.com/uRosConf/voronoiTreemap
-
基本用法
library(voronoiTreemap)
data(ExampleGDP)
gdp_json <- vt_export_json(vt_input_from_df(ExampleGDP))
vt_d3(gdp_json)
base Example
-
定制化操作 vt_d3()函数提供的属性参数就那么几个,如下:
vt_d3(data, elementId = NULL, width = NULL, height = NULL,
seed = NULL, title = NULL, legend = FALSE, legend_title = NULL,
footer = NULL, label = TRUE, color_circle = "#aaaaaa",
color_border = "#ffffff", color_label = "#000000",
size_border = "1px", size_border_hover = "3px",
size_circle = "2px")
这里进行简单的修改展示:
vt_d3(gdp_json,label = FALSE, color_border = "#000000", size_border = "2px")
custom set example
由于是基于D3进行绘制,所以在定制化方面略显不足,看样子还得抽时间学习下D3~,好了,感兴趣的小伙伴可自行去官网探索哈~
总结
今天这篇推文,我们分别使用R-SysbioTreemaps和voronoiTreemap包绘制了Voronoi treemap图,希望可以给大家一些绘图参靠,至于D3的内容就交给小编来学习探索吧~~