工具提示,当你鼠标悬停ggplot闪亮 [英] ToolTip when you mouseover a ggplot on shiny

查看:108
本文介绍了工具提示,当你鼠标悬停ggplot闪亮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我使用鼠标悬停功能时我想要一个显示数据框中某一列的工具提示(可自定义的工具提示)



您能否提出最好的前进方向?



简单应用:

 #ui.R 

shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
h4(TEst PLot)),
mainPanel(
plotOutput(plot1)


))

#server.R

图书馆(ggplot2)
数据(mtcars)

shinyServer(
function(input,output){
output $ plot1 < - renderPlot({
p < - ggplot(data = mtcars,aes(x = mpg,y = disp ,color = factor(cyl)))
p <-p + geom_point()
print(p)
})
}

当我将鼠标移到点上时,我希望它显示mtcars $ wt

解方案

如果我理解正确的问题,这可以通过光泽包为GGPLOT2和基础包两者最近更新来实现。使用Winston Chang和Joe Cheng的这个例子 http://shiny.rstudio.com/gallery/plot-interaction -basic.html ,我能够解决这个问题。 Hover现在是plotOutput()的一个输入参数,所以它被添加到ui以及verbatimTextOutput中,以显示悬停点的mtcars $ wt。

在服务器中,我基本上创建了一个距离向量,它可以计算从鼠标到图中任意点的距离,如果此距离小于3(在此应用程序中有效),那么它显示mtcars $ wt为您的鼠标的最近点。要清除输入$ plot_hover返回关于鼠标位置的信息列表,并且在此示例中仅从输入$ plot_hover中提取x和y元素。

  library(ggplot2)
library(Cairo)#对于部署在Linux上的更好的ggplot2输出

ui< - fluidPage(
fluidRow(
column(width = 12,
plotOutput(plot1,height = 350,hover = hoverOpts(id =plot_hover))

),
fluidRow(
column(width = 5,
verbatimTextOutput(hover_info)




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


输出$ plot1< - 渲染图({

ggplot(mtcars,aes(x = mpg,y = disp,color = factor(cyl)))+ geom_point()

})

输出$ hover_info< - renderPrint({
if(!is.null(input $ plot_hover)){
hover = input $ plot_hover
dist = sqrt((悬停$ x-mtcars $ mpg)^ 2 +( hover $ y-mtcars $ disp)^ 2)
cat(Weight(lb / 1000)\\\

if(min(dist)<3)
mtcars $ wt [which.min(d ist)]
}


})
}
shinyApp(ui,server)
pre>

我希望这有助于!


I am building a shiny application.

I am plotting charts using ggplot.

When I mouseover the points on the graph, I want a tooltip showing one of the columns in the data frame (customizable tooltip)

Can you please suggest the best way forward.

Simple App:

# ui.R

shinyUI(fluidPage(
 sidebarLayout(
    sidebarPanel(
        h4("TEst PLot")),
    mainPanel(
        plotOutput("plot1")
    )
)
))

# server.R

library(ggplot2)
data(mtcars)

shinyServer(
function(input, output) {
    output$plot1 <- renderPlot({
        p <- ggplot(data=mtcars,aes(x=mpg,y=disp,color=factor(cyl)))
        p <- p + geom_point()
        print(p)
    })
}
)

When I mouse over the points, I want it to show mtcars$wt

解决方案

If I understand the question correctly, this can be achieved with the recent update of the shiny package for both the ggplot2 and the base package. Using this example from Winston Chang and Joe Cheng http://shiny.rstudio.com/gallery/plot-interaction-basic.html , I was able to solve this problem. Hover is now an input argument into plotOutput() so that is added to the ui along with a verbatimTextOutput to display mtcars$wt for the point hovered over.

In the server I basically make a distance vector which calculates the distance from the mouse to any point in the plot and if this distance is less than 3 (works in this application) then it shows mtcars$wt for the closest point to your mouse. To be clear input$plot_hover returns a list of info about the location of the mouse and only the x and y elements are extracted from input$plot_hover in this example.

library(ggplot2)
library(Cairo)   # For nicer ggplot2 output when deployed on Linux

ui <- fluidPage(
    fluidRow(
        column(width = 12,
               plotOutput("plot1", height = 350,hover = hoverOpts(id ="plot_hover"))
        )
    ),
    fluidRow(
        column(width = 5,
               verbatimTextOutput("hover_info")
        )
    )
)

server <- function(input, output) {


    output$plot1 <- renderPlot({

        ggplot(mtcars, aes(x=mpg,y=disp,color=factor(cyl))) + geom_point()

    })

    output$hover_info <- renderPrint({
        if(!is.null(input$plot_hover)){
            hover=input$plot_hover
            dist=sqrt((hover$x-mtcars$mpg)^2+(hover$y-mtcars$disp)^2)
            cat("Weight (lb/1000)\n")
            if(min(dist) < 3)
                mtcars$wt[which.min(dist)]
        }


    })
}
shinyApp(ui, server)

I hope this helps!

这篇关于工具提示,当你鼠标悬停ggplot闪亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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