Drawing shot location points on top of ice rink in plotly

I have this example dataset, df

structure(list(game_date = structure(c(17689, 17689, 17689, 17689, 
17689), class = "Date"), event_team = c("WSH", "WSH", "WSH", 
"VGK", "VGK"), event_description = c("WSH ONGOAL - #8 OVECHKIN, Deflected, Off. Zone, 10 ft. Expected Goal Prob: 25.7%", 
"WSH ONGOAL - #8 OVECHKIN, Wrist, Off. Zone, 38 ft. Expected Goal Prob: 4.9%", 
"WSH ONGOAL - #29 DJOOS, Wrist, Off. Zone, 65 ft. Expected Goal Prob: 1%", 
"VGK ONGOAL - #5 ENGELLAND, Wrist, Off. Zone, 38 ft. Expected Goal Prob: 1.5%", 
"VGK ONGOAL - #88 SCHMIDT, Wrist, Off. Zone, 62 ft. Expected Goal Prob: 1.3%"
), event_type = c(0, 0, 0, 0, 0), home_team = c("VGK", "VGK", 
"VGK", "VGK", "VGK"), away_team = c("WSH", "WSH", "WSH", "WSH", 
"WSH"), coords_x = c(-80, -53, -31, 56, 34), coords_y = c(1, 
-14, 30, -17, -26)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-5L))

I try to plot shot location points on top of an ice rink using the below code

df %>% 
  plot_ly(x = ~coords_x, y=~coords_y, 
          text= ~event_description)  %>% 
  add_markers(size = ~event_type,
              sizes = c(150, 700),
              
              alpha = 0.75,
              color = ~factor(event_team),
              colors = c("black", "slategrey")
  ) %>%
  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")
      
    )
  )

Unfortunately, the ice rink is misplaced (the coordinates of x,y axis do not align with the ice rink):

I suspect that the my parameters inside list are wrong...Can anyone lend me a hand?

I would use the data (instead of paper) coordinates to size the rink image, so something similar to this:

plotly_empty(df, x = ~coords_x, y=~coords_y, colors = c("black", "slategrey"))  %>% 
  add_markers(size = ~event_type, alpha = 0.75, color = ~factor(event_team)) %>%
    layout(
      xaxis = list(range = c(-100, 100), title = ""), 
      yaxis = list(range = c(-45, 45), title = ""),
      images= list(
        list(
          source= "https://i.imgur.com/Y2kOUX5.png",
          xref= "x",
          yref= "y",
          x = 0,
          y = 0,
          sizex = 200,
          sizey = 90,
          opacity = 0.8,
          layer = "below",
          xanchor = "center",
          yanchor = "middle"
        )
      )
    )

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.