如何在悬停在ggplot2中时在工具提示上显示y值 [英] How do I show the y value on tooltip while hover in ggplot2

查看:237
本文介绍了如何在悬停在ggplot2中时在工具提示上显示y值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将鼠标放在图上的一个点上时,我想要显示y值。我的情节的代码如下所示:

  output $ graph<  -  renderPlot((
p1< ggplot(data,aes(x = date))+
geom_line(aes(y = Height,color =Height),size = 1)+
geom_point Height,text = paste(Weight / Height:,Height)))
plot(p1)
})

我做了一些研究,并认为 text = paste(Weight / Height:,Height) code> aes 会确保文本出现。不幸的是没有出现有谁知道我做错了什么?

解决方案

ggplot a href =https://plot.ly/r/ =noreferrer> plotly 包。您只需将 plotOutput 替换为 plotlyOutput ,然后使用 renderPlotly < code $。

例1:plotly

  library(shiny)
library(ggplot2)
library(plotly)

ui< - fluidPage(
plotlyOutput(distPlot))


服务器< - 函数(输入,输出){
输出$ distPlot< - renderPlotly({
ggplot(iris,aes(Sepal.Width ,Petal.Width))+
geom_line()+
geom_point()
})
}

shinyApp(ui = ui,server = server )






您也可以玩<$ c $例如点击悬停 dblclick 使绘图交互。 (在闪亮画廊中查找更多示例)



在下面的例子中,我们通过 hover =plot_hover添加 hoverID 和然后指定默认为300ms的延迟。

  plotOutput(distPlot,hover =plot_hover,hoverDelay = 0)

然后我们可以通过输入$ plot_hover 并使用函数 nearPoints 显示接近点的值。




示例2:plotOutput(...,hover =plot_hover)

  ui<  -  fluidPage(
selectInput(var_y,Y-Axis,choices = names(iris)),
plotOutput(distPlot,hover =plot_hover,hoverDelay = 0 ),
uiOutput(dynamic)



服务器< - 函数(输入,输出){

输出$ distPlot< - renderPlot({
req(input $ var_y)
ggplot(iris,aes_string(Sepal.Width,输入$ var_y))+
geom_point()
})

输出$ dynamic< - renderUI({
req(input $ plot_hover)
verbatimTextOutput(vals)
})

输出$ vals< - renderPrint({
悬停< - input $ plot_hover
#print(str(悬停))#列表
y< - nearPoints(iris,input $ plot_hover)[input $ var_y]
req(nrow y)!= 0)
y
})

}
shinyApp(ui = ui,server = server)






示例3:自定义ggplot2工具提示:



然而,第二种解决方案可能并不令人满意,所以我决定创建一个自定义的工具提示,在指针附近显示。它需要一点javaScript / JQuery。

 库(闪亮)
库(ggplot2)

ui< - fluidPage(

tags $ head(tags $ style('
#my_tooltip {
position:absolute;
width:300px;
z-index:100;
padding:0;
}
')),

tags $ script('
$(document ).ready(function(){
//绘图的ID
$(#distPlot)。mousemove(function(e){

// uiOutput的ID
$(#my_tooltip)。show();
$(#my_tooltip)。css({
top:(e.pageY + 5)+px,$ (e.pageX + 5)+px
});
});
});
'),

selectInput(var_y,Y-Axis,choices = names(iris)),
plotOutput(distPlot,hover =plot_hover,hoverDelay = 0),
uiOutput( my_tooltip)




服务器< - 函数ion(input,output){


output $ distPlot < - renderPlot({
req(input $ var_y)
ggplot(iris,aes_string(Sepal .Width,输入$ var_y))+
geom_point()
})

输出$ my_tooltip< - renderUI({
hover< - input $ plot_hover
y< - nearPoints(iris,input $ plot_hover)[input $ var_y]
req(nrow(y)!= 0)
verbatimTextOutput(vals)
} )

output $ vals< - renderPrint({
hover< - input $ plot_hover
y< - nearPoints(iris,input $ plot_hover)[input $ var_y]
req(nrow(y)!= 0)
y
})
}
shinyApp(ui = ui,server = server)

$ hr

您也可以使用 ggvis 包。这个包很棒,但是还不够成熟

$ b

  library(ggvis)

ui< - fluidPage(
ggvisOutput(plot)


服务器< - 函数(输入,输出){

虹膜%>%
ggvis(〜Sepal.Width,〜Petal.Width)%>%
layer_points()%>%
layer_lines()%>%
add_tooltip(function(df){paste0(Petal.Width:,df $ Petal.Width)})% >%
bind_shiny(plot)
}

shinyApp(ui = ui,server = server)






EDITED






示例5:

在这篇文章之后,我搜索了互联网,看它是否可以做得更好比示例3 。我找到了这个精彩的ggplot自定义工具提示,我相信这样做不可能比这做得更好。


I want the show the y-value when I hold my mouse on a point in the graph. The code for my plot looks like this:

output$graph <- renderPlot({
  p1 <- ggplot(data, aes(x= date)) + 
  geom_line(aes(y=Height, colour = "Height"), size=1) + 
  geom_point(aes(y=Height, colour = "Height", text = paste("Weight/Height:", Height)))
  plot(p1)
})

I did some research and I thought that the text = paste("Weight/Height:", Height) part in aes would make sure that the text would appear. Unfortunately nothing appears. Does anyone know what I did wrong?

解决方案

ggplot is not interactive but it can be easily done with plotly package. You only need to replace plotOutput with plotlyOutput and then render a plot on with renderPlotly.

Example 1: plotly

library(shiny)
library(ggplot2)
library(plotly)

ui <- fluidPage(
    plotlyOutput("distPlot")
)

server <- function(input, output) {
   output$distPlot <- renderPlotly({
      ggplot(iris, aes(Sepal.Width, Petal.Width)) + 
       geom_line() + 
       geom_point()
   })
}

shinyApp(ui = ui, server = server)


You can also play with plotOutput options as for instance click, hover, dblclick to make the plot interactive. (Look for more examples in shiny gallery)

In the example below we add an hoverID by hover = "plot_hover" and then specify delay which is by default 300ms.

plotOutput("distPlot", hover = "plot_hover", hoverDelay = 0)

We then can access the value via input$plot_hover and use a function nearPoints to show values that are near the points.


Example 2: plotOutput(..., hover = "plot_hover"):

ui <- fluidPage(
  selectInput("var_y", "Y-Axis", choices = names(iris)),
  plotOutput("distPlot", hover = "plot_hover", hoverDelay = 0),
  uiOutput("dynamic")

)

server <- function(input, output) {

  output$distPlot <- renderPlot({
    req(input$var_y)
    ggplot(iris, aes_string("Sepal.Width", input$var_y)) + 
      geom_point()
  })

  output$dynamic <- renderUI({
    req(input$plot_hover) 
    verbatimTextOutput("vals")
  })

  output$vals <- renderPrint({
    hover <- input$plot_hover 
    # print(str(hover)) # list
    y <- nearPoints(iris, input$plot_hover)[input$var_y]
    req(nrow(y) != 0)
    y
  })

}
shinyApp(ui = ui, server = server)


Example 3: Custom ggplot2 tooltip:

However, the second solution may not be satisfactory so I've decided to create a custom tooltip which shows near the pointer. It requires a little bit javaScript/JQuery.

library(shiny)
library(ggplot2)

ui <- fluidPage(

  tags$head(tags$style('
     #my_tooltip {
      position: absolute;
      width: 300px;
      z-index: 100;
      padding: 0;
     }
  ')),

  tags$script('
    $(document).ready(function(){
      // id of the plot
      $("#distPlot").mousemove(function(e){ 

        // ID of uiOutput
        $("#my_tooltip").show();         
        $("#my_tooltip").css({             
          top: (e.pageY + 5) + "px",             
          left: (e.pageX + 5) + "px"         
        });     
      });     
    });
  '),

  selectInput("var_y", "Y-Axis", choices = names(iris)),
  plotOutput("distPlot", hover = "plot_hover", hoverDelay = 0),
  uiOutput("my_tooltip")


)

server <- function(input, output) {


  output$distPlot <- renderPlot({
    req(input$var_y)
    ggplot(iris, aes_string("Sepal.Width", input$var_y)) + 
      geom_point()
  })

  output$my_tooltip <- renderUI({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover)[input$var_y]
    req(nrow(y) != 0)
    verbatimTextOutput("vals")
  })

  output$vals <- renderPrint({
    hover <- input$plot_hover 
    y <- nearPoints(iris, input$plot_hover)[input$var_y]
    req(nrow(y) != 0)
    y
  })  
}
shinyApp(ui = ui, server = server)


You also can use ggvis package. This package is great, however, not enough mature yet

Example 4: ggvis and add_tooltip:

library(ggvis)

ui <- fluidPage(
  ggvisOutput("plot")
)

server <- function(input, output) {

  iris %>%
    ggvis(~Sepal.Width, ~Petal.Width) %>%
    layer_points() %>%
    layer_lines() %>% 
    add_tooltip(function(df) { paste0("Petal.Width: ", df$Petal.Width) }) %>%
    bind_shiny("plot")
}

shinyApp(ui = ui, server = server)


EDITED


Example 5:

After this post I searched internet to see whether it could be done more nicely than example 3. I found this wonderful custom tooltip for ggplot and I believe it can hardly be done better than that.

这篇关于如何在悬停在ggplot2中时在工具提示上显示y值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆