Skip to content
Prev 79485 / 398503 Next

make three plot to one plot

On Fri, 2005-10-21 at 21:13 +0200, Jan Sabee wrote:
Jan,

matplot() won't work well here, because you do not have common x axis
values across the three sets of data. Thus, you need to use plot() and
then lines().

Try this:

# First set up your data

test.five.x <- c(0.02,0.05,0.07,0.09,0.10,0.12,
                 0.13,0.14,0.16,0.17,0.20,0.21,
                 0.34,0.40)
test.five.y <- c(18,12,17,12,3,15,1,5,1,1,3,10,
                 15,10)

test.six.x <- c(0.03,0.05,0.07,0.08,0.09,0.10,
                0.12,0.13,0.14,0.15,0.17,0.18,
                0.21,0.22,0.24,0.33,0.39,0.43)
test.six.y <- c(8,32,14,21,3,8,11,14,11,21,16,
                14,10,21,1,2,13,19)

test.seven.x <- c(0.04,0.05,0.08,0.09,0.10,0.12,
                  0.13,0.14,0.16,0.17,0.19,0.21,
                  0.22,0.24,0.29,0.32,0.38,0.40,
                  0.46,0.50)
test.seven.y <- c(18,135,240,42,63,128,215,267,
                  127,36,21,23,223,12,66,96,12,
                  26,64,159)


# Now use plot to draw the first set of data
# The key here is to properly set to the x and y axis
# ranges ('xlim' and 'ylim') so that they are based
# upon all three sets of data.
# Note that I also set the x and y axis labels to "". You
# can adjust these and the plot title as you require.

plot(test.five.x, test.five.y, type="l",lty=1, lwd=3, col="red",
     xlim = range(test.five.x, test.six.x, test.seven.x),
     ylim = range(test.five.y, test.six.y, test.seven.y),
     xlab = "", ylab = "")


# Now use lines() to add the second and third sets of data

lines(test.six.x, test.six.y, lty=2, lwd=3, col="green")
lines(test.seven.x, test.seven.y, lty=3, lwd=2, col="blue")


# Now use legend(). Place it at the upper right hand corner
# and set the line types and colors to reflect the above

legend("topright", xjust = 1,
       legend = c("five", "six", "seven"),
       lwd = 3, lty = 1:3,
       col = c("red", "green", "blue"))


See ?lines for more information.

HTH,

Marc Schwartz