[R] make three plot to one plot

Marc Schwartz (via MN) mschwartz at mn.rr.com
Fri Oct 21 22:02:32 CEST 2005


On Fri, 2005-10-21 at 21:13 +0200, Jan Sabee wrote:
> Dear all,
> I want to make three plot below to only one plot together with legend,
> how can I do that?
> I have tried with matplot function but I did not succeed.
> Thanks for your help.
> Sincerelly,
> Jan Sabee
> 
> 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)
> plot(test.five.x, test.five.y, type="l",lty=1, lwd=3, col='red')
> legend(par('usr')[2], par('usr')[4], xjust=1,
>       c('five'),
>       lwd=3,
>       lty=1,
>       col=c('red'))
> 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)
> plot(test.six.x, test.six.y, type="l",lty=2, lwd=3, col='green')
> legend(par('usr')[2], par('usr')[4], xjust=1,
>       c('six'),
>       lwd=3,
>       lty=2,
>       col=c('green'))
> 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)
> plot(test.seven.x, test.seven.y, type="l",lty=3, lwd=2, col='blue')
> legend(par('usr')[2], par('usr')[4], xjust=1,
>       c('seven'),
>       lwd=3,
>       lty=3,
>       col=c('blue'))

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




More information about the R-help mailing list