Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
578 views
in Technique[技术] by (71.8m points)

ggplot2 - Customise the infoWindow / tooltip in R --> plotly

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/

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.

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

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Question and answer were originally posted at Using 2+ legends from R to 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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...