简体   繁体   English

创建不规则时间序列图的最简单方法(R?GGPLOT?ITS?)

[英]Easiest way to create an irregular time series graph (R? GGPLOT? ITS?)

I'm a graphic designer who is trying to use R to create graphs that are too complicated for Excel.我是一名平面设计师,正在尝试使用 R 创建对 Excel 来说过于复杂的图形。 I'm specifically trying to create an irregular time series step chart.我特别想创建一个不规则的时间序列阶梯图。 I've had no problems creating a regular time series chart, but for some reason, the irregular dates are throwing everything off.我在创建常规时间序列图表时没有遇到任何问题,但由于某种原因,不规则的日期会影响一切。

I'm starting with a basic text file with two columns of data:我从一个包含两列数据的基本文本文件开始:

01-04-1940    4
05-29-1963    35
12-02-2002    24

I've loaded the data using我已经使用加载数据

d <- read.delim("file.txt", header = TRUE)

and I've converted the first column in Unix time using我已经使用 Unix 时间转换了第一列

d$date <- as.Date(d$date, format = "%m-%d-%Y")

But at this point, I can't find any more information anywhere on how to proceed.但在这一点上,我无法在任何地方找到更多关于如何进行的信息。 I've seen the R package "ITS," But I cannot find any documentation on it beyond technical descriptions of the classes involved.我看过 R package “ITS”,但除了所涉及类的技术描述之外,我找不到任何关于它的文档。

I'd much appreciate it if someone with some experience in R could point out the few lines of code I need to create this graph.如果在 R 方面有一定经验的人能指出创建此图表所需的几行代码,我将不胜感激。 Thanks!谢谢!

ggplot deals quite nicely with data in date format. ggplot很好地处理日期格式的数据。 Here are some suggestions:以下是一些建议:

d <- data.frame(
    date = c("01-04-1940", "05-29-1963", "12-02-2002"),
    value = c(4, 35, 24)
)

d$date <- as.Date(d$date, format = "%m-%d-%Y")

ggplot(d, aes(x=date, y=value)) + geom_step(colour="blue")

在此处输入图像描述

ggplot(d, aes(x=date, y=value)) + geom_line(colour="red")

在此处输入图像描述

I would use xts/zoo.我会使用 xts/zoo。 They both handle irregular time series easily.他们都可以轻松处理不规则的时间序列。

z <- zoo(d[,2], d[,1])
plot(z)
plot(z, type="s")

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

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