Search code examples
rggplot2plotlyggplotly

How to adapt plotly hovermode 'x' option to work with no additional aesthetics and at overlapping co-ordinates?


I'm trying to replicate the hovermode seen in this plotly example at 33.1:

Where it gives you only the information of the specific x co-ordinate in the hover, but with multiple entries.

In my real dataset, I do not set any additional aesthetics, other than text as I want some additional information in the hover. My dataset has multiple entries for some x co-ordinates like in the example. However, when I try and adapt the code for my real dataset:

p <- ggplot(df, mapping = aes(x, Points, text = paste("Person: ", Person,
                                                      "\nEvent: ", Event))) +
  # geom_jitter(shape = 4, width = 0, height = 50) +
  geom_point(shape = 4) +

ggplotly(p) %>%
  layout(hovermode = "x")

enter image description here

The hover for a specific x co-ordinate now only shows one individual. I've tried jittering the points vertically to see if it would make a difference, as I read that overlapping points often have issues, but that doesn't solve my issue.

I tried setting a colour aesthetic, just to test what it would look like:

p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
                                                      "\nEvent: ", Event))) +
  geom_point(shape = 4)
ggplotly(p) %>%
  layout(hovermode = "x")

enter image description here

I can easily get rid of the legend and make it so all the colours are set to a single colour, but here in the hover it shows values from multiple x co-ordinates, which introduces another issue.

Desired output at x co-ordinate 1090:

enter image description here

My df:

df <- structure(list(Event = c("2012", "2008", "2005", "2012", "2018", "2005", "2009", "2018", "2013", "2016", "2007", "2000", "2017", "2000", "2000", "2015", "2004", "2012", "2002", "2015", "2017", "2016", "2018", "2013", "2005", "2015", "2001", "2000", "2008", "2004", "2012", "2003", "2016", "2015", "2007", "2009", "2006", "2011", "2016", "2001", "2018", "2010", "2011", "2016", "2005", "2017", "2004", "2004", "2009", "2004", "2015", "2002", "2012", "2001", "2006", "2004", "2014", "2012", "2003", "2008", "2007", "2015", "2009", "2011"), x = c(1106, 1124, 1090, 1110, 1090, 1107, 1066, 1112, 1098, 1096, 1121, 1121, 1094, 1121, 1131, 1105, 1106, 1109, 1083, 1097, 1093, 1062, 1082, 1105, 1067, 1122, 1099, 1075, 1113, 1108, 1090, 1072, 1130, 1127, 1086, 1089, 1114, 1111, 1117, 1072, 1129, 1135, 1128, 1086, 1081, 1126, 1086, 1050, 1093, 1055, 1126, 1089, 1035, 1074, 1083, 1066, 1050, 1078, 1075, 1115, 1104, 1093, 1088, 1125), Points = c(847, 808, 883, 838, 883, 845, 938, 834, 865, 870, 814, 814, 874, 814, 793, 850, 847, 841, 899, 867, 876, 947, 901, 850, 935, 812, 863, 917, 832, 843, 883, 924, 795, 801, 892, 885, 830, 836, 823, 924, 797, 784, 799, 892, 903, 804, 892, 975, 876, 963, 804, 885, 1011, 919, 899, 938, 975, 910, 917, 827, 852, 876, 888, 806), Person = c("Pascal", "Hans", "Attila", "Brent", "Fredrik", "Óscar", "Yunior", "Pieter", "Sergey", "Adam", "Nicklas", "Fedor", "Marek", "Michael", "Indrek", "Simone", "Qi", "Willem", "Eugenio", "Pieter", "Devon", "Kai", "Martin", "Adam", "Hamdi", "Bastien", "Eduard", "Lev", "Aleksey", "Indrek", "Norman", "Dmitriy", "Keisuke", "Keisuke", "Andrei", "Eelco", "Andrei", "Kim", "Jonas", "Dean", "Niklas", "Petter", "Mikk", "Cedric", "Christopher", "Gael", "Markus", "Dmitriy", "Norman", "Nikolay", "Pau", "Thomas", "Ashton", "Lev", "Maurice", "Chiel", "Trey", "Sergey", "Markus", "Mikk", "Roman", "Ingmar", "Pascal", "Roman")), row.names = c(NA, -64L), class = "data.frame")

Solution

  • Try fixing the hover distance

    p <- ggplot(df, mapping = aes(x, Points, colour = Event, text = paste("Person: ", Person,
                                                                          "\nEvent: ", Event))) +
      theme(legend.position='none')+
      geom_point(shape = 4)
    ggplotly(p) %>%
      layout(hovermode = "x", hoverdistance = 1)
    

    enter image description here