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

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)

This question is related to r plot

The answer is


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() 

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


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.


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!


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.


enter image description here

Just zoom this area if you use RStudio.


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.


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


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)
    ```

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


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


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.


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