使用R和ggplot2语法添加自定义工具提示 [英] Adding custom tooltip to plotly using R and ggplot2 syntax

查看:128
本文介绍了使用R和ggplot2语法添加自定义工具提示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从R使用ggplot对象创建一个剧情阴谋,该对象具有自定义标签。

'devtools')
#install_github(ropensci / plotly)
library('plotly')
set_credentials_file(username =your_name,api_key =your_key)

py < - plotly()
labels = LETTERS [sample(x = 26,size = nrow(iris),replace = T)]
ggiris < - ggplot(iris,aes( Petal.Width,Sepal.Length,color = Species))+ geom_point()

r < - py $ ggplotly(ggiris)
r $ response

我希望特定数据点的值将来自 labels 和只有在悬停数据点时才会显示。

解决方案

我一直在寻找同样的问题,我想你需要做的就是这样(通过 https://stackoverflow.com/a/27007513/829256 和h / t to @plotlygraphs on Twitte r)

 #首先使用Plotly连接并检索您上传的ggiris图的数据
irisplot< -py $ get_figure('username',n)#其中n =你的账户上这个图的数量

#检查irisplot对象
str(irisplot)#一个2 $ b $的列表b
#inspect irisplot $ data $ b $ str(irisplot $ data)#列表3,每个列表一个物种

#覆盖每个物种列表的文本
irisplot $ data [[1]] $ text< - labels [1:50]
irisplot $ data [[2]] $ text< - labels [51:100]
irisplot $ data [[3]] $ text< - labels [101:150]

#重新上传至Plotly
resp< -py $ plotly(irisplot $ data,kwargs = list (layout = irisplot $ layout))

#查看你的新情节
resp $ url

因此,对于每个数据点,绘图应该有一个来自'labels'的值,显示为带有mouseover的工具提示。 想要在分配方式上做更聪明的事情点击标签,但希望这会让你开始。



谢谢,我认为通过这个问题的解决将帮助我解决我自己的任务: - ) / p>

I am trying to create a plotly plot from R using a ggplot object, which has custom labels.

#library('devtools')
#install_github("ropensci/plotly")
library('plotly')
set_credentials_file(username="your_name", api_key="your_key")

py <- plotly()
labels = LETTERS[sample(x=26, size=nrow(iris), replace=T)]
ggiris <- ggplot(iris, aes(Petal.Width, Sepal.Length, color = Species)) + geom_point()

r <- py$ggplotly(ggiris)
r$response

I would like that the value for a particular data-point would be taken from labels and would be displayed only when hovering the data-point.

解决方案

I've been looking at the same problem and I think what you need to do is something like this (via https://stackoverflow.com/a/27007513/829256 and h/t to @plotlygraphs on Twitter)

# first use your Plotly connection and retrieve data for the ggiris plot you uploaded
irisplot <- py$get_figure('username', n)  # where n = the number of this plot on your account

# inspect the irisplot object
str(irisplot)  # a list of 2

# inspect irisplot$data
str(irisplot$data)  # a list of 3, one list for each Species

# overwrite 'text' for each Species list
irisplot$data[[1]]$text <- labels[1:50]
irisplot$data[[2]]$text <- labels[51:100]
irisplot$data[[3]]$text <- labels[101:150]

# re-upload to Plotly
resp <- py$plotly(irisplot$data, kwargs = list(layout = irisplot$layout))

# check out your new plot
resp$url

So the plot should now have a value from 'labels' for each data point, displayed as a tooltip with mouseover.

You will presumably want to do something smarter in how you assign the labels to the points, but hopefully this gets you started.

And thanks, I think working through this question will help me solve my own task too :-)

这篇关于使用R和ggplot2语法添加自定义工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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