r语言 - 在绘图工具提示中显示更多详细信息时出错



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

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

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

编辑:以下是允许您重现错误的代码,以及 im 为此使用的数据集示例:

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

也许你可以试试这个:

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"))
})

相关内容

最新更新