[R] plot error -- figure margins too large

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Sep 29 10:35:53 CEST 2009



Ping-Hsun Hsieh wrote:
> 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 error depens on the space of your device. If your device is larger, 
it might work. For a smaller device I'd try


df <- matrix(c(runif(20000,0,5), rep(1:20, each=1000)), ncol=2)
cols <- sample(rainbow(20))
par(mfrow=c(1,20), oma=c(5,5,4,1), mar=rep(0, 4))
# nor margins (mar) for a single figure, but outer margins (oma)
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), 
ylab="")
     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)
     }
}



or even better go with lattice that does the job for you.



> 
> 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.


To add a main label, use the code above and add:


title("Multiple figures in a plot", outer=TRUE)


Uwe Ligges



> 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
> 
> ______________________________________________
> 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