在R闪亮的情节中悬停在大酒吧上方

时间:2016-10-13 08:26:58

标签: r hover shiny

我正在制作一个Shiny项目,我正在绘制大量数据,为了了解情节中每个条形的剂量来源,我使用了悬停。 / p>

当我将鼠标悬停在某个条形图上时,我会使用nearPoints()函数获取它引用的数据框中的行。 (来自shiny包)

如果酒吧很小我没问题。但是如果我有足够大的条形,或者我放大了,我需要将鼠标悬停在条形的开始处,当我将鼠标悬停在条形的任何部分上时,有没有办法获得该行? 谢谢!

# reproducible example
# when hovering over the small segments/bars there's no problem
# hovering over the long/short, you need to go to the beggining
# of the segment/bar in order to see the hovering info
library(shiny)
library(stats)
library(ggplot2)

ui <- shinyUI(fluidPage(
  titlePanel(""),
  fluidRow(verbatimTextOutput("hover_info")),
  fluidRow(plotOutput("myplot", hover = hoverOpts(id ="myplot_hover")))
))

id <- c(rep("mut1 vs wt", 100), "long", "short")
s <- c(seq(10, 1000, length.out = 100), 30, 90)
e <- c(seq(15, 1005, length.out = 100), 900, 210)
log <- c(rnorm(100), -3, 2)
df <- data.frame(id,s,e,log)
colnames(df) <- c("id", "start","end","log2")

server <- shinyServer(function(input, output) {
  output$myplot <- renderPlot({
    ggplot(df) + facet_grid(id~.) + 
      geom_segment(data=subset(df, grepl("wt",df$id)),
                   mapping=aes(x=start,xend=end,y=log2,yend=log2,log2>0,color=log2>0)) +
      geom_segment(data=subset(df,grepl("short$",id)),
                   mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) +
      geom_segment(data=subset(df,grepl("long$",id)),
                   mapping=aes(x=start,xend=end,y=log2,yend=log2,size=3,color=log2>0)) #+
  })
  output$hover_info <- renderPrint({
    nearPoints(df, input$myplot_hover,maxpoints=1)
  })
})

shinyApp(ui, server)

0 个答案:

没有答案