4

首先......我可能会错误地处理这个任务。我是图表功能的新手,如果我对图表功能的理解是错误的,从编程的角度来看,我不会感到惊讶。

问题出现在代码的“节点 [fillcolor = COLOR]”语句中。
如果我只是写'fillcolor = Green,style =filled'而不是fillcolor = object1,那么它就完美了。同样,如果我用“深红色”或任何其他颜色替换绿色,也没有问题。

我的问题是我希望这种颜色发生变化,具体取决于由条件确定的对象的值。基本上,如果去看医生的病人太少,这应该会在我每天创建的报告中引发危险信号,并且每天手动编程会有点痛苦。

我尝试过
的:我没有指定颜色,而是尝试使用条件对象的输出作为填充颜色(例如我在下面尝试过的)

fillcolor = object1 - 用黑色填充最终框
fillcolor = ', object1 ' - 最终框再次被黑色填充
fillcolor = object1[1] - 输出结果错误
fillcolor = ', object1[1] ' - 最终框为黑色- 再次填充

# 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%,则为深红色(红色)。

感谢所有/任何帮助!

4

1 回答 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
                  ")

在此处输入图像描述

于 2019-05-24T18:43:38.063 回答