[R] subplot strange behavoir

Sarah Goslee sarah.goslee at gmail.com
Wed Oct 5 22:06:32 CEST 2011


Hi,

I'm assuming you're using subplot() from Hmisc, but it's a good idea
to specify.

It's not subplot() that's causing the problem, it's layout, or rather the
interaction between the two.

This section run at the command line doesn't work:
layout(matrix(c(1,2),2,2,byrow=TRUE),c(1.5,2.5),respect=TRUE)
plot(seq(1:10),seq(1:10),type="l",las=1,col="blue")
plot(x,y,type="l",las=1,ylim=c(0,12))
subplot(edm.sub(x[seq(1:5)],y[seq(1:5)]),x=4,y=9,size=c(1,1.5))
subplot(edm.sub(x[seq(15,20,by=1)],y[seq(15,20,by=1)]),x=17,y=9,size=c(1,1.5))

You appear to have run afoul of two things:
from ?subplot
     Any graphical parameter settings that you would like to be in
     place before ‘fun’ is evaluated can be specified in the ‘pars’
     argument (warning: specifying layout parameters here (‘plt’,
     ‘mfrow’, etc.) may cause unexpected results).

and from ?layout
     These functions are totally incompatible with the other mechanisms
     for arranging plots on a device: ‘par(mfrow)’, ‘par(mfcol)’ and
     ‘split.screen’.

And also apparently subplot().

You could try asking the package maintainer, but I think you may be
better off making two separate figures. Or you could delve into the
mysteries of par(), of course.

Incidentally, you can't use png() to make a tif, no matter what you call
it. You probably want tiff() instead.

Sarah

On Wed, Oct 5, 2011 at 3:40 PM, emorway <emorway at usgs.gov> wrote:
> Hello,
>
> Below is some example code that should reproduce an error I'm encountering
> while trying to create a tiff plot with two subplots.  If I run just the
> following bit of code through the R GUI the result is what I'd like to have
> appear in the saved tiff image:
>
> x<-seq(0:20)
> y<-c(1,1,2,2,3,4,5,4,3,6,7,1,1,2,2,3,4,5,4,3,6)
> plot(x,y,type="l",las=1,ylim=c(0,12))
> subplot(edm.sub(x[seq(1:5)],y[seq(1:5)]),x=4,y=9,size=c(1,1.5))
> subplot(edm.sub(x[seq(15,20,by=1)],y[seq(15,20,by=1)]),x=17,y=9,size=c(1,1.5))
>
> However, if expanding on this code with:
>
> edm.sub<-function(x,y){plot(x,y,col="red",frame.plot=F,
>                       las=1,xaxs="i",yaxs="i",type="b",
>                       ylim=c(0,6),xlab="",ylab="")}
>
> png("c:/temp/lookat.tif",res=120,height=600,width=1200)
> layout(matrix(c(1,2),2,2,byrow=TRUE),c(1.5,2.5),respect=TRUE)
> plot(seq(1:10),seq(1:10),type="l",las=1,col="blue")
> plot(x,y,type="l",las=1,ylim=c(0,12))
> subplot(edm.sub(x[seq(1:5)],y[seq(1:5)]),x=4,y=9,size=c(1,1.5))
> subplot(edm.sub(x[seq(15,20,by=1)],y[seq(15,20,by=1)]),x=17,y=9,size=c(1,1.5))
> dev.off()
>
> One will notice the second subplot is out of position (notice the
> y-coordinate is the same for both subplots...y=9):
> http://r.789695.n4.nabble.com/file/n3875917/lookat.png
>
> If I try to 'guess' a new y-coordinate for the second subplot, say y=10:
>
> png("c:/temp/lookat.tif",res=120,height=600,width=1200)
> layout(matrix(c(1,2),2,2,byrow=TRUE),c(1.5,2.5),respect=TRUE)
> plot(seq(1:10),seq(1:10),type="l",las=1,col="blue")
> plot(x,y,type="l",las=1,ylim=c(0,12))
> subplot(edm.sub(x[seq(1:5)],y[seq(1:5)]),x=4,y=9,size=c(1,1.5))
> subplot(edm.sub(x[seq(15,20,by=1)],y[seq(15,20,by=1)]),x=17,y=10,size=c(1,1.5))
> dev.off()
>
> R kicks back the following message
> Error in plot.new() : plot region too large
>
> Am I mis-using subplot?
>
> Thanks, Eric
>
-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list