【发布时间】:2017-02-08 03:15:48
【问题描述】:
我想看看我是否可以在 Shiny 应用中创建折线图:
- 画一条垂直线,然后
- 标签
每个geom_line()上最接近鼠标悬停点x值的数据点,类似于这两个图表的组合:
Vertical Line through Mouse Hover Point
Data Label for Point at x-value of Mouse Hover Point
这是我第一次尝试让我的 ggplot 图形交互。我遇到了一些奇怪的行为,希望有人可以向我解释。我的可重现示例如下。它创建两个系列并用geom_line() 绘制它们。我距离我想要的最终状态(如上所述)只有几步之遥,但我的直接问题是:
- 当鼠标超出绘图范围时,如何消除垂直线?我尝试过的所有操作(比如将
NULL传递给xintercept,如果input$plot_hover是NULL)会导致绘图出错。 - 为什么,当鼠标在绘图范围内时,
geom_vline会到处弹跳?为什么鼠标停止移动后又回到x = 0?
谢谢。
library(shiny)
library(ggplot2)
library(tidyr)
library(dplyr)
ui <- fluidPage(
titlePanel("Interactive Plot"),
sidebarLayout(
sidebarPanel(
sliderInput("points",
"Number of points:",
min = 10,
max = 50,
value = 25),
textOutput(outputId = "x.pos"),
textOutput(outputId = "y.pos"),
textOutput(outputId = "num_points")
),
mainPanel(
plotOutput("distPlot", hover = hoverOpts(id = "plot_hover",
delay = 100,
delayType = "throttle")))))
server <- function(input, output) {
# Create dataframe and plot object
plot <- reactive({
x <- 1:input$points
y1 <- seq(1,10 * input$points, 10)
y2 <- seq(20,20 * input$points, 20)
df <- data.frame(x,y1,y2)
df <- df %>% gather(key = series, value = value, y1:y2)
ggplot(df,aes(x=x, y=value, group=series, color=series)) +
geom_line() +
geom_point() +
geom_vline(xintercept = ifelse(is.null(input$plot_hover),0,input$plot_hover$x))
})
# Render Plot
output$distPlot <- renderPlot({plot()})
# Render mouse position into text
output$x.pos <- renderText(paste0("x = ",input$plot_hover$x))
output$y.pos <- renderText(paste0("y = ",input$plot_hover$y))
}
# Run the application
shinyApp(ui = ui, server = server)
【问题讨论】: