gpt4 book ai didi

r - ggplotly 错误顺序为 : argument 1 is not a vector

转载 作者:行者123 更新时间:2023-12-02 11:17:14 26 4
gpt4 key购买 nike

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

# 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 ,该应用程序运行良好。我在这里做错了什么?

最佳答案

我无法重现您的示例,但我在自己的应用程序中遇到并解决了完全相同的错误。此错误的原因是因为 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 之前,将每个对象绘制为正常图。
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...
如果您在测试脚本和 Shiny 的应用程序中进行这些检查,我相信您会隔离问题。
我还发现在绘制之前尝试将不同的数据对象连接在一起也有帮助,即使是粗略地使用 bind_rows() . (仅在合适的情况下。)这样您就可以将一条数据传递给 ggplot()首先调用,并为每个几何图形分别调整美感。 Plotly 仍然为绘图获取一个底层数据对象,这看起来很顺利,但可能不会那么快揭示真正的潜在问题。

关于r - ggplotly 错误顺序为 : argument 1 is not a vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com