简体   繁体   中英

Embed logo on top of another embedded logo in plotly

I have this example dataset ( example_data ) of hockey shot locations I want to plot over an ice rink.

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(1, 0, 0, 0, 0), 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))

I successfully embedded the ice rink with the answer to my previous question . Now, I want to embed two logos (home/away team logos) and I've tried this code to no avail...

example_data %>% 
  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"),
      list(
        source = "http://www.capsinfo.com/images/NHL_Team_Logos/NHL_Ducks_Primary.png"

      )
    )
  )

What am I doing wrong? I think I need to specify xref , yref , etc inside the list, but I have no idea what those parameters mean

I think there might something wrong with your second image (too big, too small?). For example, using another picture from the web (taking here https://community.plot.ly/t/plotly-unable-to-embed-a-logo-or-background-image-in-plotly/2100/18 ) as your second logo, you will see the second logo shows up when you set the right parameters.

example_data %>% 
  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"),
      list(
        source = "https://images.contentful.com/w3cthbuv8rf8/1g2AFv1q32mcceWaSK6CCS/7e8ae1f1a9266a044aa1c7988612b985/2008-the-dark-knight-version-of-the-symbol-is-largely-unchanged-from-the-original.png",
        xref = "x",
        yref = "y",
        x = 50,
        y = -20,
        sizex = 20,
        sizey = 20,
        sizing = "stretch",
        opacity = 0.5

      )
    )
  )

Now in your question, you do not say where you want to put it. To place your logo you can use two different references (paper or x/y coordinates).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM