简体   繁体   English

R + ggplot:绘制不规则的时间序列

[英]R + ggplot: plotting irregular time series

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. 这些数据是不定期抽样的 - 我的时间点分别为0,5,6,10,104天。 I don't have specific date-time information - ie I have no idea when in real life the event I'm studying occurred. 我没有具体的日期时间信息 - 即我不知道在现实生活中我正在研究的事件发生了什么。

I'd like to plot, using ggplot, my time series. 我想用ggplot绘制我的时间序列。 I can use, say 我可以用,比方说

p <- ggplot(data,aes(x=time,y=expression))
p <- p + geom_point()

but of course my x-axis variables are plotted next to each other, so that the distance between t=10 and t=104 is the same as t=5 and t=6. 但当然我的x轴变量是彼此相邻绘制的,因此t = 10和t = 104之间的距离与t = 5和t = 6相同。 So I can make something up like 所以我可以做点什么

start <- ISOdate(2001, 1, 1, tz = "")
data$time <- start + data$time*60*60*12

which almost works, but now the ticks on my x-axis are horribly inaccurate date times. 几乎可以工作,但现在我的x轴上的刻度是非常不准确的日期时间。 I could re-format them maybe? 我可以重新格式化它们吗? But can't see anyway to make the format "days from start". 但是无论如何都看不到格式“从开始的日子”。 And by now I've been googling around for quite a while, with the nagging feeling that I'm missing something seriously obvious. 到现在为止,我一直在谷歌上搜索相当长一段时间,有一种唠叨的感觉,我错过了一些非常明显的东西。 Am I? 我呢?

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. 您可以使用scale_x函数重新格式化轴并处理不规则性。 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: 顺便说一句,这是我为绘制多变量zoo对象而创建的函数:

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")
}

Sounds like your time variable is a factor or maybe a character vector, not a numeric value! 听起来你的time变量是一个因素,也可能是一个字符向量,而不是数值! If you do data$time <- as.numeric(data$time) it may well solve your problem. 如果你做data$time <- as.numeric(data$time)它可能很好地解决你的问题。

ggplot is pretty good at using the right sort of scale for the right sort of data. ggplot非常擅长为正确的数据类型使用正确的比例。 (Sadly, data import routines in R generally are less smart...) (遗憾的是,R中的数据导入程序通常不那么聪明......)

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

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