簡體   English   中英

無法使用帶有注解_自定義的繪圖實現ggplotly

[英]Can't implement ggplotly on a plot with annotation_custom

我正在繪制溜冰場上射擊位置的數據集。 我想使用plotly使用戶將鼠標懸停在每個點上時可以看到一個描述框。 我認為可以使用自定義工具提示來完成

溜冰場存儲在rink

rink <- rasterGrob(readJPEG("full-rink.jpg"))

這是full-rink.jpg 在此處輸入圖片說明

這是我正在使用的數據集的前5行:

structure(list(game_date = structure(c(17674, 17674, 17674, 17674, 
17674), class = "Date"), event_team = c("WSH", "WSH", "T.B", 
"T.B", "T.B"), event_description = c("WSH #8 OVECHKIN(12), Slap, Off. Zone, 53 ft.Assists: #92 KUZNETSOV(13); #43 WILSON(8) Expected Goal Prob: 1.6%", 
"WSH ONGOAL - #92 KUZNETSOV, Wrist, Off. Zone, 13 ft. Expected Goal Prob: 50.4%", 
"T.B ONGOAL - #17 KILLORN, Backhand, Off. Zone, 18 ft. Expected Goal Prob: 4.5%", 
"T.B ONGOAL - #17 KILLORN, Wrist, Off. Zone, 23 ft. Expected Goal Prob: 4.6%", 
"T.B ONGOAL - #27 MCDONAGH, Slap, Off. Zone, 57 ft. Expected Goal Prob: 1.2%"
), event_type = c("GOAL", "SHOT", "SHOT", "SHOT", "SHOT"), home_team = c("T.B", 
"T.B", "T.B", "T.B", "T.B"), away_team = c("WSH", "WSH", "WSH", 
"WSH", "WSH"), coords_x = c(-42, -80.3, 71, 67, 34), coords_y = c(-21, 
12, -3, 9, 19)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-5L))

這是我的情節的代碼:

example_data %>%
ggplot(aes(coords_x, coords_y, text = event_description)) +
  annotation_custom(rink, -100, 100, -45, 45) +
  geom_point(aes(color = event_team), size = 3, show.legend = FALSE) +
  coord_fixed() +
  xlim(-100, 100) +
  ylim(-45, 45) +
  theme_nothing() +
  theme(text = element_text(size = 15),
        plot.title = element_text(hjust = 0.5)) +
  ggtitle(paste0(game_date, "\n", away_team, " vs ", home_team)) +
  scale_color_manual(values = c("#000000", "slategrey"))

不幸的是,一旦我跑步

ggplotly(pbp_plotly_processed) ,我收到一條錯誤消息:

Warning message:
In geom2trace.default(dots[[1L]][[1L]], dots[[2L]][[1L]], dots[[3L]][[1L]]) :
  geom_GeomCustomAnn() has yet to be implemented in plotly.`

不要認為這是可能的plotly呢。 有沒有人可以建議的解決方法?

謝謝!

一個好的解決方案是直接使用plotly構建您的繪圖:

library(plotly)

example_data %>%
plot_ly(x = ~coords_x, y=~coords_y, 
        text=~paste("Event team:", event_team, "<br>Event type:", event_type))  %>% 
add_markers(marker=list(size=20)) %>%
layout(
xaxis = list(range = c(-100,100)), 
yaxis = list(range = c(-45,45)),
images= list(
    list(
         source= "https://i.imgur.com/Y2kOUX5.png",
         xref= "paper",
         yref= "paper",
         x= 0,
         y= 1,
         sizex= 1,
         sizey= 1,
         opacity= 0.8,
         layer = "below")
       )
)

一個警告。 從鏈接https://i.imgur.com/Y2kOUX5.png下載的圖像是刪除白色邊框后的原始圖像。

在此處輸入圖片說明

暫無
暫無

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

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