Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly - code to color segments depending on value (positive/negative)

Hi,
I’m struggling to find a solution for the problem below - already browsed so many articles and other resources.

What I’ve got as data source:

Dataframe being created as reactive data (I’ve truncated the really long SQL query

 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)
 })

This gives me a fully populated dataframe.

*‘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 …*

Now I’d like to plot this, works without any issues (not 100% if this is the correct approach, but … it works). But I haven’t found any way to change the color of one element, (as you can see, the value Delta_GU is positive/negative)) I’d like to change the colour depending on this).

   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)
      )
    
    }
)

Any ideas really appreciated. I’ve tried various approaches, but in the end - all failed (ifelse for the color, which, etc.))