2017-08-10 17 views
0
> 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() 

比方说,我有一个数据框与列x,y和z。我创建了一个g轴,x沿x轴,y轴沿y轴。我看过悬停代码的examples,它们显示悬停在某个点上时的x和y值,但是除了沿x和y绘制的图形之外,还可以访问数据框中的其他数据吗?例如,如果有人悬停在点(1,6)上,有没有办法显示'a'?任何方式显示数据框信息除了X和Y悬停闪亮/ ggplot?

+0

查找https://plot.ly和http://jkunst.com/highcharter/ –

+0

怎么样ggvis ?以及Shiny Gallery上的电影资源管理器示例,https://github.com/rstudio/shiny-examples/blob/master/051-movie-explorer/server.R – Sam

回答

2

您可以使用基于plotly包下面的代码:

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