[R] Graphics - plotting two graphs

John Kane jrkrideau at yahoo.ca
Wed Oct 24 18:05:36 CEST 2007


It is not at all clear what you are trying to do.
However does this do something like what you want?  I
just ignored the second y-axis as it is redundent. 
I have not been able to figure out what all the par
calls were intended to do.

x1 <- c(2, 4, 6, 8, 10, 12)
x2 <- c(10, 20, 30, 40, 50, 60)
y1 <- c(10,12,15,22,34,21)
y2 <- c(40, 130, 150, 145, 40, 30)

plot(x1,y1, pch=16, cex=2, lty=1, xlab="", ylab="",
          ylim=c(0,170), type="b")
mtext("k", side=1, line=2, cex=0.8)
mtext("GO terms", side=2, line=2, las=0, cex=0.8)
mtext("Num clusters", side=3, line=2, las=0, cex=0.8)
mtext("GO terms", side=4, line=2, las=0, cex=0.8)

par(new=TRUE)
plot(x2,y2, pch=21, cex=2, lty=2, axes=FALSE, xlab="",
ylab="", type="b" ,
          ylim=c(0,170))
axis(3, at=seq(0, 70, 10))


Multiple scales like this are not a great idea You are
almost certainly better off with two graphs.

# Alternative approach

op <- par(mfcol=c(1,2),oma=c(1,0,2,0))
plot(x1,y1, pch=16, cex=2, lty=1, xlab="K", ylab="GO
Terms",
          ylim=c(0,170), type="b")
plot(x2,y2, pch=21, cex=2, lty=2, xlab="Num clusters",
ylab="", type="b" ,
          ylim=c(0,170))
mtitle("My Results")
par(op) 


--- Tim Smith <tim_smith_666 at yahoo.com> wrote:

> Hi,
> 
> I wanted to plot 2 lines on a single graph. Each
> graph has one axis that can be common. The code that
> I'm using is:
> 
>
-------------------------------------------------------
> 
>   par(mfrow=c(1, 1))
> x1 <- c(2, 4, 6, 8, 10, 12)
> x2 <- c(10, 20, 30, 40, 50, 60)
> y1 <- c(10,12,15,22,34,21)
> y2 <- c(40, 130, 150, 145, 40, 30)
> par(las=1, mar=c(4, 4, 2, 4))
> plot.new()
> #plot.window(range(x1), c(0, 12))
> plot.window(range(x2), c(0, 170))
> plot.window
> lines(x1, y1)
> lines(x2, y2)
> points(x1, y1, pch=16, cex=2)
> points(x2, y2, pch=21, bg="white", cex=2)  
> par(col="grey50", fg="grey50", col.axis="grey50")   
> axis(1, at=seq(0, 16, 4))
> axis(2, at=seq(0, 170, 10))
> axis(3, at=seq(0, 70, 10))
> axis(4, at=seq(0, 170, 10))
> box(bty="u") 
> mtext("k", side=1, line=2, cex=0.8)
> mtext("GO terms", side=2, line=2, las=0, cex=0.8)
> mtext("Num clusters", side=3, line=2, las=0,
> cex=0.8)
> mtext("GO terms", side=4, line=2, las=0, cex=0.8)
> par(mar=c(5.1, 4.1, 4.1, 2.1), col="black",
> fg="black", col.axis="black")
> 
> ---------------------------------------------------
> 
> One of the lines comes out alright, but the other
> doesn't ( one x axis doesn't come out right either).
>  Is there any way I can stretch one of the x axis so
> that it shows the range 0-16 spread over the entire
> axis? Hopefully, that should take care of the line
> problem too.
> 
> Any help would be appreciated.
> 
> thanks!
> 
>  __________________________________________________
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained,
> reproducible code.
>



More information about the R-help mailing list