1
> df <- data.frame(x=c(1,2,3,4,5), y=c(6,7,8,9,10), z=c('a','b','c','d','e'))
> df
  x  y z
1 1  6 a
2 2  7 b
3 3  8 c
4 4  9 d
5 5 10 e

ggplot(df,
  aes(x=x, y=y)+
  geom_point()

Let's say I have a data frame with columns x, y, and z. I create a ggplot with x along the x axis and y along the y axis. I've seen examples of hover code that displays the x and y values when hovering over a point, but is it possible to access other data in the data frame besides what's graphed along x and y? For example if someone hovers over point (1,6), is there a way to display 'a'?

Takkun
  • 6,131
  • 16
  • 52
  • 69
  • lookup https://plot.ly and http://jkunst.com/highcharter/ – moodymudskipper Aug 10 '17 at 11:41
  • what about ggvis? and the movie Explorer example on the Shiny Gallery, https://github.com/rstudio/shiny-examples/blob/master/051-movie-explorer/server.R – Sam Aug 10 '17 at 13:07

1 Answers1

4

You can use the following code based on the plotly package:

df <- data.frame(x=c(1,2,3,4,5), y=c(6,7,8,9,10), z=c('a','b','c','d','e'))

library(ggplot2)
# Define the content and format of the tooltip in the "text" aesthetic
p <- ggplot(df, aes(x=x, y=y, 
           text=paste("X=",x,"<br>Y=",y,"<br>Z=",z))) + 
geom_point()

library(plotly)
p <- ggplotly(p, tooltip="text")
print(p)

enter image description here

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58