我正在尝试使用具有自定义标签的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
我希望将特定数据点的值取自labels
并且仅在将数据点悬停时才显示。
我一直在寻找相同的问题,我认为您需要做的是这样的(通过https://stackoverflow.com/a/27007513/829256和h / t到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
因此,对于每个数据点,该图现在应具有来自“标签”的值,并通过鼠标悬停显示为工具提示。
您可能希望在将标签分配给点时做一些更聪明的事情,但是希望这可以帮助您入门。
谢谢,我认为解决这个问题也将帮助我解决自己的任务:-)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句