如何在三个相似的时间/值图表中使用公共轴

时间:2011-05-24 19:31:42

标签: r plot axis time-series measurement

我有三个带时间戳的测量系列,采用相同的间隔,但具有不同的实际时间戳。我想在组合图中显示这三个轨迹,但由于x轴(时间戳)在每种情况下都不同,我遇到了一些麻烦。有没有办法在不选择x轴的情况下使用和插值其他两个测量序列的y值?我对R很新,但我觉得我有一些明显的东西可以忽略。

例如:

系列1

Time    Value
1.023   5.786
2.564   10.675
3.678   14.678
5.023   17.456

系列2

0.787   1.765
1.567   3.456
3.011   5.879
4.598   7.768

系列3

1.208   3.780
2.478   6.890
3.823   9.091
5.125   12.769

4 个答案:

答案 0 :(得分:5)

使用基本图片,您可以使用plotpointslines的组合:

dat1 <- data.frame(Time = c(1.023, 2.564, 3.678, 5.023), Value = c(5.786, 10.675, 14.678, 17.456))
dat2 <- data.frame(Time = c(0.787, 1.567, 3.011, 4.598), Value = c(1.765, 3.456, 5.879, 7.768))
dat3 <- data.frame(Time = c(1.208, 2.478, 3.823, 5.125), Value = c(3.780, 6.890, 9.091, 12.769))

with(dat1, plot(Time, Value, xlim = c(0,6), ylim = c(0,20)))
with(dat2, points(Time, Value, col = "red"))
with(dat3, points(Time, Value, col = "green"))

查看?legend添加图例。或者,学习ggplot2并让它为您处理它的一部分:

library(ggplot2)
library(reshape)
plotdata <- melt(list(dat1 = dat1, dat2 = dat2, dat3 = dat3), "Time")

qplot(Time, value, data = plotdata, colour = L1)

答案 1 :(得分:3)

试试这个:

t1 <- "Time Value
1.023   5.786
2.564   10.675
3.678   14.678
5.023   17.456"

t2 <- "Time Value
0.787   1.765
1.567   3.456
3.011   5.879
4.598   7.768"

t3 <- "Time Value
1.208   3.780
2.478   6.890
3.823   9.091
5.125   12.769"

tex1 <- read.table(textConnection(t1), header = TRUE)
tex2 <- read.table(textConnection(t2), header = TRUE)
tex3 <- read.table(textConnection(t3), header = TRUE)

plot(tex1, type="l", xlim=range(tex1$Time, tex2$Time, tex3$Time), ylim=range(tex1$Value, tex2$Value, tex3$Value), main="Common Time Axis for 3 Data Series", col="black")
grid()
lines(tex2, col="red")
lines(tex3, col="blue")

enter image description here

答案 2 :(得分:1)

如果没有任何进一步的信息,您似乎必须使用以下组合:pointsxlim
使用plot绘制点(或线)的单个组合,传递xlim参数,以便所有时间输入都适合绘图。
然后,使用pointslines将其他数据添加到绘图中,也可以将color参数传递给这些函数以区分输出。
如果您包含一个可重复性最小的示例,我们可以提供更多详细信息!

答案 3 :(得分:0)

减去每个系列中的最小时间值。确定三个结果的最大值为xlim [2]。使用带有标签抑制的matplot进行绘图,然后使用axis()添加标签=和at =。