简体   繁体   English

在 r-plotly 中显示值之间的差异

[英]Show difference between values in r-plotly

I would like to add to the hover information about the difference between the values in the chart.我想在 hover 中添加有关图表中值之间差异的信息。 It would be best if it looks like the graphics below.如果它看起来像下面的图形,那将是最好的。

在此处输入图像描述

My example:我的例子:

library(plotly)
trace_1 <- sample(1:20)
main <- sample(1:20)
diff <-main - trace_1
x <- c(1:20)

data <- data.frame(x, trace_1, diff)

fig <- plot_ly(data, x = ~x, y = ~trace_1, name = 'trace 1', type = 'scatter', mode = 'lines') 
fig <- fig %>% add_trace(y = ~main, name = 'main', type = 'scatter', mode = 'lines') 
fig <- fig %>%
  layout(hovermode = "x unified")
fig

You could add another line that is calculated as the difference andd set the line color to transparent.您可以添加另一条计算为差异的线,并将线颜色设置为透明。

library(plotly)

df = mtcars %>%
  tibble::rownames_to_column("cars") %>%
  mutate(diff = mpg - wt)

plot_ly(df) %>% 
  add_lines(x = ~cars,y = ~mpg,name="mpg") %>% 
  add_lines(x = ~cars,y = ~wt,name="weight") %>% 
  add_lines(x = ~cars,y = ~diff,name="diff",line = list(color = "rgba(0,0,0,0)")) 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM