自定义 R 中的 infoWindow/tooltip -->情节地 [英] Customise the infoWindow / tooltip in R --> plotly

查看:15
本文介绍了自定义 R 中的 infoWindow/tooltip -->情节地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 plot.ly 的新手,前几天我刚刚从 R 中绘制了我的第一个散点图 - 太棒了!

I am new to plot.ly and I just drew my first scatter plot out of R the other day - its great!

testplot <- ggplot(a, aes(OR_Edu, OR_Illn, color=Country, size=total)) + geom_point()
py$ggplotly(testplot)

https://plot.ly/~SyTpp/14/or-illn-vs-or-edu/

现在,我想更改工具提示或将鼠标悬停在数据点上时弹出的小信息窗口.在这种情况下,我对 y 坐标不感兴趣,但我想显示国家名称和人口规模,我将其映射到 aestectic 规模.

Now, I would like to change the tooltip or the little info window that pops up on hover over a datapoint. In this case I am not interested in the y-coordinate but instead I would like to display the country name and the population size, which I mapped to the aestectic size.

理想情况下,我想知道我是否/如何可以自定义信息窗口,甚至可能在我的数据框中显示我没有在 aes() 中给出图的每个国家/地区的变量,例如一个国家的GDP等等等等.

Ideally I would like to know if/how I can customize the infowindow in general, maybe even display variables in my dataframe for each country which I don't give the plot in aes(), e.g. the GDP of a country etc etc..

谢谢!

推荐答案

问答原贴于 使用 R 中的 2+ 个图例来 plotly/plot.ly

编辑悬停框中显示的内容:

To edit what appears in the hover box:

# Load "plotly"
library(plotly)
# Open a Plotly connection
py <- plotly()

# Retrieve a Plotly graph in R
hover_text <- py$get_figure("PlotBot", 81)

str(hover_text$data)
# This list has 4 elements, which all have dimension (attribute) 'text'

# You can overwrite them one by one, say
hover_text$data[[1]]$text[1]
hover_text$data[[1]]$text[1] <- "US"

# If you need something functional, I would recommend defining the necessary
# functions and using sapply()

# Plotly graph with hover text we just edited
py$plotly(hover_text$data, kwargs=list(layout=hover_text$layout))

这篇关于自定义 R 中的 infoWindow/tooltip -->情节地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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