【问题标题】:error when displaying more details in plotly tooltip在绘图工具提示中显示更多详细信息时出错
【发布时间】:2018-05-07 21:03:42
【问题描述】:

我试图在一个点上显示比默认点坐标更多的数据。 当我只显示一个额外的信息时它会起作用,例如:

 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

这意味着粘贴时值有问题。 但是我不知道如何将所有三个变量从 filters() 数据框中包含到 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

【问题讨论】:

  • 你能分享完整的代码以便我们运行它吗?
  • 我的代码和数据集都起来了。

标签: r ggplot2 shiny plotly r-plotly


【解决方案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"))

  })

【讨论】:

  • 它似乎可以按我的意愿工作,但是现在默认显示的点值(坐标)不再显示。有没有办法让它们也留在那里?我不想在 X 和 Y 变量更改时对它们进行硬编码
  • @Alex T. 请参阅编辑。你可以简单地使用这个:ggplotly(a, tooltip = c("x", "y", "text")).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
相关资源
最近更新 更多