首先......我可能会错误地处理这个任务。我是diagrammeR 功能的新手,如果我对diagrammeR 的理解是错误的,从编程的角度来看,我不会感到惊讶。
问题出现在代码的“node [fillcolor = COLOR]”语句中。
如果我简单地写'fillcolor = Green, style =filled' 而不是fillcolor = object1,那么它就可以完美运行。同样,如果我用“深红色”或任何其他颜色替换绿色,也没有问题。
我的问题是我希望这种颜色改变,这取决于由条件决定的对象的值。基本上,如果很少有病人去看医生,这应该会在我每天创建的报告上发出危险信号,每天手动编程会有点痛苦。
我尝试过的:
我没有指定颜色,而是尝试使用条件对象的输出作为填充颜色(例如我在下面尝试过的)
# Just create some random data for a flow chart
a = 100 # Total people
b = 60 # Number of total that are sick
c = 19 # Number of sick that saw a doctor
d = round(c/b * 100) # percent of sick who saw a doctor
# create a flowchart-list-object
flow <- list(a=a, b=b, c=c, d=d)
# this could be where I am going wrong
# Condition that determines if the Percentage of sick people who saw a doctor
# is above 40%
if (d > 40) {
object1 <- 'Green'
} else
object1 <- 'Crimson'
# Output the flowchart using grViz
DiagrammeR::grViz("
digraph dot {
graph[layout = dot, fontsize = 15]
# Node numbers with labelled text
node [shape = box,
width = 3,
fontname = Helvetica]
a [label = '@@1']
b [label = '@@2']
c [label = '@@3']
# First set of node to edge connections
a -> b -> c
node [fillcolor = object1, style = filled]
d [label = '@@4']
c -> d
}
[1]: paste0('Total Sick \\n ', flow$a, '')
[2]: paste0('Number of total sick \\n ', flow$b, '')
[3]: paste0('Number of Sick who see a doctor \\n ', flow$c, '')
[4]: paste0('% of sick who see a doctor \\n ', flow$d, '')
")
如果患病人数的百分比高于 40%,我希望流程图中的最后一个框为绿色,如果低于 40%,则为深红色(红色)。
感谢所有/任何帮助!
最佳答案
您必须将颜色定义为脚注,就像节点的标签一样。这是因为 object1
是一个 R 变量,而不是实际值。我在最后将其定义为脚注 [5]:
。
DiagrammeR::grViz("
digraph dot {
graph[layout = dot, fontsize = 15]
# Node numbers with labelled text
node [shape = box,
width = 3,
fontname = Helvetica]
a [label = '@@1']
b [label = '@@2']
c [label = '@@3']
# First set of node to edge connections
a -> b -> c
d [style = filled, fillcolor = '@@5', label = '@@4']
c -> d
}
[1]: paste0('Total Sick \\n ', flow$a, '')
[2]: paste0('Number of total sick \\n ', flow$b, '')
[3]: paste0('Number of Sick who see a doctor \\n ', flow$c, '')
[4]: paste0('% of sick who see a doctor \\n ', flow$d, '')
[5]: object1
")
关于r - 使用diagrammeR,如何根据条件使节点填充颜色?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56297691/