Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a common axis for three similar time/value graphs

I have three timestamped measurement series, taken over the same interval, but with different actual timestamps. I'd like to show these three trajectories in a combined plot, but because the x-axis (timestamps) is different in each case, I'm having some trouble. Is there a way to do this without picking an x-axis to use and interpolating the y-values for the other two measurement series? I'm fairly new to R, but I feel like there's something obvious I'm overlooking.

For example:

Series 1

Time    Value
1.023   5.786
2.564   10.675
3.678   14.678
5.023   17.456

Series 2

0.787   1.765
1.567   3.456
3.011   5.879
4.598   7.768

Series 3

1.208   3.780
2.478   6.890
3.823   9.091
5.125   12.769
like image 920
William Gunn Avatar asked Dec 09 '25 20:12

William Gunn


1 Answers

With base graphics, you can use a combination of plot and points or 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"))

Take a look at ?legend to add a legend. Or, learn ggplot2 and let it handle that part of it for you:

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

qplot(Time, value, data = plotdata, colour = L1)
like image 73
Chase Avatar answered Dec 11 '25 10:12

Chase



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!