Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何为三个类似的时间/值图使用公共轴_R_Plot_Axis_Time Series_Measurement - Fatal编程技术网

R 如何为三个类似的时间/值图使用公共轴

R 如何为三个类似的时间/值图使用公共轴,r,plot,axis,time-series,measurement,R,Plot,Axis,Time Series,Measurement,我有三个带时间戳的测量序列,在相同的时间间隔内,但实际时间戳不同。我想在一个组合图中显示这三条轨迹,但因为x轴(时间戳)在每种情况下都不同,所以我遇到了一些麻烦。是否有一种方法可以做到这一点,而无需拾取x轴来使用和插值其他两个测量系列的y值?我对R相当陌生,但我觉得有些明显的东西我忽略了 例如: 系列1 系列2 系列3 在没有任何进一步信息的情况下,您似乎必须使用以下组合:点和xlim 使用Plot,通过xlim参数,绘制点(或线)的单个组合,以便所有时间输入都能适合绘制。 然后,使用点或线将其

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

例如:

系列1 系列2 系列3
在没有任何进一步信息的情况下,您似乎必须使用以下组合:
xlim

使用
Plot
,通过
xlim
参数,绘制点(或线)的单个组合,以便所有时间输入都能适合绘制。
然后,使用
线
将其他数据添加到绘图中,并可能向这些函数传递
颜色
参数,以区分输出。

我们可以提供更多的细节,如果你包括一个最小的可复制的例子

对于基本图形,您可以结合使用
绘图
线

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

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

尝试以下操作:

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

t1您可以添加一些示例数据(真实的或虚构的)以便我们知道您拥有什么类型的时间/日期数据吗?这样更好,如果您改用
dput(数据)
,数据就在您拥有series1、series2和series3Rguy的任何地方,您可以扩展一下吗?dput如何改进我的问题?@william-dput将让其他人复制并粘贴数据的确切结构到我们的R会话中。这通常是最简单、最有效的数据通信方式。它确保数据类型与您的R会话一致,并消除了您的R会话与其他会话之间几乎所有可能的差异。好的,这是有意义的。所以对于长数据集,只需显示前几行就行了?这很管用&比插值好得多。“至少我现在知道,我没有忽略任何明显的更简单的方法。”蔡斯在上面给出了一个更彻底的答案,他的回答很好!您缺少函数melt来自的库(重塑)。谢谢@patrickT,已修复。这个答案来自于调用ggplot2时加载重塑的日子……这种情况不再发生。
1.208   3.780
2.478   6.890
3.823   9.091
5.125   12.769
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"))
library(ggplot2)
library(reshape)
plotdata <- melt(list(dat1 = dat1, dat2 = dat2, dat3 = dat3), "Time")

qplot(Time, value, data = plotdata, colour = L1)
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")