在绘图中更改工具提示中的值 [英] change the value in tooltips in plotly

查看:52
本文介绍了在绘图中更改工具提示中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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)

使用上面的代码,您可以获得带有工具提示的绘图图.x 和 y 轴显示原始数据.但是工具提示中的值显示为 (0,3)、(1,2)、(2,2)、(3,0).我想要的工具提示是 (1, 1000), (10, 100), (100, 10), (1000, 1).

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).

有什么办法吗?

推荐答案

Plotly 不会将输入值作为数字,而是覆盖轴标签.

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

更改 x 和 y 值会导致更多工作,因为工具提示显示的文本与这些值无关.本示例中最简单的解决方案是覆盖 text 属性.

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

这篇关于在绘图中更改工具提示中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆