1
library(ggplot2)
library(plotly)
dataset = data.frame(x = c(1,10, 100, 1000),
                 y = c(1000, 100, 10, 1))
p = ggplot(data = dataset, aes(x = x, y = y)) + geom_point() + geom_line() + 
scale_x_log10() + scale_y_log10()
ggplotly(p)

With above codes, you can have a plotly graph with tooltip. The x and y axis show the original data. But the values in tooltip is shown as (0,3), (1,2), (2,2), (3,0). What I want in the tooltip is (1, 1000), (10, 100), (100, 10), (1000, 1).

Is there any way to do it?

Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99
Vera0816
  • 23
  • 3

1 Answers1

1

Plotly does not take the input values as numbers but instead overwrites the axis labels.

> gp$x$data[[1]]$x
[1] 0 1 2 3
> gp$x$data[[1]]$y
[1] 3 2 1 0

Changing the x and y-values would cause even more work because the tooltip shows text which is independent of those values. The easiest solution in this example would be to overwrite the text attribute.

enter image description here

library(ggplot2)
library(plotly)
x <- c(1,10, 100, 1000)
y <- c(1000, 100, 10, 1)
dataset = data.frame(x = x,
                     y = y)
p = ggplot(data = dataset, aes(x = x, y = y)) + geom_point() + geom_line() + 
  scale_x_log10() + scale_y_log10()

gp <- ggplotly(p)
gp$x$data[[1]]$text = paste(x, y, sep='<br />')
gp
Maximilian Peters
  • 30,348
  • 12
  • 86
  • 99