使用riverplot画桑基图
#生成桑基图的边,N1存储源节点,N2存储目的节点,Value存储边的值
edges = data.frame(N1 = c("Direct","SEM","引荐","自然搜索","行业","提交量"),
N2 = c(rep("行业",each=4),"提交量","托管量"),Value = c(28,32,18,40,60,40),
stringsAsFactors = F)
head(edges)
library(riverplot)
#生成桑基图的点集(即edges出现过的所有点)
nodes = data.frame(ID = unique(c(edges$N1, edges$N2)), stringsAsFactors = FALSE)
#节点所在桑基图的层级
nodes$x = c(1,1,1,1,2,3,4)
#节点所在层的排列位置
nodes$y = c(0,1,2,3,2,2,2)
rownames(nodes) = nodes$ID
head(nodes)
library(RColorBrewer)
#添加颜色、文本格式等
palette = paste0(brewer.pal(6, "Greens"), "60")palette = brewer.pal(7, "Greens")
styles = lapply(nodes$y, function(n) {
list(col = palette[n+1], lty = 0, textcol = "black",srt=0,nodestyle=point)
})
names(styles) = nodes$ID
#生成riverplot对象
rp <- list(nodes = nodes, edges = edges, styles = styles)
class(rp) <- c(class(rp), "riverplot")
plot(rp)
结果如下:
想画桑基图,百度了很多帖子,自己找了一个代码跟着做和修改,终于成功了。。
新手小白弄了好久才成功,过程中出现蛮多错误。。并没有记录。