简体   繁体   English

R Shiny:ggplot中的工具提示

[英]R Shiny: Tooltip in ggplot

I want the values of the height or weight to show when I hover over a point in the graph. 我希望将鼠标悬停在图形中的某个点时显示身高或体重的值。 I already tried to make this work by using the plotly package and the example of this link . 我已经尝试通过使用plotly包和此链接的示例来使此工作有效。 But I got all kind of errors and I do not know how to make it work. 但是我遇到了各种各样的错误,我不知道如何使它起作用。

I've included my whole code so I hope someone can help me with this problem. 我已经包含了整个代码,所以希望有人可以帮助我解决这个问题。

library("shiny")
library("ggplot2")
library('readxl')
library('gridExtra')

ui<- fluidPage(
   titlePanel("Animals"),
   sidebarLayout(
     sidebarPanel(
  helpText("Create graph of height and/or weight animals"),
  selectInput("location", 
              label = "Choose a location",
              choices = list("New York"="New York", "Philadelphia" = "Philadelphia"),
              selected = "New York"),
  uiOutput("animal"),
  checkboxGroupInput("opti", 
              label = "Option",
              choices = c("weight", "height"),
              selected = "weight")
  ),
mainPanel(plotOutput("graph"))
))

server <- function(input, output){
  animal <- read_excel('data/animals.xlsx', sheet =1)
  var <- reactive({
    switch(input$location,
       "New York" = list("Cat1", "Dog2"),
       "Philadelphia"= list("Cat4","Dog3"))
     })

  output$animal <- renderUI({
  checkboxGroupInput("anim", "Choose an animal",
                   var())
  })

output$graph <- renderPlot({
  if (length(input$anim)==1){
    p <- ggplot(subset(animal, Name %in% input$anim & Location %in% input$location), aes(x=date))
    if ("weight" %in% input$opti){
      p <- p + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
    }
    if ("height" %in% input$opti){
      p <- p + geom_line(aes(y=height)) + geom_point(aes(y=height))
    }
    print(p)
  }

  if (length(input$anim)==2){
    p1 <- ggplot(subset(animal, Name %in% input$anim[1] & Location %in% input$location), aes(x=date))
    p2 <- ggplot(subset(animal, Name %in% input$anim[2] & Location %in% input$location), aes(x=date))
    if ("weight" %in% input$opti){
      p1 <- p1 + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
      p2 <- p2 + geom_line(aes(y=weight)) + geom_point(aes(y=weight))
    }
    if ("height" %in% input$opti){
      p1 <- p1 + geom_line(aes(y=height)) + geom_point(aes(y=height))
      p2 <- p2 + geom_line(aes(y=height)) + geom_point(aes(y=height))
    }
    grid.arrange(p1,p2, ncol = 2)
  }
})
}
shinyApp(ui=ui, server= server)

A part of the data: 数据的一部分:

Location    Name    date    weight  height
New York    Cat1    Mar-16  34,20   22,50
New York    Cat1    Apr-16  35,02   23,02
New York    Cat1    May-16  35,86   23,55
New York    Cat1    Jun-16  36,72   24,09
New York    Dog2    Mar-16  33,55   22,96
New York    Dog2    Apr-16  33,62   23,42
New York    Dog2    May-16  33,68   23,89
New York    Dog2    Jun-16  33,75   24,37
Philadelphia    Cat4    Mar-16  20,33   16,87

I used this tooltip and customised it a little bit. 我使用了工具提示并对其进行了一些自定义。

Your plots initially don't show up because you don't return any plot. 您的图最初不会显示,因为您没有返回任何图。 I return an ggplot object p without calling print function. 我返回了ggplot对象p而不调用print函数。

In general, I heavily modified your code and this is the result: 通常,我对您的代码进行了重大修改,结果如下:

在此处输入图片说明

As the function nearPoints needs the same dataset that you pass to ggplot, I had to create a new reactive , in which I did some subsetting and reshaping of your data. 由于功能nearPoints需要你传递给ggplot相同的数据集,我不得不创建一个新的reactive ,在我做你的一些数据子集和整形。

Instead of grid.arrange to create two seperate plots I used facet_grid (and hence I had to transform the data). 我不是使用grid.arrange来创建两个单独的图,而是使用facet_grid (因此必须转换数据)。 I also used colours to differentiate lines. 我还使用颜色区分线条。

Everything works fine with the example data you provided. 您提供的示例数据一切正常。


Full example: 完整示例:

rm(ui)
rm(server)

library("shiny")
library("ggplot2")
library('readxl')
library('gridExtra')
library(reshape) # for "melt"

ui<- fluidPage(
  titlePanel("Animals"),
  sidebarLayout(
    sidebarPanel(
      helpText("Create graph of height and/or weight animals"),
      selectInput("location", 
                  label = "Choose a location",
                  choices = list("New York"="New York", "Philadelphia" = "Philadelphia"),
                  selected = "New York"),
      uiOutput("animal"),
      checkboxGroupInput("opti", 
                         label = "Option",
                         choices = c("weight", "height"),
                         selected = "weight")
    ),
    mainPanel(

      # this is an extra div used ONLY to create positioned ancestor for tooltip
      # we don't change its position
      div(
        style = "position:relative",
        plotOutput("graph", 
                   hover = hoverOpts("plot_hover", delay = 100, delayType = "debounce")),
        uiOutput("hover_info")
      )

    )
  ))

server <- function(input, output){

  animal <- read_excel('data/animals.xlsx', sheet =1)
  #animal <- read_excel("~/Downloads/test2.xlsx")
  var <- reactive({
    switch(input$location,
           "New York" = c("Cat1", "Dog2"),
           "Philadelphia"= c("Cat4","Dog3"))
  })

  output$animal <- renderUI({
    checkboxGroupInput("anim", "Choose an animal",
                       var())
  })


  output$graph <- renderPlot({
    req(input$anim, sub())

    if (length(input$anim) == 1) {
      p <- ggplot(sub(), aes(x = date, colour = variable))
      p <- p + geom_line(aes(y = value)) + 
               geom_point(aes(y = value)) +
        guides(colour = guide_legend(title = NULL))

      return(p) # you have to return the plot
    }

    if (length(input$anim) == 2) {

      p <- ggplot(sub(), aes(x = date, colour = variable)) +
        geom_line(aes(y = value)) + 
        geom_point(aes(y = value)) + 
        facet_grid(~ Name) + 
        guides(colour = guide_legend(title = NULL))

      return(p) # you have to return the plot
    }
  })

  observe({
    print(sub())
  })


  sub <- reactive({
    req(input$anim)

    if (length(input$anim) == 1) {

      df <- animal[animal$Name %in% input$anim & animal$Location %in% input$location, ]
      df <- melt(as.data.frame(df), measure.vars = c("weight", "height"))
      df <- subset(df, df$variable %in% input$opti)
      return(df)
    }

    if (length(input$anim) == 2) {
      df <- animal[animal$Name %in% input$anim & animal$Location %in% input$location, ]
      df$Name <- factor(df$Name)
      df <- melt(as.data.frame(df), measure.vars = c("weight", "height"))
      df <- subset(df, df$variable %in% input$opti)
      return(df)
    }
  })

  output$hover_info <- renderUI({
    hover <- input$plot_hover
    point <- nearPoints(sub(), 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("<b>", point$variable, ": </b>", point$value)))
    )
  })


}
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM