使用 diagrammeR,如何使节点填充颜色取决于条件?
using diagrammeR, how can I make a node fillcolor dependent on a condition?
首先...我可能错误地处理了这个任务。我是 diagrammeR 功能的新手,如果我对 diagrammeR 的理解是错误的,从编程的角度来看,我不会感到惊讶。
问题出现在代码的 'node [fillcolor = COLOR]' 语句中。
如果我简单地写 'fillcolor = Green, style = filled' 而不是 fillcolor = object1,那么它就完美了。同样,如果我用 'Crimson' 或任何其他颜色替换绿色,没问题。
我的问题是我希望此颜色根据由条件确定的对象的值而改变。基本上,如果去看医生的病人太少,这应该会在我每天创建的报告上发出危险信号,每天手动编程会有点痛苦。
我尝试过的:
我没有指定颜色,而是尝试使用条件对象的输出作为填充颜色(例如我在下面尝试过的)
fillcolor = object1 - fills the final box with black
fillcolor =
', object1 ' - final box is black-filled again
fillcolor =
object1[1] - outputs results in an error
fillcolor = ',
object1[1] ' - final box is black-filled again
# 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%,则为深红色(红色)。
感谢all/any帮助!
您必须将颜色定义为脚注,就像节点的标签一样。这是因为 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
")
首先...我可能错误地处理了这个任务。我是 diagrammeR 功能的新手,如果我对 diagrammeR 的理解是错误的,从编程的角度来看,我不会感到惊讶。
问题出现在代码的 'node [fillcolor = COLOR]' 语句中。
如果我简单地写 'fillcolor = Green, style = filled' 而不是 fillcolor = object1,那么它就完美了。同样,如果我用 'Crimson' 或任何其他颜色替换绿色,没问题。
我的问题是我希望此颜色根据由条件确定的对象的值而改变。基本上,如果去看医生的病人太少,这应该会在我每天创建的报告上发出危险信号,每天手动编程会有点痛苦。
我尝试过的:
我没有指定颜色,而是尝试使用条件对象的输出作为填充颜色(例如我在下面尝试过的)
fillcolor = object1 - fills the final box with black
fillcolor = ', object1 ' - final box is black-filled again
fillcolor = object1[1] - outputs results in an error
fillcolor = ', object1[1] ' - final box is black-filled again
# 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%,则为深红色(红色)。
感谢all/any帮助!
您必须将颜色定义为脚注,就像节点的标签一样。这是因为 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
")