我正在使用闪亮和ggplot2的交互式图形。我还使用"plot1_click“来获取x和y位置。
output$selected <- renderText({
paste0("Value=", input$plot1_click$x, "\n",
"Names=", input$plot1_click$y) }) #how to get names???这是服务器代码的一部分。这里我想要的不是打印"y“坐标,而是打印写在y轴上的相应名称。有什么可行的办法吗?
发布于 2017-09-26 18:08:48
据我所知,在plotOutput中不支持单击点。单击事件只返回单击位置的坐标。然而,这些坐标可以用来确定最近的点。
发亮的图片库页面中的这个闪亮的应用程序使用了shiny::nearPoints函数,它正是这样做的。下面是一个很小的例子。
library(shiny)
library(ggplot2)
shinyApp(
fluidPage(
plotOutput("plot", click = "plot_click"),
verbatimTextOutput('print')
),
server = function(input, output, session){
output$plot <- renderPlot({ggplot(mtcars, aes(wt, mpg)) + geom_point()})
output$print = renderPrint({
nearPoints(
mtcars, # the plotting data
input$plot_click, # input variable to get the x/y coordinates from
maxpoints = 1, # only show the single nearest point
threshold = 1000 # basically a search radius. set this big enough
# to show at least one point per click
)
})
}
)verbatimTextOutput向您显示与单击位置最近的点。请注意,nearPoints只适用于这样的ggplots图。但“帮助”页面表明,也有一种方法可以将其用于基本图形。
https://stackoverflow.com/questions/46407717
复制相似问题