3

我有三个带时间戳的测量系列,在相同的时间间隔内,但具有不同的实际时间戳。我想在一个组合图中显示这三个轨迹,但是因为 x 轴(时间戳)在每种情况下都不同,所以我遇到了一些麻烦。有没有办法做到这一点,而无需选择要使用的 x 轴并插入其他两个测量系列的 y 值?我对 R 很陌生,但我觉得有一些明显的东西我忽略了。

例如:

系列一

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

4 回答 4

5

对于基本图形,您可以使用plotandpoints或的组合lines

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)
于 2011-05-24T19:56:21.203 回答
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")

在此处输入图像描述

于 2011-05-24T20:12:44.003 回答
1

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

于 2011-05-24T19:47:12.027 回答
0

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

于 2011-05-24T20:04:30.700 回答