R + ggplot:绘制不规则时间序列 [英] R + ggplot: plotting irregular time series

查看:18
本文介绍了R + ggplot:绘制不规则时间序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有事件发生后几天的数据.这些数据是不定期采样的——我的时间点是 0、5、6、10、104 天.我没有具体的日期时间信息——也就是说,我不知道我正在研究的事件在现实生活中是什么时候发生的.

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 information - i.e. I have no idea when in real life the event I'm studying occurred.

我想使用 ggplot 绘制我的时间序列.我可以使用,说

I'd like to plot, using ggplot, my time series. I can use, say

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

当然,我的 x 轴变量是彼此相邻绘制的,因此 t=10 和 t=104 之间的距离与 t=5 和 t=6 相同.所以我可以编造一些类似的东西

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. So I can make something up like

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

这几乎可以工作,但现在我的 x 轴上的刻度是非常不准确的日期时间.我可以重新格式化它们吗?但无论如何都看不到制作从开始的天数"的格式.到现在为止,我已经在谷歌上搜索了很长一段时间,总感觉我错过了一些非常明显的东西.我是吗?

which almost works, but now the ticks on my x-axis are horribly inaccurate date times. 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?

推荐答案

听起来你的 time 变量是一个因素,或者可能是一个字符向量,而不是一个数值!如果您这样做 data$time <- as.numeric(data$time) 它可能会很好地解决您的问题.

Sounds like your time variable is a factor or maybe a character vector, not a numeric value! If you do data$time <- as.numeric(data$time) it may well solve your problem.

ggplot 非常擅长为正确的数据使用正确的比例.(遗憾的是,R 中的数据导入例程通常不太智能......)

ggplot is pretty good at using the right sort of scale for the right sort of data. (Sadly, data import routines in R generally are less smart...)

这篇关于R + ggplot:绘制不规则时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆