change the value in tooltips in plotly

爷,独闯天下 提交于 2021-02-07 10:21:32

问题


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?


回答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.

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


来源:https://stackoverflow.com/questions/45698196/change-the-value-in-tooltips-in-plotly

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!