按照此question https://stackoverflow.com/questions/70576618/r-plotly-bar-chart-positive-negative-values-different-color-second-axis
我希望通过悬停模式“x”看到 x 值...所以我添加了一个额外的 add_trace ,它可以工作,但是它以绿色/红色描绘,并且希望将其显示为白色
t <- tibble("one" = c(1,2,3,4,5,6,7,8,9),
"two" = c(-5,6,9,-8,7,-1,5,2,1),
"three" = c(2,5,6,9,8,7,8,4,5))
# 'bar' objects don't have these attributes: 'mode', 'line'
Plot <- plot_ly(t, x = ~one, y = ~two, type = 'bar', mode = NULL, line = NULL, name = ~ifelse(two < 0, yes = "two < 0", no = "two > 0"),
color = ~two < 0, colors = c("#28a745", "#dc3545"),
yaxis = 'y2') %>%
add_trace(type = "bar",
x = ~one, y = 0,
hoverinfo='text',
text = ~paste("X:", one),
yaxis = "y", opacity = 0,
showlegend=F
) %>%
add_trace(x = ~one, y = ~three, type = 'scatter', mode = 'lines',
name = "three", line = list(color = "#ffc107"), color = NULL, yaxis = "y") %>%
layout(title = "test",
xaxis = list(titel = "one"),
yaxis = list(side = 'right',
title = 'three',
showgrid = F, zeroline = F,
showline = T),
yaxis2 = list(side = 'left', title = 'two',
showgrid = F, zeroline = F,
showline = T, overlaying = "y"),
hovermode = "x")