R + ggplot: plotting irregular time series

前端 未结 2 903
余生分开走
余生分开走 2020-12-31 15:20

I have data at a number of days since an event. This data is sampled irregularly - my time points are like 0, 5, 6, 10, 104 days. I don\'t have specific date-time informatio

2条回答
  •  我在风中等你
    2020-12-31 16:08

    Not sure if this is what you're looking for (see this related question). You can reformat the axis and deal with irregularity by using the scale_x functions. For instance:

    p <- qplot(1:3, 1:3, geom='line') 
    p + scale_x_continuous("", breaks=1:3, 
            labels = as.Date(c("2010-06-03", "2010-06-04", "2010-06-07")))
    

    Incidentally, here's a function that I created for plotting multivariate zoo objects:

    qplot.zoo <- function(x) {
      if(!inherits(x, "zoo")) stop("x must be a zoo object")
      x.df <- data.frame(dates=index(x), coredata(x))
      x.df <- melt(x.df, id="dates", variable="value")
      ggplot(x.df, aes(x=dates, y=value, group=value, colour=value)) + geom_line() + opts(legend.position = "none")
    }
    

提交回复
热议问题