Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

r - Any way to put a picture (.png) in a ggplotly tooltip?

Hi I've been adding onto an R Shiny project of mine and I've had a hard time finding information on working with ggplotly tooltips. I have a graph that plots NBA PPG vs shooting efficiency for the top scorers, and I have a basic tooltip set up displaying some information about the player when the user hovers over them.

The data comes with a variable called urlPlayerHeadshot, which gives links to a png picture of the player (for example, here is Anthony Davis' - https://ak-static.cms.nba.com/wp-content/uploads/headshots/nba/latest/260x190/203076.png). Every Player in the graph has their own link & image.

Below is the graph and the code I use to produce it. I've also included an example of what my data frame looks like.

text

text2

top20_plot <- function(df){
  p <- df %>%
    ggplot(aes(avg_PTS, season_ts_percent, fill = Top5)) +
    geom_point(size = 6, alpha = 0.7, pch = 21, color = 'black', aes(text = paste0(Player, '<br>',
                                                                                   Team, ' (', Wins, '-', Losses, ')', '<br>',
                                                                                   'PPG: ', round(avg_PTS, 1), '<br>',
                                                                                   'TS%: ', round(season_ts_percent * 100, 1), '%', '<br>',
                                                                                   'Games Played: ', GP))) +
    scale_y_continuous(labels = scales::percent, limits=c(.48, .72), breaks=seq(.48, .72, by = .08)) + 
    scale_fill_manual(values = c('light blue', 'orange')) +
    labs(color = 'Top 5 MVP Candidate', fill = 'Top 5 MVP Candidate',
         title = 'Player Efficiency Tracker 
 PPG vs TS% for all 20+ PPG Scorers',
         x = 'Average Points per Game',
         y = 'True Shooting Percentage') +
    theme_jacob() +
    theme(plot.title = element_text(hjust = 0.5), legend.position = 'top')
  
  ggplotly(p, tooltip = c('text')) %>%
    layout(legend = list(orientation = "h", x = 0.35))
  
}
top20_plot(top_20pt_scorers) 

I was wondering if there is a solution to somehow point to this variable and include that png in the tooltip of the graph, or if this is simply not possible using ggplotly. Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...