【发布时间】: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