[R] plot error -- figure margins too large

Ping-Hsun Hsieh hsiehp at ohsu.edu
Tue Sep 29 01:39:51 CEST 2009


Hi,

I am trying to plot my dataset, consisting of one column with numeric values and one column with group IDs.
The set is similar to the following df.

df <- NULL
for ( i in 1:20)
{
  tmp1 <- runif(1000,0,5)
  tmp2 <- cbind(tmp1,i)
  df <- rbind(df,tmp2)
}


Now I would like to plot the numeric column, stratified by the group IDs, in a single figure.
First, I partitioned the frame into a 1x20 array. Then I plot the numeric values of each group in a sub-plot.
Note that only should the left most y-axis be shown, and the x-axis is only the group numbers.
Also the margin between two sub-frame should be fairly small so it looks like only one figure, rather than 20 figures.

The following codes were used to achieve my goal, but I got an error saying figure margins too large.

cols <- sample(rainbow(20))
par(mfrow=c(1,20))
for ( i in 1:20)
{
  tmp <- subset(df, df[,2]==i)
  if(i!=1)
  {
    plot(df[,1],axes=F,col=cols[i],frame.plot=F,xlab=NA,pch=19,ylim=c(0,5))
    mtext(i, line=1,side=1)
  }
  else{
    plot(df[,1],axes=F,col=cols[i],frame.plot=F,xlab=NA,pch=19,ylim=c(0,5))
    axis(2,at=0:5)
    mtext(i, line=1,side=1)
    }
}

I also tried to set the margins by modifying the _par_ command: par(mfrow=c(1,20),mar=(4,2,4,0.05))
Still, received the same error.


The other experiment:
While I was using the following codes, it almost showed what I would like, but the figure ended with only the last sub-plot. The only difference between the two is I was attempting to add a main-label in the figure.

cols <- sample(rainbow(20))
par(mfrow=c(1,20),mar=c(4,0.05,3,0.05))
plot.new()
mtext("Multiple figures in a plot",line=1,at=13)
for ( i in 1:20)
{
  tmp <- subset(df, df[,2]==i)
  if(i!=1)
  {
    plot(df[,1],axes=F,col=cols[i],frame.plot=F,xlab=NA,pch=19,ylim=c(0,5))
    mtext(i, line=1,side=1)
  }
  else{
    plot(df[,1],axes=F,col=cols[i],frame.plot=F,xlab=NA,pch=19,mar=c(4,4,3,0.05),ylim=c(0,5))
    axis(2,at=0:5)
    mtext(i, line=1,side=1)
    }
}


Any suggestions and advices are welcome.

Thanks,
Mike



More information about the R-help mailing list