小编Mat*_*ens的帖子

使用rpart生成sankey图的决策树

我可以使用Kyphosis数据集创建一个Rpart树,它是基础R的一部分:

fit <- rpart(Kyphosis ~ Age + Number + Start,
         method="class", data=kyphosis)
printcp(fit)
plot(fit, uniform=TRUE,main="Classification Tree for Kyphosis")
text(fit, use.n=TRUE, all=TRUE, cex=.8)
Run Code Online (Sandbox Code Playgroud)

这就是树的样子: 在此输入图像描述

现在为了更好地可视化树,我想使用plotly来使用sankey图.要在plotly中创建一个sankey图,必须执行以下操作:

library(plotly)
nodes=c("Start>=8.5","Start>-14.5","absent",
                   "Age<55","absent","Age>=111","absent","present","present")
p <- plot_ly(
  type = "sankey",
  orientation = "h",      
  node = list(
    label = nodes,
    pad = 10,
    thickness = 20,
    line = list(
      color = "black",
      width = 0.5
    )
  ),

  link = list(
    source = c(0,1,1,3,3,5,5,0),
    target = c(1,2,3,4,5,6,7,8),
    value =  c(1,1,1,1,1,1,1,1)
  )
) %>% 
  layout(
    title = "Desicion Tree",
    font …
Run Code Online (Sandbox Code Playgroud)

r decision-tree rpart plotly sankey-diagram

8
推荐指数
1
解决办法
614
查看次数

标签 统计

decision-tree ×1

plotly ×1

r ×1

rpart ×1

sankey-diagram ×1