简体   繁体   English

无法使用带有注解_自定义的绘图实现ggplotly

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

I'm plotting a dataset of shot locations on an ice rink. 我正在绘制溜冰场上射击位置的数据集。 I want to use plotly to enable users to see a description box once they hover over each point. 我想使用plotly使用户将鼠标悬停在每个点上时可以看到一个描述框。 I thought this would be done using the custom tooltip 我认为可以使用自定义工具提示来完成

The ice rink is stored in a rink object. 溜冰场存储在rink

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

Here is full-rink.jpg 这是full-rink.jpg 在此处输入图片说明

This is the first 5 rows of the dataset I'm working with: 这是我正在使用的数据集的前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))

Here is the code for my plot: 这是我的情节的代码:

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"))

Unfortunately, once I run 不幸的是,一旦我跑步

ggplotly(pbp_plotly_processed) , I get an error message: 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.`

Don't think this is possible in plotly yet. 不要认为这是可能的plotly呢。 Is there a workaround that anyone can suggest? 有没有人可以建议的解决方法?

Thank you! 谢谢!

A good solution is to build your plot using directly 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")
       )
)

A warning. 一个警告。 The image downloaded from the link https://i.imgur.com/Y2kOUX5.png is the original image after deletion of white borders. 从链接https://i.imgur.com/Y2kOUX5.png下载的图像是删除白色边框后的原始图像。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您能否将表格绘制到类似于非笛卡尔坐标的 annotation_custom 方法的 ggmap 上 - Can you plot a table onto a ggmap similar to annotation_custom method for non- Cartesian coordinates 如何将带有批注(annotation_custom)()的grob放置在绘图区域的精确区域? - How to place grobs with annotation_custom() at precise areas of the plot region? 由注解_自定义用geom_bar图创建的移动表 - Moving table created by annotation_custom with geom_bar plot R ggplot2:栅格grob的annotation_custom没有绘制 - R ggplot2: annotation_custom of raster grob does not plot 尽管有输入数据集,有没有办法在图的同一点上绘制annotation_custom()? - Is there a way to plot an annotation_custom() at the same point of the plot despite the input dataset? 尝试将图像添加到极坐标图会给出“错误:annotation_custom 仅适用于笛卡尔坐标” - Trying to add an image to a polar plot gives "Error: annotation_custom only works with Cartesian coordinates" R-将图添加到多面ggplot2对象(使用注解_自定义) - R - add plot to facetted ggplot2 object (using annotation_custom) 在walk中走一个annotation_custom函数 - walk the annotation_custom function inside a walk annotation_custom 与 ggplot2 中的 npc 坐标 - annotation_custom with npc coordinates in ggplot2 将 annotation_custom 值与 ggplot 网格线对齐 - Align annotation_custom values with ggplot gridlines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM