Search code examples
rggplot2r-plotlyggplotly

ggplotly tooltip reacts erratically


enter image description here

I have this ggplotly object of NBA court showing the field goal attempts of each player. As you can see, when I hover over each region of the court, I see the tooltip behave very unpredictably. I feel like its because the court is produced with both geom_polygon and geom_path. See below for code:

Data (gg_lineup_fga): http://www.filedropper.com/gglineupfga

rank_colors <- RColorBrewer::brewer.pal(5, "RdYlBu")

ggplot2::ggplot(data = gg_lineup_fga, ggplot2::aes(x=long, 
                                                                y=lat, 
                                                                group = group,
                                                                fill = rank,
                                                                text = paste0("FGA : ", round(fga, 2)))) +
  ggplot2::facet_grid(. ~ player) +
  ggplot2::geom_polygon()  +
  ggplot2::geom_path(color = "black")  +
  ggplot2::coord_equal() +
  ggplot2::scale_fill_manual(values = rank_colors,
                             limits = 1:5,
                             name = NULL) +
  ggplot2::theme(axis.line=ggplot2::element_blank(),
                 axis.text.x=ggplot2::element_blank(),
                 axis.text.y=ggplot2::element_blank(),
                 axis.ticks=ggplot2::element_blank(),
                 axis.title.x=ggplot2::element_blank(),
                 axis.title.y=ggplot2::element_blank(),
                 panel.background=ggplot2::element_blank(),
                 panel.border=ggplot2::element_blank(),
                 panel.grid.major=ggplot2::element_blank(),
                 panel.grid.minor=ggplot2::element_blank(),
                 strip.text = ggplot2::element_text(size = 16), 
                 strip.background = ggplot2::element_blank(),
                 legend.position = "none",
                 # legend.margin = ggplot2::margin(0.1, 0.1, 0.1, 0, "npc"),
                 plot.margin = ggplot2::unit(c(0, 0, 0, 0), "cm"),
                 panel.spacing.y = ggplot2::unit(0, "cm"),
                 text=element_text(size=16, family="Avenir"))

To solve this problem, I can take out geom_path, but that'd erase the black line boundaries of the court regions. I was wondering if there is a parameter in plotly that prevents tooltip from interacting with geom_path (only interacts with geom_polygon)


Solution

  • Remove geom_path and add color to geom_polygon to color the boundries i.e., geom_polygon(color ='black')