Brydenr
Brydenr

Reputation: 798

R plotly show xcoordinate on xaxis

I'm trying to create a graph with a similar x-axis format to this (from https://plot.ly/r/line-charts/):

code given:

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

However, running the code on my machine produces this graph:

enter image description here

Notice that the x-coordinate hover is not there.

R version: 3.4.1 Plotly version: 4.7.1

Changing the hoverinfo and text tags in plot_ly just changes the hoverinfo over the graph. How do I show the same hovering x-coordinate in the first graph?

Update: setting hoverinfo = "text+x" and layout(hovermode = "x") shows the x-coordinate on the x-axis and the point tooltips.

Upvotes: 1

Views: 663

Answers (1)

Maximilian Peters
Maximilian Peters

Reputation: 31689

Try setting hovermode to 'x'

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

and it should work.

Upvotes: 1

Related Questions