在plotly工具提示中显示更多细节时出错

时间:2018-05-07 21:03:43

标签: r ggplot2 shiny plotly r-plotly

我试图在一个点上显示关于胡佛的更多数据,而不是默认点坐标。 当我只显示一个额外信息时,它可以工作,例如:

 output$myplot <- renderPlotly({
    if (is.null(filtered())) {
      return()
    }
    ggplot(filtered(), aes_string(x=input$xvar, y=input$yvar, text=filtered()$Ep.name)) +
      geom_point()
  })

工作得很好,我得到了我想要实现的东西(这是我传递给text变量的数据。但是当我尝试传递更多变量时,使用paste

 ggplot(filtered(), aes_string(x=input$xvar, y=input$yvar, text=paste("name: ",filtered()$Ep.name, "season: ", filtered()$Season, "number: ", filtered()$Ep.Number))) +
      geom_point()

我收到此错误:

Warning: Error in parse: <text>:1:12: unexpected symbol
1: name:  The Kingsroad

这意味着粘贴时该值有问题。 但是我不知道如何将filter()数据框中的所有三个变量包含到aes_string中,以便它们全部显示在工具提示中。 任何人都知道如何解决这个问题?

编辑:这是允许您重现错误的代码,以及我正在使用的数据集示例:

library(shiny)
library(ggplot2)
library(dplyr)
library(plotly)


shows <- read.csv("finalR1.csv", header=TRUE)

ui <- fluidPage(
  tabsetPanel(
    tabPanel(h1("Plot"),
             plotlyOutput("myplot"),
             hr()),
    tabPanel(h1("Table"), tableOutput("results"))
  ),

  fluidRow(
    column(3,
           h4("Episode explorer"), 
           sliderInput("voteInput", "Votes", min = 0, max = 155000, value = c(2500, 40000)),
           br(),
           sliderInput("lenInput", "Length", min = 0, max = 110, value = c(0, 60)),
           br(),
           uiOutput("ratingOutput")

    ),

   column(4,offset = 0.5,
          h4('Axis display options'),
          selectInput('xvar', 'X', choice=c("Length", "Ep.Rating", "Votes", "Year"), selected="Ep.Rating"),
          selectInput('yvar', 'Y', choice=c("Length", "Ep.Rating", "Votes", "Year"), selected="Votes")
          ))

)


server <- function(input, output) {
  output$ratingOutput <- renderUI({
    selectInput("ratingInput", "Ratings",
                c("All", as.character(sort(unique(shows$TV.Rating)))),
                selected = "All")
  })


  filtered<-reactive({
    if (is.null(input$ratingInput)) {
      return(NULL)
    }

    shows %>%
    filter(Votes >= input$voteInput[1],
           Votes <= input$voteInput[2],
           Length >= input$lenInput[1],
           Length <= input$lenInput[2],
           if (input$ratingInput != "All") {
             TV.Rating == input$ratingInput
           } else TRUE

    )

  })

  output$myplot <- renderPlotly({
    if (is.null(filtered())) {
      return()
    }
    ggplot(filtered(), aes_string(x=input$xvar, y=input$yvar, text=paste("name: ",filtered()$Ep.name, "season: ", filtered()$Season, "number: ", filtered()$Ep.Number))) +
      geom_point()
  })

  output$results <- renderTable({
    filtered()
  })
}

shinyApp(ui = ui, server = server)

https://drive.google.com/file/d/15ZqzY_msBBlBnrrqsqeagZSpJxifKKjM/view?usp=sharing

1 个答案:

答案 0 :(得分:1)

也许你可以试试这个:

  output$myplot <- renderPlotly({
    if (is.null(filtered())) {
      return()
    }

  a <- ggplot(filtered(), aes_string(x=input$xvar, y=input$yvar))  +
      geom_point(aes(text=paste('name:',filtered()$Ep.name, '<br>season:', filtered()$Season, '<br>number:', filtered()$Ep.Number)))

  ggplotly(a, tooltip = c("x", "y", "text"))

  })