R plotly图中带有悬停信息的线段或矩形 [英] Line segments or rectangles with hover information in R plotly figure

查看:12
本文介绍了R plotly图中带有悬停信息的线段或矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个线段或矩形的交互式图形,这样当用户将鼠标悬停在其上时,每个线段或矩形都会提供不同的信息.我查看了

无需额外代码即可处理大量柱状图:

set.seed(123)n <- 20largedat <- data.frame(t1 = runif(n, 1, 10),x = 1:n,任务=粘贴('系列',1:n))largedat$t2 <- largedat$t1 + runif(n, 2, 5)高图(hc_opts = 列表(图表 = 列表(倒置 = 'true'))) %>%hc_add_series_df(数据 = largedat,类型 = 'columnrange',组=任务,x = x,低 = t1,高 = t2) %>%hc_legend(enabled = FALSE) # 在这个上禁用图例

I want to create an interactive figure of line segments or rectangles, such that each segment or rectangle gives different information when the user hovers his/her mouse over it. I looked in the htmlwidgets showcase, and I thought plotly looked promising. (I am open to other R-related methods.)

Below is a simple example. I can create a plot of the end points (t1 and t2) that provide hover information. But I would like the hover information to appear any time the user hovers over the space between the two end points).

I can add a line segment using add_trace(), but I can't get the hover to work. And if I add the second line segment, I get an error message:

Error in plot_ly(data = mydat, x = t2, y = y, mode = "markers", hoverinfo = "text",  : 
  requires numeric/complex matrix/vector arguments

I can add rectangles using layout() , but again, I can't get the hover to work.

In the event that someone suggests a way to get the hover arguments to work for either approach, I would also welcome suggestions for how to code this for a large number of segments/rectangles (not just 2 as in this simple example).

Any suggestions?

mydat <- data.frame(t1=c(1, 3), t2=c(4, 5), y=c(1, 2), task=c("this", "that"))
library(plotly)

# attempt with one line segment - hover doesn't work
plot_ly(data=mydat, x=t2, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
add_trace(data=mydat, x=t1, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
add_trace(
  x=c(mydat$t1[1], mydat$t2[1]), y=c(mydat$y[1], mydat$y[1]), 
  mode="lines", hoverinfo="text", text=mydat$task[1])

# attempt with both line segments - 
# Error in plot_ly, requires numeric/complex matrix/vector arguments
plot_ly(data=mydat, x=t2, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
add_trace(data=mydat, x=t1, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
add_trace(
  x=c(mydat$t1[1], mydat$t2[1]), y=c(mydat$y[1], mydat$y[1]), 
  mode="lines", hoverinfo="text", text=mydat$task[1]) %*%
add_trace(
  x=c(mydat$t1[2], mydat$t2[2]), y=c(mydat$y[2], mydat$y[2]), 
  mode="lines", hoverinfo="text", text=mydat$task[2])

# attempt with rectangles - hover doesn't work
plot_ly(data=mydat, x=t2, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
add_trace(data=mydat, x=t1, y=y, mode="markers",
  hoverinfo="text", text=task) %>%
layout(shapes=list(
  list(type="rect", x0=mydat$t1[1], x1=mydat$t2[1], xref="x",
    y0=mydat$y[1], y1=mydat$y[1]+0.1, yref="y",
    hoverinfo="text", text=mydat$task[1]),
  list(type="rect", x0=mydat$t1[2], x1=mydat$t2[2], xref="x",
    y0=mydat$y[2], y1=mydat$y[2]+0.1, yref="y",
    hoverinfo="text", text=mydat$task[2])
  ))

解决方案

You could use highcharter, which wraps the Highcharts.js library.

# note i'm renaming y to x, since that's how highcharts will treat this dataset
mydat <- data.frame(t1=c(1, 3), t2=c(4, 5), x=c(1, 2), task=c("this", "that"))

library(highcharter)

highchart(hc_opts = list(
  chart = list(inverted = 'true')
  )) %>% 
  hc_add_series_df(data = mydat, type = 'columnrange',
                   group = task,
                   x = x, low = t1, high = t2)

Works just fine with no additional code required for a larger number of bars:

set.seed(123)
n <- 20
largedat <- data.frame(t1 = runif(n, 1, 10), 
                       x = 1:n,
                       task = paste('series', 1:n))
largedat$t2 <- largedat$t1 + runif(n, 2, 5)

highchart(hc_opts = list(
  chart = list(inverted = 'true')
  )) %>% 
  hc_add_series_df(data = largedat, type = 'columnrange',
                   group = task,
                   x = x, low = t1, high = t2) %>%
  hc_legend(enabled = FALSE) # disable legend on this one

这篇关于R plotly图中带有悬停信息的线段或矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆