簡體   English   中英

使用 R 和 ggplot2 語法將自定義工具提示添加到繪圖中

[英]Adding custom tooltip to plotly using R and ggplot2 syntax

我正在嘗試使用具有自定義標簽的 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 to @plotlygraphs on Twitter)

# 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

因此,該圖現在應該具有每個數據點的“標簽”值,並通過鼠標懸停顯示為工具提示。

您可能想要在如何將標簽分配給點方面做一些更聰明的事情,但希望這能讓您開始。

謝謝,我認為解決這個問題也將幫助我解決我自己的任務:-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM