R图形中具有悬停信息的线段或矩形 [英] Line segments or rectangles with hover information in R plotly figure
问题描述
我想创建一个线段或矩形的交互式图形,这样当用户将鼠标悬停在其上时,每个线段或矩形都会提供不同的信息.我看着 htmlwidgets展示,我认为 plotly 看起来很有希望. (我愿意接受其他与R相关的方法.)
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.)
下面是一个简单的示例.我可以创建提供悬停信息的端点(t1和t2)的图.但是我希望在用户将鼠标悬停在两个端点之间的空间时显示悬停信息.
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).
我可以使用add_trace()
添加线段,但是无法使悬停工作.而且,如果我添加第二个线段,则会收到错误消息:
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
我可以使用layout()
添加矩形,但是同样,我无法使悬停工作.
I can add rectangles using layout()
, but again, I can't get the hover to work.
如果有人提出了一种使悬停参数适用于任何一种方法的方法,我也欢迎提出建议,以针对大量的线段/矩形(在这个简单的示例中不仅仅是2个)进行编码. .
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).
有什么建议吗?
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])
))
推荐答案
您可以使用highcharter
,它包装了Highcharts.js库.
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图形中具有悬停信息的线段或矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!