[R] two curves at one graph

Jim Lemon jim at bitwrit.com.au
Mon May 19 11:21:38 CEST 2008


hanen wrote:
> i try to use par(new=TRUE) i get them at the same graph but the y_axis and
> x_axis are drowen with two unevenly graduations that graph become
> unreadable.
Hi Hanen,
If you want to plot two sets of values that fit into the same range, 
plot the one with the larger range first, then use the "points" (or 
"lines") function to plot the other set. For example:

x1<-c(1.2,2.3,3.4,4.5)
# this one has a larger range
x2<-c(0.3,2.4,4.6,6.8)
# plot the one with the wider range
plot(x2,col="red")
# then the one that fits within the first range
points(x1,col="green")

If the ranges of the values overlap, but neither range fits in the other:

x1<-c(4.5,7.9,9.1,9.9)
x2<-c(0.3,2.4,4.6,6.8)
# use ylim to leave enough room on the first plot
plot(x2,ylim=range(c(x1,x2)),col="red")
points(x1,col="green")

If the ranges are really different, you can try gap.plot or twoord.plot 
in the plotrix package:

x1<-c(1.2,2.3,3.4,4.5)
x2<-c(20.3,22.4,34.6,46.8)
gap.plot(c(x1,x2),col=c(rep("green",4),rep("red",4)),gap=c(6,18))
# OR
twoord.plot(x1,x2)

In the above example, gap.plot is probably better for this data.

Jim



More information about the R-help mailing list