r语言 - 绘图内add_segment的条件颜色,具体取决于值



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

我得到的数据源:

数据帧被创建为反应式数据(我已经截断了很长的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)
)
}
)

任何想法真的很感激。我尝试了各种方法,但都失败了(如果颜色,等等((

如上所述 - 问题已解决。

pal <- c( "#b8e55a","#e59029")
mw_index_xu <- plot_ly (data_faktoren_index_xu,color = ~Color_xu, colors = pal, showlegend = FALSE) %>%

最新更新