Home > Software design >  ggplotly is showing the same variable twice in r
ggplotly is showing the same variable twice in r

Time:03-29

enter image description here

I grouped by player_name and used colour to differntiate between players. By using ggplotly player_name is shown twice. How can I avoid this?

ggplotly(ggplot(NBA_top, aes(season, y=player_height, colour=player_name, group=player_name))   geom_point()  geom_line())

CodePudding user response:

The issue is that by default ggplotly will show the values of all aesthetics in the tooltip. As you mapped player_name on both the color and the group aes the player name will show up twice.

To prevent that you could set the aesthetics to be displayed in the tooltip via the tooltip argument. Note that for the color aes you have to use colour.

Using a simple example based on mtcars:

library(plotly)

ggplot(mtcars, aes(hp, mpg, color = factor(cyl), group = factor(cyl)))  
  geom_point()

Calling ggplotly without setting the tooltip argument gives a duplicated entry in the tooltip:

ggploty()

enter image description here

To prevent that you could set the tooltip argument:

ggplotly(tooltip = c("x", "y", "colour"))

enter image description here

  • Related