代码之家  ›  专栏  ›  技术社区  ›  Ray Tayek

如何在r中绘制xts时间序列?

  •  0
  • Ray Tayek  · 技术社区  · 3 年前

    下面对plot()的调用有效,而对ggplot(。我的视力很差,也许我错过了一些明显的东西?

    library(ggplot2)
    n=1000
    data <- rnorm(n) #Error in rpois(n) : argument "lambda" is missing, with no default
    dates <- seq(as.Date("2017-05-01"),length=n,by= "days")
    ts <- xts(x=data, order.by=dates)
    plot(ts)
    ggplot(ts)
    
    1 回复  |  直到 3 年前
        1
  •  0
  •   TarJae    3 年前

    我们可以这样做:

    library(ggplot2)
    library(xts)
    
    n <- 1000
    data <- rnorm(n)
    dates <- seq(as.Date("2017-05-01"), length = n, by = "days")
    ts <- xts(x = data, order.by = dates)
    
    # Option1: 
    autoplot(ts)
    
    
    # Option2 using ggplot2
    ggplot(df, aes(x = Index, y = data)) + 
      geom_line() +
      labs(x = "Date", y = "Value")
    

    enter image description here