【问题标题】:Ploly graph in Shiny application is a flat line instead of working scatter plotShiny 应用程序中的绘图是一条平线,而不是工作散点图
【发布时间】:2018-04-11 13:40:37
【问题描述】:

我正在使用 shiny 创建一个下拉框,您可以在其中选择一个项目。选择该项目时,将显示绘图图。当我自己运行 plotly 图时,我得到了一个漂亮的散点图,但是当我在闪亮的应用程序中运行它时,我在三个项目中的每一个上都得到一条直线。

这是单独运行时有效的情节代码:

library(plotly)
plot_ly(df1, x = ~time, y = ~item1, color = ~item1) 
%>% add_lines(name = ~"item1")

这是 4 列 26949 行的数据集的样子:

      time                      item1    item2    item3
1     2018-04-09 00:00:06        615       NA       NA
2     2018-04-09 00:00:08         NA      465       NA
3     2018-04-09 00:00:08         NA       NA       NA
4     2018-04-09 00:00:10         NA       NA      422
5     2018-04-09 00:00:13         NA       NA       NA
6     2018-04-09 00:00:21        522       NA      385
7     2018-04-09 00:00:25         NA       NA       NA
....  ....                       ....     ....     ...
26949 2018-04-09 23:59:59        323       NA      200

Shiny代码如下:

#Import libraries
library(shiny)
library(plotly)

# Define UI for the application
ui <- bootstrapPage(

selectInput(inputId = "n_breaks",
            label = "Select Item:",
            choices = c("item1", "item2", "item3"),
            selected = "item1"),

plotlyOutput(outputId = "main_plot", height = "300px")

)

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

#Import data
df = read.csv("item_list.csv")

output$main_plot <- renderPlotly({

#Plotly graph
plot_ly(df, x = ~time, y = ~input$n_breaks, color = ~input$n_breaks) %>%
  add_lines(name = ~input$n_breaks)
})
}

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

在大多数情况下,此代码有效,但图表混乱。任何帮助将不胜感激!

【问题讨论】:

    标签: r shiny plotly r-plotly


    【解决方案1】:

    你应该把plot_ly函数写成:

    plot_ly(df, x = ~time, 
            y = df[, input$n_breaks], 
            color = df[, input$n_breaks]) %>%
            add_lines(name = ~input$n_breaks)
    

    或者稍微短一点:

    plot_ly(x = df[, "time"], 
            y = df[, input$n_breaks], 
            color = df[, input$n_breaks]) %>%
            add_lines(name = ~input$n_breaks)
    

    【讨论】:

    • 像@DJack 一样工作。谢谢!
    • 不客气。如果您认为它解决了您的问题,请考虑接受关闭问题的答案 (stackoverflow.com/help/someone-answers)。
    • 刚刚接受。还是有点新,谢谢你提醒我!
    猜你喜欢
    • 2021-09-04
    • 2018-09-22
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    相关资源
    最近更新 更多