简体   繁体   English

R时间序列数据的闪亮图,ggplot,仅产生NA

[英]R Shiny plot for time series data, ggplot, producing NAs only

I have problems with my ggplot in Shiny. 我在Shiny中的ggplot有问题。 I am new to Shiny, so there are probably some rookie mistakes in the code. 我是Shiny的新手,因此代码中可能存在一些菜鸟错误。 But I receive the following warnings: 但是我收到以下警告:

Listening on http://127.0.0.1:4278` 
Warning: Removed 93 rows containing non-finite values (stat_smooth).
Warning: Removed 93 rows containing missing values (geom_point).
Warning: Removed 1 rows containing missing values (geom_text).

The R code: R代码:

library(shiny)
library(ggplot2)

ggplot_df <- data.frame("start_ts"=c(1555279200,1555280100,1555281000,1555281900,1555282800), 
                        "V1"=c(6.857970e-04,7.144347e-05,1.398045e-06,2.997632e-05,2.035446e-06),
                        "sum"=c(20,21,22,15,23))
# Small test data set with 5 observations... 93 in original one

# Define UI for application
ui <- fluidPage(sliderInput("time", "Time:", 
                            min = as.POSIXct("00:00",format="%H:%M", tz=""),
                            max = as.POSIXct("24:00",format="%H:%M", tz=""),
                            value = c(
                              as.POSIXct("00:00",format="%H:%M")
                            ), timeFormat = "%H:%M", step=60*15, timezone = "",
                            animate=
                              animationOptions(interval=300, loop=TRUE)),
                plotOutput("plot")
)
# Define server logic required 
server <- function(input, output) {

  output$plot<-renderPlot({
    ggplot_df$start_ts <-as.POSIXct(ggplot_df$start_ts, format="%H:%M", tz="",origin="1970-01-01")
    ggplot_df<-ggplot_df[ggplot_df$start_ts==input$time,]

    ggplot(ggplot_df,aes(x=sum,y=V1))+geom_point() +
      theme_bw() +
      geom_smooth(method = "lm", se = FALSE) + 
      ylim(0,3) +
      xlim(0,max(ggplot_df$sum)) +
      annotate('text', max(ggplot_df$sum)-10,3, 
               label = paste("~R^{2}==",round(cor(ggplot_df$sum, ggplot_df$V1), digits=2)),parse = TRUE,size=4)
  })
}

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

Note that exactly the same thing is happening even if I define the time zone. 请注意,即使我定义了时区,也发生了完全相同的事情。

ggplot_df is a data frame with 93 rows. ggplot_df是具有93行的数据帧。 What have I done wrong? 我做错了什么? The plot I receive is empty, no points, etc, as shown below: 我收到的图是空的,没有点,等等,如下所示:

结果

The problem is that the POSIXct column is a datetime, but the slider input is only a time. 问题在于POSIXct列是日期时间,而滑块输入仅是时间。 Is date important here is it only time of day which is of interest? 这里重要的日期是一天中唯一有趣的时间吗? The code below makes some plots, although I can't tell what the desired end result is, so it may not be quite right 下面的代码作了一些图,尽管我不能说出所需的最终结果是什么,所以它可能不太正确

ui <- fluidPage(sliderInput("time", "Time:", 
                            min = as.POSIXct("2019-04-14 00:00",format="%Y-%m-%d %H:%M", tz=""),
                            max = as.POSIXct("2019-04-15 24:00",format="%Y-%m-%d %H:%M", tz=""),
                            value = c(
                              as.POSIXct("2019-04-14 00:00")
                            ), timeFormat = "%Y-%m-%d %H:%M", step=60*15, timezone = "",
                            animate=
                              animationOptions(interval=300, loop=TRUE)),
                plotOutput("plot")
)
# Define server logic required 
server <- function(input, output) {

  output$plot<-renderPlot({
    ggplot_df$start_ts <-as.POSIXct(ggplot_df$start_ts,  tz="",origin="1970-01-01")
    ggplot_df<-ggplot_df[ggplot_df$start_ts==input$time,]
    ggplot(ggplot_df,aes(x=sum,y=V1))+geom_point() +
      theme_bw() +
      geom_smooth(method = "lm", se = FALSE) + 
      ylim(0,3) +
      xlim(0,max(ggplot_df$sum)) +
      annotate('text', max(ggplot_df$sum)-10,3, 
               label = paste("~R^{2}==",round(cor(ggplot_df$sum, ggplot_df$V1), digits=2)),parse = TRUE,size=4)
  })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM