在add_segment内的条件颜色取决于值

时间:2018-10-07 17:16:58

标签: r shiny r-plotly

我正在努力寻找以下问题的解决方案-已经浏览了许多文章和其他资源。

我所拥有的数据源:

将数据帧创建为响应数据(我已经截断了很长的SQL查询)

 get_MW_index_vergleich <- reactive({ serverdb = dbConnect(MySQL(),
 user='xx',password='xxx',dbname='xxx',host='localhost')
 on.exit(dbDisconnect(serverdb))
 rs <- dbSendQuery(serverdb, 'set character set "utf8"')
 werte_0022 <- sprintf("select some data from tables",input$var_fak_select)
 dataframe_0022 <- fetch(dbSendQuery(serverdb, werte_0022))
 return(dataframe_0022)
 })

这为我提供了一个正确填充的数据框。

*'data.frame':  
13 obs. of  13 variables:  
$ Dimension: chr  "Anfrage" "Noten" "Dauer" "Interesse" ...  
$ MW_xU    : num  3.47 3.38 3.4 3.76 3.15 ...  
$ MW_gu    : num  3.32 3.56 3.19 3.52 3.4 ...  
$ MW_EU    : num  3.41 3.74 3.19 3.59 3.41 ...  
$ MW_nEU   : num  3.21 3.35 3.2 3.45 3.42 ...  
$ Index_GU : num  100 100 100 100 100 100 100 100 100 100 ...  
$ Index_xU : num  112.4 75 98.9 113.9 101.6 ...  
$ Index_nEU: num  91 71 99.9 96 99.9 ...  
$ Index_EU : num  107 124 100 104 100 ... 
$ Delta_GU : num  12.39 -24.98 -1.08 13.87 1.57 ...**  
$ Delta_GUP: chr  "12.39 %" "-24.98 %" "-1.08 %" "13.87 %" ...  
$ Delta_eU : num  4.97 -49.38 -1.09 9.83 1.61 ...  
$ Delta_neU: num  21.44 4.05 -1.02 17.84 1.69 ...

现在,我想将其绘制出来,没有任何问题(如果这是正确的方法,则不是100%,但这是可行的)。但是我还没有找到任何方法来更改一个元素的颜色(如您所见,值Delta_GU为正/负)。我想根据此更改颜色。)

   output$plot_faktoren_index_xu = renderPlotly({
    data_faktoren_index_xu <- get_MW_index_vergleich()
    str(data_faktoren_index_xu)
    mw_index_xu <- plot_ly (data_faktoren_index_xu,color = "#ffffff") %>%
      add_segments(x = 0, xend = ~Index_GU, y = ~Dimension, yend = ~Dimension, line = list(color = "#eeeeee", width = 15), showlegend = FALSE, textfont = text_schwarz) %>%
      add_segments(x = 0, xend = ~Index_xU, y = ~Dimension, yend = ~Dimension, line = list(color = "#000000", width = 5), showlegend = FALSE, textfont = text_schwarz) %>%
      add_segments(x = 100, xend = as.numeric(data_faktoren_index_xu$Index_xU), y = ~Dimension, yend = ~Dimension, line = list(color = "red", width = 3), showlegend = FALSE, textfont = text_schwarz) %>%
#      add_segments(x = 100, xend = which(as.numeric(data_faktoren_index_xu$Index_xU) < 0) , y = ~Dimension, yend = ~Dimension, line = list(color = "red", width = 3), showlegend = FALSE, textfont = text_schwarz) %>%
      # ^^^This segment - depending on value of Delta_GU should get color red or green
      add_markers(x = ~Index_GU, y = ~Dimension, name = "abc Vergleich <br>Alle Kandidaten", marker = list(color = "orange"), textfont = text_schwarz) %>%
      add_text(x = ~Index_xU, y = ~Dimension, textfont = text_schwarz , text = ~Delta_GUP, textposition = "bottom right", showlegend = FALSE ) %>%      
      layout(
        title = paste("Dimensionen          abc-Vergleich <b>", input$var_kand,"</b> mit Alle Kandidaten"),
        xaxis = list(title = "abc "),
        yaxis = list(title = " "),
        margin = list(l = 165)
      )

    }
)

任何想法都非常感谢。我尝试了各种方法,但是都失败了(颜色,种类等都不多)

1 个答案:

答案 0 :(得分:1)

如上所述-问题已解决。

    pal <- c( "#b8e55a","#e59029")

    mw_index_xu <- plot_ly (data_faktoren_index_xu,color = ~Color_xu, colors = pal, showlegend = FALSE) %>%