热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

互动图:显示悬停的垂线-Rinteractiveplot:showverticallineonhover

Ihavebeenlookingaroundforawaytodrawaverticallinealongthex-axiswhenhoveringoverpoi

I have been looking around for a way to draw a vertical line along the x-axis when hovering over points in a plot using R. It does not really matter which package it is, whether it is plotly, ggvis, rCharts, googleVis or any other for that matter, but I would rather use one of the these mentioned ones if possible.

我一直四处寻找一种方法来画一条垂直线沿着x轴在悬停点阴谋使用r .哪个包并不重要,无论是情节,ggvis,rCharts,googleVis或其他,但我宁愿用这些提到的如果可能的话。

Here is an example of what I would like to have.

这是我想要的一个例子。

3 个解决方案

#1


2  

A partial answer (can't comment)... Plotly has type "scattergl" which draws a horizontal and vertical line on hover.

部分回答(不能评论)……Plotly有类型“分散”绘制水平和垂直线悬停。

Data

数据

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")

Output enter image description here

输出

#2


1  

Follow Chris' answer, to make it work

按照克里斯的回答,让它工作

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)

enter image description here

#3


0  

Here's a partial answer. I can't quite get it to work, but maybe someone will see something obvious. I've used ggplot2 instead of ggvis for the geom_vline() function which creates a vertical line.

这是一个部分的答案。我不能让它正常工作,但也许有人会看到一些显而易见的东西。我用了ggplot2代替了ge_vline()函数,它创建了一条垂直线。

What's working:

On the input$plot_hover event, we assign the x coordinate to a variable (h) and then use that variable as the xintercept arguement to the geom_vline() function which draws a vertical line.

在输入$plot_hover事件上,我们将x坐标赋给一个变量(h),然后使用该变量作为对绘制垂直线的geom_vline()函数的xintercept论述。

The problem:

Since this is happening in a reactive environment, on every update h is flushed, so the line disappears about a second after it first appears.

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

What I've tried:

I tried to assign h to a second variable t in order to keep it between updates. This didn't work, so I created a third variable prev_t and when there was no input (is.null(input$plot_hover) == TRUE), keep t as prev_t. This also isn't working but I don't have a ton of time to try out different things.

我试图把h赋给第二个变量t,以便在更新之间保持它。这不起作用,所以我创建了第三个变量prev_t,当没有输入(is.null(输入$plot_hover) == TRUE)时,保持t为prev_t。这也行不通,但我没有太多时间去尝试不同的东西。

Here's the code:

这是代码:

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)

Hopefully that should maybe put you on a different path or help a little. If someone sees something that could fix this problem, please let me know.

希望这能让你走上一条不同的道路或者有所帮助。如果有人看到什么可以解决这个问题,请告诉我。


推荐阅读
  • 也就是|小窗_卷积的特征提取与参数计算
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了卷积的特征提取与参数计算相关的知识,希望对你有一定的参考价值。Dense和Conv2D根本区别在于,Den ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • 拥抱Android Design Support Library新变化(导航视图、悬浮ActionBar)
    转载请注明明桑AndroidAndroid5.0Loollipop作为Android最重要的版本之一,为我们带来了全新的界面风格和设计语言。看起来很受欢迎࿰ ... [详细]
  • 不同优化算法的比较分析及实验验证
    本文介绍了神经网络优化中常用的优化方法,包括学习率调整和梯度估计修正,并通过实验验证了不同优化算法的效果。实验结果表明,Adam算法在综合考虑学习率调整和梯度估计修正方面表现较好。该研究对于优化神经网络的训练过程具有指导意义。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • 3.223.28周学习总结中的贪心作业收获及困惑
    本文是对3.223.28周学习总结中的贪心作业进行总结,作者在解题过程中参考了他人的代码,但前提是要先理解题目并有解题思路。作者分享了自己在贪心作业中的收获,同时提到了一道让他困惑的题目,即input details部分引发的疑惑。 ... [详细]
  • 本文介绍了机器学习手册中关于日期和时区操作的重要性以及其在实际应用中的作用。文章以一个故事为背景,描述了学童们面对老先生的教导时的反应,以及上官如在这个过程中的表现。同时,文章也提到了顾慎为对上官如的恨意以及他们之间的矛盾源于早年的结局。最后,文章强调了日期和时区操作在机器学习中的重要性,并指出了其在实际应用中的作用和意义。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文介绍了使用Spark实现低配版高斯朴素贝叶斯模型的原因和原理。随着数据量的增大,单机上运行高斯朴素贝叶斯模型会变得很慢,因此考虑使用Spark来加速运行。然而,Spark的MLlib并没有实现高斯朴素贝叶斯模型,因此需要自己动手实现。文章还介绍了朴素贝叶斯的原理和公式,并对具有多个特征和类别的模型进行了讨论。最后,作者总结了实现低配版高斯朴素贝叶斯模型的步骤。 ... [详细]
author-avatar
工商领域LW
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有