2014-10-02 53 views
2

我是plot.ly的新手,我刚刚从R中抽出了我的第一个散点图 - 非常棒!在R中自定义infoWindow/tooltip - > plotly

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坐标不感兴趣,但我希望显示国家名称和人口规模,我将其映射到无意义的大小。

理想情况下,我想知道我是否可以自定义infowindow,甚至可以在我的数据框中显示每个国家的变量,但我不会在aes()中显示该图。一个国家的GDP等等。

谢谢!

回答

0

问题和答案原本张贴在Using 2+ legends from R to plotly/plot.ly

修改的内容将出现在悬停框:

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