我根据data.tree
建议的几个功能实现了这个library(data.tree)
library(yaml)
library(DiagrammeR)
node=as.Node(yaml.load("
name: Plaintiff to Litigate?
type: decision
Litigate:
type: chance
Prove negligence, Win Case:
type: chance
p: 0.60
High damages:
type: terminal
p: 0.30
payoff: 2900000
Medium damages:
type: terminal
p: 0.40
payoff: 1900000
Low damages:
type: terminal
p: 0.30
payoff: 900000
Fail to prove negligence:
type: terminal
p: 0.40
payoff: -100000
Settlement Offer:
type: terminal
payoff: 1000000"))
GetPrelimNodeLabel = function(node) switch(node$type,
terminal = format(node$payoff, scientific = FALSE, big.mark = ","),'')
GetEdgeLabel = function(node) {
if (isNotRoot(node) && node$parent$type == 'chance') {
label = paste0(node$name, " (", node$p, ")")
} else {
label = node$name
}
return (label)
}
GetNodeShape = function(node) switch(node$type, decision = "box", chance = "circle", terminal = "none")
GetArrowHead = function(node) switch(node$type, terminal = "oinv", "none")
SetEdgeStyle(node, fontname = 'helvetica', label = GetEdgeLabel, arrowhead=GetArrowHead)
SetNodeStyle(node, fontname = 'helvetica', label = GetPrelimNodeLabel, shape = GetNodeShape)
ToDiagrammeRGraph(node,direction = "climb") %>%
set_global_graph_attrs("layout", "dot", "graph") %>%
add_global_graph_attrs("rankdir", "LR","graph") %>%
render_graph()
包,建议用于post的决策分析。
如何画出如上所示的直边?不过,我不介意这种颜色。
这是我的代码:
event_id