r - 使用 R 和 ggplot2 语法将自定义工具提示添加到绘图中

标签 r ggplot2 plotly

我正在尝试使用具有自定义标签的 ggplot 对象从 R 创建一个绘图。

#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

我希望特定数据点的值将从标签中获取,并且仅在悬停该数据点时才显示。

最佳答案

我一直在研究同样的问题,我认为你需要做的是这样的事情(通过 https://stackoverflow.com/a/27007513/829256 和 Twitter 上的 @plotlygraphs)

# 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

因此,该图现在应该具有每个数据点的“标签”值,并在鼠标悬停时显示为工具提示。

您可能希望在如何将标签分配给点方面做一些更聪明的事情,但希望这可以帮助您开始。

谢谢,我认为解决这个问题也将帮助我解决我自己的任务:-)

关于r - 使用 R 和 ggplot2 语法将自定义工具提示添加到绘图中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24175766/

相关文章:

r - 使用 R 和 rvest 进行网络抓取

r - ggplot2:不同美学的传说

r - ggplot 中的配对段

javascript - 在不卡住 GUI 的情况下呈现 plotly.js 图形(Web Worker?)

在多个内核上运行不同的程序

r - 如何将 MVN 包的输出获取到 R 中的表中?

r - 将 mob() 树(partykit 包)与 nls() 模型一起使用

r - 将浮雕(GEOtiff,.tif)添加到瑞士国家边界的 ggplot((多边形)shapefile,.shp)

r - Plotly:如何自定义圆环图中的颜色?

python-3.x - 在我的应用程序中使用数据表