如何将转换后的值悬停在ggplot上的

时间:2017-06-08 14:33:19

标签: r ggplot2 shiny hover

我希望在悬浮在有光泽的地块上时显示工具提示中的一些数据。我使用了this脚本,当我使用变量(pmin_p)时,数据显示得很好。但是,我真正需要的是显示日志转换的pmin_p,因此我的ggplot将如下所示:

ggplot(dataset, aes(x = -log10(p), y = -log10(min_p))) +
    geom_point()

这就是问题,因为hover_info不再识别数据,我收到以下错误:

  

错误:替换表有0行,替换表有20行

我不确定如何解决这个问题,我承认我并不完全理解悬停是如何运作的。

以下是包含示例数据的代码:

library(shiny)
library(shinydashboard)
library(ggplot2)

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Plot result", tabName = "scatterplot", icon = icon("area-chart"))
  ))

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "scatterplot", 
            fluidRow(
              box(
                uiOutput("scatterPlotButton"),
                width = 5
              ),
              box(
                title="PLOT",solidHeader = TRUE, status="primary",
                plotOutput("plot", 
                           hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
                uiOutput("hover_info"),
                width=9
              )
            )
    )
  )
)


ui=dashboardPage(
  dashboardHeader(title = "analysis"),
  sidebar,
  body
)


server=shinyServer(function(input, output, session) {
  dataset <- structure(list(p = c(6.03934743495282e-02, 1.50433174696588e-01, 
                                  2.08047037184403e-03, 5.89297106629446e-05, 0.000102485231497565, 
                                  0.0010651774924444, 0.0126458836222225, 0.000210364148948929, 
                                  0.00274720409905674, 0.281095738489031, 0.000316170681574214, 
                                  0.0316321461125659, 0.000369171267912158, 0.000369171267912158, 
                                  0.0395213746526263, 0.475174078010843, 0.000718770258398781, 
                                  0.760859052164441, 0.000810153915789446, 0.000875314011490406), 
                            ratio_p_group_min = c(1.57380553778931, 1.11245772000324, 
                                  1.504084996599, 1.00963266560562, 1.28098052443163, 1.49882201127675, 
                                  1.10761702001084, 0.767267328293303, 1.03412495601202, 1.33508933929913, 
                                  0.835478202626155, 0.998537147454481, 1.2008830437325, 1.2008830437325, 
                                  1.15710746065582, 0.99677375722945, 1.37744067975694, 1.3666109673056, 
                                  1.34583027836758, 1.34766012381264),
                            min_p = c(0.15789, 0.25772, 0.56599, 0.99632, 0.00004, 0.00275, 0.10761, 
                                      0.76726, 0.00103, 0.00013, 0.83547, 0.99853, 0.00120, 0.12008, 
                                      0.01157, 0.99677, 0.01377, 0.13666, 0.14583, 0.01347),
                            genes = c("Gene1", "Gene2", "Gene3", "Gene4", "Gene5", "Gene6", "Gene7", 
                                      "Gene8", "Gene9", "Gene10", "Gene11", "Gene12", "Gene13", "Gene14", 
                                      "Gene15", "Gene16", "Gene17", "Gene18", "Gene19", "Gene20")),
                       .Names = c("p", "ratio_p_group_min","min_p","genes"), 
                       row.names = c(NA, 20L), class = "data.frame")



output$scatterPlotButton <- renderUI({
  actionButton("scatterPlotButton", "Generate Plot", class="btn-block btn-primary")
}) 

scatterPlot <- eventReactive(input$scatterPlotButton,{
  if (is.null(input$scatterPlotButton)) return()
  dataset <- dataset[which(round(dataset$ratio_p_group_min,digits=2)>=0 & round(-log10(dataset$p),digits=2)>=0 & !is.na(dataset$ratio_p_group_min)),]
  dataset$ratio_p_group_min=ifelse(dataset$ratio_p_group_min>2 & dataset$p>0.05,1,dataset$ratio_p_group_min)
  ggplot(dataset, aes(x = -log10(p), y = -log10(min_p))) +
    geom_point()
})


output$plot <- renderPlot({ scatterPlot() })


output$hover_info <- renderUI({
  if (is.null(input$scatterPlotButton)) return()
  dataset <- dataset[which(round(dataset$ratio_p_group_min,digits=2)>=0 & round(-log10(dataset$p),digits=2)>=0 & !is.na(dataset$ratio_p_group_min)),]
  hover <- input$plot_hover
  point <- nearPoints(dataset, hover, threshold = 5, maxpoints = 1, addDist = TRUE)
  if (nrow(point) == 0) return(NULL)

  left_pct <- (hover$x - hover$domain$left) / (hover$domain$right - hover$domain$left)
  top_pct <- (hover$domain$top - hover$y) / (hover$domain$top - hover$domain$bottom)


  left_px <- hover$range$left + left_pct * (hover$range$right - hover$range$left)
  top_px <- hover$range$top + top_pct * (hover$range$bottom - hover$range$top)

  style <- paste0("position:absolute; z-index:100; background-color: rgba(245, 245, 245, 0.85); ",
                  "left:", left_px + 2, "px; top:", top_px + 2, "px;")
  wellPanel(
    style = style,
    p(HTML(paste0(point)))
  )
})
})

shinyApp(ui=ui, server=server)

更新

我找到了一个不那么优雅的解决方案:在dataset-log10(p)的{​​{1}}中创建新变量,并在-log10(min_p)中使用这些新变量。

ggplot

但我仍然想知道这是否可以以某种方式避免。

1 个答案:

答案 0 :(得分:0)

您也可以使用图表包。只需在ggplot对象后调用ggploty()即可。