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

15

129

I'm new to R but I've made numerous correlation plots with smaller data sets. However, when I try to plot a large dataset (2gb+), I can produce the plot just fine, but the legend doesn't show up. Any advice? or alternatives?

library(gplots)
r.cor <- cor(r)
layout(matrix(c(1,1,1,1,1,1,1,1,2,2), 5, 2, byrow = TRUE))
par(oma=c(5,7,1,1))
cx <- rev(colorpanel(25,"yellow","black","blue"))
leg <- seq(min(r.cor,na.rm=T),max(r.cor,na.rm=T),length=10)
image(r.cor,main="Correlation plot Normal/Tumor data",axes=F,col=cx)
axis(1, at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]], 
    cex.axis=0.9,las=2)
axis(2,at=seq(0,1,length=ncol(r.cor)), labels=dimnames(r.cor)[[2]],
     cex.axis=0.9,las=2)
image(as.matrix(leg),col=cx,axes=T)     

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

tmp <- round(leg,2)
axis(1,at=seq(0,1,length=length(leg)), labels=tmp,cex.axis=1)
Hemichordate answered 7/10, 2012 at 4:37 Comment(2)
You should provide us with a reproducible example demonstrating ills you're having. #12766168Emendate
I tried all of the above, and nothing worked. However, once in a while (at least for a newbie like me), the data in a matrix or data.frame can have been coerced into some type you weren't aware of. In that case, use "as.numeric" before your data to make sure this isn't the problem.Matelote
T
94

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)
Thierry answered 7/10, 2012 at 8:38 Comment(2)
Ah, thank you for the clarification. I was manipulating layout(matrix()) instead. Appreciate the help!Hemichordate
this was the right hint for me. I had to increase the image size, or decrease the resolution in png(filename="myfile.png", res=150, width = 1000, height = 1000)Greensand
V
168

This error can occur in Rstudio simply because your "Plots" pane is just barely too small. Try zooming your "Files, Plots, Packages, Help, Viewer" and see if it helps!

Vasos answered 5/9, 2014 at 18:46 Comment(5)
This solved my problem! I had expanded the "Environment" window, shrinking the "Plots", etc. window. I just had to expand the window. Thank you!Martz
Agreed, this affected my RStudio as well, and just expanding the window helped.Kenyettakenyon
Sometimes I accidentally end up with a number of panes due to using par(). par(mfrow=c(1,1)) can reset you to one pane.Ashcraft
it was very weird error for me since I am new to R. Never had any issues before with any other languages/IDE where IDE layout will impact my code!!Rondure
For me, the reason was that the x-axis names (written vertically) were too long. Enlarging the plot window helped but the error was caused by too little space for the name.Kissie
T
94

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)
Thierry answered 7/10, 2012 at 8:38 Comment(2)
Ah, thank you for the clarification. I was manipulating layout(matrix()) instead. Appreciate the help!Hemichordate
this was the right hint for me. I had to increase the image size, or decrease the resolution in png(filename="myfile.png", res=150, width = 1000, height = 1000)Greensand
S
75

If you get this message in RStudio, clicking the 'broomstick' figure "Clear All Plots" in Plots tab and trying plot() again may work.

enter image description here

Subtemperate answered 26/8, 2015 at 0:28 Comment(4)
This is the best answer.Bemire
graphics.off()Jeconiah
I like this answerCollateral
This is really the best answer. Thanks.Neysa
C
28

This sometimes happen in RStudio. In order to solve it you can attempt to plot to an external window (Windows-only):

windows() ## create window to plot your file
## ... your plotting code here ...
dev.off() 
Counterspy answered 18/3, 2016 at 17:55 Comment(5)
This is a better answer than buying a bigger monitor. There is also an x11() command that should work on Linux.Hensley
The most appropriate answer ever. Thanks.Holiday
any equivalent for MacOSX ?Naquin
I tried this solution when I get Error in plot.new() : figure margins too large error in RStudio when drawing OLS-CUSUM, and it worked miraculously. Many thanks jobligado.Lully
For me, dev.off() closed the window, so if you'd like to see it in addition to plotting it, I would skip that lineUncouth
W
22

I got this error in R Studio, and was simply fixed by making the sidebar bigger by clicking and dragging on its edge from right to left.

Picture here: https://janac.medium.com/error-in-plot-new-figure-margins-too-large-in-r-214621b4b2af

Weaner answered 25/11, 2015 at 17:55 Comment(4)
this was the winner. Why is this even a thing?Rudiment
None of the other solutions worked for me except this one.Keneth
Don't know how or why, but this also was the only solution that worked for me.Relax
If this answer helped you, feel free to clap for this article: janac.medium.com/…Weaner
C
10

Check if your object is a list or a vector. To do this, type is.list(yourobject). If this is true, try renaming it x<-unlist(yourobject). This will make it into a vector you can plot.

Clone answered 12/6, 2013 at 19:50 Comment(1)
This did it for me (using png() / dev.off() in Rstudio).Persaud
S
5

enter image description here

Just zoom this area if you use RStudio.

Sarrusophone answered 27/4, 2016 at 17:42 Comment(0)
R
4

I found this error today. Initially, I was trying to output it to a .jpeg file with low width and height.

jpeg("method1_test.jpg", width=900, height=900, res=40)

Later I increased the width and height to:

jpeg("method1_test.jpg", width=1900, height=1900, res=40)

The error was not there. :)

You can also play with the resolution, if the resolution is high, you need more width and height.

Redundant answered 16/9, 2016 at 10:41 Comment(0)
H
2

I had this error when I was trying to plot high dimensional data. If that's what is going on with you, try multidimensional scaling: http://www.statmethods.net/advstats/mds.html

Hispidulous answered 20/6, 2013 at 17:34 Comment(0)
S
2

I struggled with this error for weeks (using RStudio). I tried moving the plot window bigger and smaller, but that did not consistently help. When I moved (dragged) the application to my bigger monitor, the problem disappeared! I was stunned... so many wasted hours... I knew my code was correct...

Showman answered 1/5, 2016 at 21:32 Comment(0)
S
1

If margin is low, then it is always better to start with new plotting device:

dev.new()
# plot()
# save your plot
dev.off()

You will never get margin error, unless you plot something large which can not be accommodated.

Sikorski answered 11/3, 2020 at 21:20 Comment(0)
F
0

RStudio Plots canvas is limiting the plot width and heights. However if you make your plot from Rmarkdown code chunk, it works without canvas field limitation because plotting area set according to the paper size.

For instance:

    ```{r}
#inside of code chunk in Rmarkdown
        grid <- par(mfrow=c(4, 5))
        plot(faithful, main="Faithful eruptions")
        plot(large.islands, main="Islands", ylab="Area")
        ...
        par(grid)
    ```
Fushih answered 29/1, 2016 at 4:15 Comment(0)
H
0

I found the same error today. I have tried the "Clear all Plots" button, but it was giving me the same error. Then this trick worked for me, Try to increase the plot area by dragging. It will help you for sure.

Hypermeter answered 11/2, 2019 at 13:32 Comment(0)
T
0

I have just use the Clear all plots then again give the plot command and it was helpfull

Throttle answered 22/7, 2019 at 7:19 Comment(1)
Welcome to SO. Please can you explain why this is the answer.Eboni
N
0

I reproduced the error when: par(mfrow = c(2, 1))

What worked for me was: par(mfrow = c(1, 2))

So basically play around with par(mfrow = (x1,x2)) to see what works best.

Nonmaterial answered 21/4 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.