r - 如何将 R 中的自定义悬停文本添加到两个相互引用的系列中

标签 r plotly

我目前有 plotly 代码(见下面的代码),它产生如下所示的输出:

Current

而我想要生产的将更像这样:

enter image description here

即我想要自定义文本 hoverinfo ,对于任一系列,同时显示其他系列的相应信息,并报告哪个更高或更低。

生成第一张图片的代码:

require(tidyverse)
require(plotly)

data("beavers")

beaver_tidy <- beaver1 %>% 
  mutate(label = "a") %>% 
  bind_rows(
    beaver2 %>% 
      mutate(label = "b")
  ) %>% 
  mutate(daytime  = day * max(time) + time) %>% 
  as_tibble()


beaver_tidy %>% 
  group_by(label) %>% 
  plot_ly(
    x = ~ time, y = ~temp, color = ~label
  ) %>% 
  add_lines(
  )

我希望能够在 plot_ly 中设置 hoverinfo 参数或 add_lines到“文本”,然后添加一个 text参数来调整上面的代码以生成上面显示的模型图像。

最佳答案

一个有点hacky的解决方案,但它适用于您的示例。可能值得分解管道以查看字符变量是如何形成的。

library(tidyverse)
library(plotly)
library(glue)

beaver_tidy %>%
  group_by(time) %>%
  #utilise glue to add temperature value to a string
  mutate(main_label = glue("{label} value is {temp}"),
  #now add another variable with the opposite value (with conditions)
  opp_label = case_when(
    #n() counts the number of rows in the time group
    label == "a" & n() == 2 ~ lead(main_label),
    label == "b" & n() == 2 ~ lag(main_label),
       n() == 1 ~ ""),
     #add a string with difference calculated (gives some NA values)
     diff = glue("difference is {round(temp - lead(temp),2)}"),
     #combine strings into one variable with conditions
     comb = case_when(
       diff == "difference is NA" & n() == 1 ~ str_c(main_label, 
                                                     "<br>",
                                                     "No corresponding value", 
                                                      sep = " "),
       diff == "difference is NA" & n() == 2 ~ str_c(main_label, 
                                                     "<br>",
                                                     opp_label, 
                                                     "<br>",
                                                     lag(diff),
                                                     sep = " "),
       TRUE ~ str_c(main_label, 
                    "<br>",
                    opp_label,
                    "<br>",
                     diff, 
                     sep = " ") )) %>%
#and pipe into the original plot
group_by(label) %>% 
plot_ly(x = ~ time, 
        y = ~temp, 
        color = ~label) %>% 
add_lines() %>% 
#add in the hover text
add_trace(x = ~ time, 
          y = ~temp, 
          text = ~comb, 
          hoverinfo = "text")

这给出了以下输出

关于r - 如何将 R 中的自定义悬停文本添加到两个相互引用的系列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56988823/

相关文章:

r - ARIMA、ARMA 和 AIC?

r - 在数据框中的多行上提取具有匹配模式的 id

python - 阴谋冲刺 : How to display three graphs next to each other inside a tab

python - 如何在 plotly python 中引用日期设置色标

python - 如何在plotly 3D中绘制不同高度的多个正方形

python - 如何从一个巨大的txt文件中获取分数列表的反向百分位?

r - 如何在R中加载地理空间pdf?

r - 使用 purrr/tidyverse 按名称字母顺序对列表进行排序

javascript - 用于 plotly.js 图表的 React 和包装器

javascript - 如何创建自定义 JS 函数以将 plotly 图像复制到 R shiny 中的剪贴板