【问题标题】:ggplotly Error in order: argument 1 is not a vectorggplotly 顺序错误:参数 1 不是向量
【发布时间】:2017-11-14 13:41:52
【问题描述】:

我正在尝试在 R 中的 shiny 应用程序中使用 ggplot2plotly。以下是我使用的代码:

# Loading the libraries ---------------
library(shiny)
library(tidyverse)
library(plotly)

# Global ----------------
if (!exists('ds', envir = .GlobalEnv, inherits = FALSE)) {
   ds <- readRDS("Analysis/Data/df_sim_updated.rds")
}

if (!exists('ado', envir = .GlobalEnv, inherits = FALSE)) {
  ado <- readRDS("Analysis/Data/df_ADOs.rds")
}

ds <- as_tibble(ds)
file_ID <- unique(ds$file.ID)
ado_Name <- c("ditiExpeon6", "VanC5", "NeonC4", "llacCadi3", "WhiteC2", 
              "Ford1", "BMWC10", "StarT7", "owT8Yell", "peC9Esca", "tarCWins13", 
              "rC11RoveLand", "F150C12", "CargoT4", "SemiT3", "RedT1", "RaceT11", 
              "BlueT5", "artTWalm9", "ghtTFrei10", "MoveT12", "WhiteT2", "ilT6Carg"
)

ado <- as_tibble(ado)


# Define UI for application --------------------
ui <- fluidPage(

   # Application title
   titlePanel("exploRing simulation"),

  # Sidebar 
   sidebarLayout(
      sidebarPanel(
        selectInput("fileid", label = h3("Select scenario"), 
                    choices = file_ID),

        selectInput("adoName", label = h3("Select ADO(s)"), 
                    multiple = TRUE, choices = ado_Name)
      ),

      # Main Panel
      mainPanel(
        uiOutput("FS"),

        plotlyOutput("plot1")

      )
   )
)

# Define server logic ---------------------------------
server <- function(input, output) {

  # Filter ds according to fileid
  data <- reactive({ds %>% filter(file.ID==input$fileid)})

  # Time Frame Slider
  output$FS <- renderUI(
    sliderInput("fs", label = h3("Time Frame"), min = min(data()$frames), 
                max = max(data()$frames), value = min(data()$frames), width = "800px")
  )

  # Position of OV at given time
  data2 <- reactive({data() %>% filter(frames==input$fs)})

  # Filter ado(s) according to fileid
  ado_data1 <- reactive(ado %>% 
                          filter(file.ID==input$fileid,
                                 ADO_name %in% input$adoName))

  # Position of ado at given time
  ado_data2 <- reactive(ado_data1() %>% 
                          filter(frames==input$fs))

  # Plot data
  output$plot1 <- renderPlotly(

  ggplotly( ggplot() +
    geom_line(data=data(),
               aes(x = x, y = y)) +
    geom_point(data = data2(),
               aes(x = x, y = y), size = 2) +
    geom_line(data = ado_data1(),
              aes(x = pos.2, y = pos.1, color = ADO_name)) +
    geom_point(data = ado_data2(),
               aes(x = pos.2, y = pos.1, color = ADO_name),
               size = 2) +
    geom_hline(yintercept = 278, linetype="longdash") +
    geom_hline(yintercept = 278-16) +
    geom_hline(yintercept = 278+16) +
      labs(x = "Longitudinal Position",
           y = "Lateral Position") +
    theme_bw())



  )





}

# Run the application ---------------------------------
shinyApp(ui = ui, server = server)

问题

以下是我收到的错误消息:

> runApp('exploRe')

Listening on http://127.0.0.1:7666
Warning: Error in filter_impl: incorrect length (0), expecting: 26826
Stack trace (innermost first):
    109: <Anonymous>
    108: stop
    107: filter_impl
    106: filter_.tbl_df
    105: filter_
    104: filter
    103: function_list[[k]]
    102: withVisible
    101: freduce
    100: _fseq
     99: eval
     98: eval
     97: withVisible
     96: %>%
     95: <reactive:data2> [C:\Users\my_username\Google Drive\DrivingSimulator\exploRe/app.R#65]
     84: data2
     83: fortify
     82: layer
     81: geom_point
     80: ggplotly
     79: ggplotly
     78: func
     77: origRenderFunc
     76: output$plot1
      1: runApp
Warning: Error in order: argument 1 is not a vector
Stack trace (innermost first):
    88: order
    87: [.data.frame
    86: [
    85: to_basic.GeomLine
    84: to_basic
    83: layers2traces
    82: gg2list
    81: ggplotly.ggplot
    80: ggplotly
    79: ggplotly
    78: func
    77: origRenderFunc
    76: output$plot1
     1: runApp  

如果我只使用renderPlotplotOutput,应用程序就可以正常工作。我在这里做错了什么?

【问题讨论】:

  • ADO被选中时可以工作。

标签: r ggplot2 shiny plotly


【解决方案1】:

我无法重现您的示例,但我在自己的应用程序中遇到并解决了完全相同的错误。出现此错误的原因是因为 Plotly 正在传递一个绘图对象,该对象本身没有数据,但只有与其他几何图层关联的数据。这是因为您在ggplot() 的初始调用中没有传递任何数据,并且传递给至少一个几何图形的重要数据列之一可能有问题(即丢失)。 (geom_path 在绘制之前对每个轴进行排序,所以可能是xy。)

如果你运行:

plot <- ggplot() +
    geom_line(data=data(),
               aes(x = x, y = y)) +
    geom_point(data = data2(),
               aes(x = x, y = y), size = 2) +
    geom_line(data = ado_data1(),
              aes(x = pos.2, y = pos.1, color = ADO_name)) +
    geom_point(data = ado_data2(),
               aes(x = pos.2, y = pos.1, color = ADO_name),
               size = 2) +
    geom_hline(yintercept = 278, linetype="longdash") +
    geom_hline(yintercept = 278-16) +
    geom_hline(yintercept = 278+16) +
      labs(x = "Longitudinal Position",
           y = "Lateral Position") +
    theme_bw())

plot$data

你可能会发现这样的输出:

list()
attr(,"class")
[1] "waiver"

显然 ggplot 可以很好地处理这个空对象,即使其中一层存在一些问题。它会打印一个警告,并错过那些因为缺少维度而无法绘制的点/层。

同样,Plotly (通常)不会对出现故障的层感到困惑,只要底层绘图对象中有一些数据,其他层仍然可以正常显示。这是由于其中一个图层的数据存在问题,以及主绘图对象中没有数据导致 Plotly 出现问题。

我建议您尝试单独绘制每一层,但在 ggplot() 中调用数据,以确定是否存在特定层的问题(我强烈怀疑存在)。此外,在传递给 plotly 之前,将每个对象绘制为普通图。

即:

plot <- ggplot(data = data()) +
    geom_line(aes(x = x, y = y)) 
print(plot)
ggplotly(plot)

plot <- ggplot(data = data2())+
    geom_point(aes(x = x, y = y), size = 2)
print(plot)
ggplotly(plot)

# and so on...

如果您在测试脚本和闪亮的应用程序中都进行这些检查,我相信您会隔离问题。

我还发现,在绘制它们之前尝试将不同的数据对象连接在一起也会有所帮助,即使是粗略地使用bind_rows()。 (仅在合适的情况下。)这样,您可以先将一条数据传递给ggplot() 调用,然后为每个几何图形分别调整美学。 Plotly 仍然为绘图获取了一个底层数据对象,这似乎使事情变得顺利,但可能不会很快揭示真正的底层问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多