使用diagrammeR,如何根据条件使节点填充颜色发生变化?

4
首先,我可能错误地处理了这个任务。 我对diagrammeR功能还很陌生,如果我对diagrammeR的理解是错误的,从编程角度来看,我也不会感到惊讶。
问题出现在代码的“node [fillcolor = COLOR]”语句中。
如果我只写“fillcolor =绿色,style =填充”而不是fillcolor = object1,则完全正常。同样,如果我用'Crimson'或任何其他颜色替换Green,也没有问题。
我的问题是,我希望这个颜色根据由条件确定的对象的值而改变。 基本上,如果太少的病人去看医生,这应该在我每天创建的报告中引起警报,并且每天手动编程有点麻烦。
我尝试过:
不是指定颜色,我正在尝试将条件对象的输出用作fillcolor(例如下面所示的内容)。
# 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%则为深红色。感谢任何帮助!
1个回答

3

你必须像标签一样定义颜色的脚注,就像节点的标签一样。这是因为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
                  ")

enter image description here


这太完美了,谢谢!我是 Stack 的新手,因此目前无法为您的解决方案点赞,但它正是我正在寻找的——非常感谢您! - TVZ3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接