ggplotly 顺序错误:参数 1 不是向量

uma*_*ani 8 r ggplot2 shiny plotly

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

# 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)
Run Code Online (Sandbox Code Playgroud)

问题

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

> 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  
Run Code Online (Sandbox Code Playgroud)

如果我只使用renderPlotand plotOutput,该应用程序运行良好。我在这里做错了什么?

小智 6

我无法重现您的示例,但我在自己的应用程序中遇到并解决了完全相同的错误。出现此错误的原因是因为 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
Run Code Online (Sandbox Code Playgroud)

您可能会发现如下输出:

list()
attr(,"class")
[1] "waiver"
Run Code Online (Sandbox Code Playgroud)

显然 ggplot 对于这个空对象没问题,即使其中一层存在一些问题。它将打印一条警告,并且会错过那些由于缺少尺寸而无法绘制的点/图层。

同样地,Plotly(通常)不会对故障层感到烦恼,只要底层绘图对象中有一些数据,其他层仍然会正常显示。其中一层的数据问题以及主绘图对象中没有数据共同导致了 Plotly 问题。

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

IE:

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...
Run Code Online (Sandbox Code Playgroud)

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

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