【问题标题】:R interactive plot: show vertical line on hoverR交互式绘图:悬停时显示垂直线
【发布时间】:2015-12-19 12:31:53
【问题描述】:

我一直在寻找一种在使用 R 将鼠标悬停在绘图中的点上时沿 x 轴绘制垂直线的方法。它是哪个包并不重要,无论是 plotly、ggvis、rCharts, googleVis 或任何其他的,但如果可能的话,我宁愿使用这些提到的其中之一。

这是我想要的示例。

【问题讨论】:

标签: r interactive rcharts ggvis googlevis


【解决方案1】:

部分答案(无法评论)... Plotly 具有“scattergl”类型,在悬停时绘制水平和垂直线。

数据

require(plotly)    

sdate <- as.Date("2015-01-01", format = "%Y-%m-%d")
timedf <- data.frame(Date = seq.Date(sdate, by="month", length.out=12),
                         Amount = runif(12, 0, 100))
# Plotly plot
plot_ly(timedf, x=Date, y=Amount, type="scattergl")

输出

【讨论】:

    【解决方案2】:

    按照克里斯的回答,让它发挥作用

    library(ggplot2)
    library(shiny)
    
    ui <- fluidPage(
        fluidRow(
            column(width = 12,
                   plotOutput("plot1", height = 350,hover = "plot_hover")
            )
        )
    )
    
    server <- function(input, output) {
        testPlot <- ggplot(mtcars, aes(x=mpg,y=disp,color=factor(cyl))) + 
            geom_point()
    
        #start up plot
        output$plot1 <- renderPlot({testPlot})
    
        # plot after mouse over
        observeEvent(input$plot_hover, {
            x = input$plot_hover$x
            y = input$plot_hover$y
            nearPoint <- nearPoints(mtcars, input$plot_hover, 
                                    threshold = 10, maxpoints = 1)
            output$plot1 <- renderPlot({
                if (nrow(nearPoint) == 1) {
                    testPlot + 
                        geom_vline(xintercept = nearPoint$mpg) +
                        geom_label(x = x + 1.5, y = y, 
                                   label = paste(rownames(nearPoint), "\n", nearPoint$disp))
                } else {
                    testPlot
                }
            })
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      【解决方案3】:

      这是部分答案。我不能让它工作,但也许有人会看到一些明显的东西。我使用ggplot2 而不是ggvis 来创建垂直线的geom_vline() 函数。

      什么工作:

      input$plot_hover 事件中,我们将x 坐标分配给一个变量(h),然后将该变量用作xintercept 参数给绘制一条垂直线的geom_vline() 函数。

      问题:

      由于这是在反应式环境中发生的,因此每次更新时都会刷新 h,因此该行在首次出现后大约一秒钟就会消失。

      我尝试过的:

      我尝试将h 分配给第二个变量t,以便在更新之间保持它。这不起作用,所以我创建了第三个变量prev_t,当没有输入时(is.null(input$plot_hover) == TRUE),保持tprev_t。这也不起作用,但我没有大量是时候尝试不同的东西了。

      代码如下:

      library(ggplot2)
      library(shiny)
      
      ui <- fluidPage(
          fluidRow(
              column(width = 12,
                     plotOutput("plot1", height = 350,hover = hoverOpts(id ="plot_hover"))
              )
          )
      )
      
      server <- function(input, output) {
          
         #h <- reactive(input$plot_hover)
      
         prev_t <- vector()
      
          output$plot1 <- renderPlot({
      
              if(!is.null(input$plot_hover)){
                  x <- input$plot_hover
                  h <- x$x
                  t <- h
      
      
              # the below isnt quite working
              # I was trying to store t between updates to make the line stay on 
              # the graph until there was a second hover event
      
              ################### !!! ###################
              } else if(is.null(input$plot_hover)) {
                  t <- prev_t
              }
      
              prev_t <- t
              ################## !!! ####################
      
              ggplot(mtcars, aes(x=mpg,y=disp,color=factor(cyl))) + geom_point() + geom_vline(xintercept = t)
      
          })
      
      
      }
      shinyApp(ui, server)
      

      希望这可能会让您走上不同的道路或有所帮助。如果有人看到可以解决此问题的方法,请告诉我。

      【讨论】:

      • 谢谢!我会对此进行试验,如果我能正常工作,我会回来!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多