R在xaxis上显示xcoordinate

时间:2017-07-31 21:48:53

标签: r plotly

我尝试使用与此类似的x轴格式创建图表(来自https://plot.ly/r/line-charts/):

给出的代码:

library(plotly)

x <- c(1:100)
random_y <- rnorm(100, mean = 0)
data <- data.frame(x, random_y)

p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines')

graph

但是,在我的机器上运行代码会产生以下图表:

enter image description here

请注意,x坐标悬停不存在。

R版本:3.4.1 Plotly版本:4.7.1

更改hoverinfo中的textplot_ly标记只会更改图表上的hoverinfo。如何在第一个图表中显示相同的悬停x坐标?

更新:设置hoverinfo = "text+x"layout(hovermode = "x")显示x轴上的x坐标和点工具提示。

1 个答案:

答案 0 :(得分:1)

尝试将hovermode设为'x'

p <- plot_ly(data, x = ~x, y = ~random_y, type = 'scatter', mode = 'lines') %>% layout(hovermode = 'x')

它应该有用。