當我將滑鼠懸停在使用 ggplot 創建的徽標圖上時,我希望顯示資訊,類似于:
影像本地存盤在我的計算機上。問題是還沒有為 Plotly 實作 annotations_custom、annotation_raster 和 ggdraw 等。
我嘗試使用 Plotly 直接創建圖形,但似乎無法在不通過 Shiny 應用程式的情況下直接讀取存盤的影像(有沒有辦法將本地存盤的影像讀取到 Plotly 中?)。
唯一的解決方案似乎是使用 JavaScript,所以我正在嘗試調整此處找到的以下代碼:https ://anchormen.nl/blog/data-science-ai/images-in-plotly-with-r/
library(tidyverse)
library(plotly)
g <- ggplot(iris, aes(x = Sepal.Length,
y = Petal.Length,
color = Species,
text = Species)) geom_point()
p <- ggplotly(g, tooltip = "text")
p %>% htmlwidgets::onRender("
function(el, x) {
// when hovering over an element, do something
el.on('plotly_hover', function(d) {
// extract tooltip text
txt = d.points[0].data.text;
// image is stored locally
image_location = '1.png';
// define image to be shown
var img = {
// location of image
source: image_location,
// top-left corner
x: 0,
y: 1,
sizex: 0.2,
sizey: 0.2,
xref: 'paper',
yref: 'paper'
};
// show image and annotation
Plotly.relayout(el.id, {
images: [img]
});
})
}
")
通過按原樣重用代碼,我設法顯示在線影像,而不是本地存盤的影像,無論是使用相對路徑(作業目錄設定為源檔案位置)還是絕對路徑,從 file:///C 開始: /用戶/。顯示圖表,我可以將滑鼠懸停在點上,但懸停時不顯示影像。
如果可以顯示本地存盤的影像,我將非常感謝在調整代碼方面提供的幫助,以便影像不會顯示在左上角而是顯示在點上以重新創建徽標圖。
非常感謝!
uj5u.com熱心網友回復:
解決了這個問題:https ://plotly-r.com/embedding-images.html 。最終使用本地存盤的影像似乎比預期的要容易。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493129.html
