Error in Plot.New():Figure Margins Too Large, Scatter Plot

Error in plot.new() : figure margins too large in R

The problem is that the small figure region 2 created by your layout() call is not sufficiently large enough to contain just the default margins, let alone a plot.

More generally, you get this error if the size of the plotting region on the device is not large enough to actually do any plotting. For the OP's case the issue was having too small a plotting device to contain all the subplots and their margins and leave a large enough plotting region to draw in.

RStudio users can encounter this error if the Plot tab is too small to leave enough room to contain the margins, plotting region etc. This is because the physical size of that pane is the size of the graphics device. These are not independent issues; the plot pane in RStudio is just another plotting device, like png(), pdf(), windows(), and X11().

Solutions include:

  1. reducing the size of the margins; this might help especially if you are trying, as in the case of the OP, to draw several plots on the same device.

  2. increasing the physical dimensions of the device, either in the call to the device (e.g. png(), pdf(), etc) or by resizing the window / pane containing the device

  3. reducing the size of text on the plot as that can control the size of margins etc.

Reduce the size of the margins

Before the line causing the problem try:

par(mar = rep(2, 4))

then plot the second image

image(as.matrix(leg),col=cx,axes=T)

You'll need to play around with the size of the margins on the par() call I show to get this right.

Increase the size of the device

You may also need to increase the size of the actual device onto which you are plotting.

A final tip, save the par() defaults before changing them, so change your existing par() call to:

op <- par(oma=c(5,7,1,1))

then at the end of plotting do

par(op)

How to solve 'Error in plot.new() : figure margins too large' in rstudio?

The only way I can possibly imagine doing this is (as I think has previously been suggested) sending the output to a PDF file and then using a PDF viewer to look at it. (My answer is very similar to the answer you got to this question, except that I'm using PDF rather than PNG as output ... tried it for PNG as well, seems to work for 2000x2000 pixels ...)

For smaller/reasonable examples, zooming and/or opening an external graphics window with dev.new() should work ...

a <- iris[,1:4]
a <- t(a)
a <- as.data.frame(a)
pdf(file="tmp.pdf",width=100,height=100)
pairs(a,gap=0,pch=".")
dev.off()

This took about 18 seconds on my laptop and produced a 1.2M PDF file. Here's what the picture looked like when I zoomed all the way out ("fit to window") in my PDF viewer:

Sample Image

If you have 150x150 subplots and want to render each one at 1 cm wide x 1 cm tall (which seems pretty small for viewing any detail), you're either going to need a very high-resolution projector or a large-format (poster) printer. You could scroll around the image with a PDF viewer, but it doesn't seem very practical ...

Histogram, error: Error in plot.new() : figure margins too large

You set the outer margins (outside the whole set of plots) but the inner margins (for each panel) are at the default. With the margins at the default, there is not enough room within each panel to plot the histogram and have the marginal information.

So you need to change the inner margins as well, and if you want anything to look reasonable then you should also change size and position of the things to be added.

Here is an example (I had to change to random data since your example was not reproducible):

par(mfcol=c(12,12), oma=c(1,1,0,0), mar=c(1,1,1,0), tcl=-0.1, mgp=c(0,0,0))

for(m in 1:141 ){
x <- rnorm(100)
hist(x[x != 0],30, xlab=NA, ylab=NA, main=paste('data: ',m),
cex.axis=0.5, font.main=1, cex.main=0.8)
}


Related Topics



Leave a reply



Submit