176

I've looked in different questions for a solution and I've tried what was suggested but I have not found a solution to make it work.

Everytime I want to run this code it always says:

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

and I don't know how to fix it. Here is my code:

par(mfcol=c(5,3))
hist(RtBio, main="Histograma de Bio Pappel")
boxplot(RtBio, main="Diagrama de Caja de Bio Pappel")
stem(RtBio)
plot(RtBio, main="Gráfica de Dispersión")

hist(RtAlsea, main="Histograma de Alsea")
boxplot(Alsea, main="Diagrama de caja de Alsea")
stem(RtAlsea)
plot(RtTelev, main="Gráfica de distribución de Alsea")

hist(RtTelev, main="Histograma de Televisa")
boxplot(telev, main="Diagrama de Caja de Televisa")
stem(Telev)
plot(Telev, main="Gráfica de dispersión de Televisa")

hist(RtWalmex, main="Histograma de Walmex")
boxplot(RtWalmex, main="Diagrama de caja de Walmex")
stem(RtWalmex)
plot(RtWalmex, main="Gráfica de dispersión de Walmex")

hist(RtIca, main="Histograma de Ica")
boxplot(RtIca, main="Gráfica de caja de Ica")
stem(RtIca)
plot(RtIca, main="Gráfica de dispersión de Ica")

What can I do?

3
  • 3
    possible duplicate of Error in plot.new() : figure margins too large in R
    – Calimo
    Commented Feb 6, 2015 at 8:40
  • 4
    Margins appear to be too large for your image. This can happen if you have a small plot window. In any case, your description is insufficient to diagnose the problem. We could use a reproducible example or screenshot of your R session with the plot window. Commented Feb 6, 2015 at 9:49
  • 2
    I my case, it helped to debug with a small subset of the data that was to be plotted like plot(df[1,1:3], df2[1,1:3]) - and then I realized that what I actually wanted to do is to plot(unlist(df[1,1:3]), unlist(df2[1,1:3])) Also see: stackoverflow.com/a/17074060/6018688
    – fabianegli
    Commented Oct 17, 2019 at 15:41

8 Answers 8

240

Every time you are creating plots you might get this error - "Error in plot.new() : figure margins too large". To avoid such errors you can first check par("mar") output. You should be getting:

[1] 5.1 4.1 4.1 2.1

To change that write:

par(mar=c(1,1,1,1))

This should rectify the error. Or else you can change the values accordingly.

Hope this works for you.

7
  • 8
    how do you exactly know which values are inside the margins? And why do you say that I should be getting [1] 5.1 4.1 4.1 2.1 but then you tell me to chage it to all 1's? Commented May 27, 2016 at 0:23
  • 6
    I ran into the same problem with RStudio, and when I entered par("mar") I retrieved the same exact string [1] 5.1 4.1 4.1 2.1 so I entered par(mar=c(1,1,1,1)) but then plot() would not plot anything, so I had to close close both RStudio and the terminal. After reopening RStudio, it was back to normal.
    – noobninja
    Commented Jul 28, 2016 at 0:09
  • 2
    Running into the same problem in R markdown in RStudio as well. Neither Guest R's solution or @noobninja restart fixed it for me though.
    – SC.
    Commented Dec 26, 2016 at 18:00
  • 1
    @Nicole Sullivan I got this error also without RStudio. I did as described and it works. Thanks @djhurio! Commented Apr 9, 2019 at 15:37
  • 2
    This worked for me with par(mar=c(2,2,2,2)). To answer @HermanToothrot, It seems those are the default values, which is how he knows what you should get, and then he decreases them all to 1.
    – mcp
    Commented Mar 16, 2021 at 1:02
158

This can happen when your plot panel in RStudio is too small for the margins of the plot you are trying to create. Try making expanding it and then run your code again.

RStudio UI causes an error when the plot panel is too small to display the chart: RStudio with the plot panel too small

Simply expanding the plot panel fixes the bug and displays the chart: RStudio with the plot panel expanded

5
  • 8
    It indeed works.. simply expanding the plot area helps
    – JP Zhang
    Commented Mar 10, 2017 at 11:34
  • 5
    Yes, resizing the panels in RStudio works. It is an RStudio bug caused when you minimize the right side of the UI by sliding the plot panel shut. Commented Mar 8, 2018 at 19:59
  • this actually works in the majority of cases. there is a small minority of cases where the margins are indeed so small that even if you maximize this window you have no solution to this problem Commented Nov 1, 2019 at 11:20
  • I first tried this, to no avail. Then I tried to set par(mar=c(1,1,1,1)), as suggested in another answer, and it worked. No idea why. :(
    – bli
    Commented Apr 11, 2021 at 9:38
  • 1
    This should have been answer number one Commented Jul 27, 2022 at 23:59
40

Invoking dev.off() to make RStudio open up a new graphics device with default settings worked for me. HTH.

4
  • 1
    Could you please explain how to do that? Commented Jan 3, 2020 at 17:30
  • 2
    Type dev.off() in the R console, hit Enter. Commented Sep 30, 2020 at 1:14
  • I ran into that error with an @example when building a package. Adding dev.off() in the example solved the problem. Thx. :-)
    – qasta
    Commented Dec 2, 2020 at 7:06
  • But if your are making more than one figure, try to add this as well: par(mar=rep(2, 4)) Commented May 25, 2021 at 4:44
20

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

Moreover Execute the command

graphics.off()
1
  • 12
    write this three lines graphics.off() par("mar") par(mar=c(1,1,1,1))
    – Hiren
    Commented Nov 29, 2016 at 0:07
19

Just clear the plots and try executing the code again...It worked for me

0
14

Just a side-note. Sometimes this "margin" error occurs because you want to save a high-resolution figure (eg. dpi = 300 or res = 300) in R.
In this case, what you need to do is to specify the width and height. (Btw, ggsave() doesn't require this.)

This causes the margin error:

# eg. for tiff()
par(mar=c(1,1,1,1))
tiff(filename =  "qq.tiff",
     res = 300,                                                 # the margin error.
     compression = c( "lzw") )
# qq plot for genome wide association study (just an example)
qqman::qq(df$rawp, main = "Q-Q plot of GWAS p-values", cex = .3)
dev.off()

This will fix the margin error:

# eg. for tiff()
par(mar=c(1,1,1,1))
tiff(filename =  "qq.tiff",
     res = 300,                                                 # the margin error.
     width = 5, height = 4, units = 'in',                       # fixed
     compression = c( "lzw") )
# qq plot for genome wide association study (just an example)
qqman::qq(df$rawp, main = "Q-Q plot of GWAS p-values", cex = .3)
dev.off()
2

Try to set margin size by mai=c() in par(), e.g.,

par(mfcol=c(5,3),mai=c(0.5,0.5,0.5,0))

See the documentation for more details about mai

enter image description here

0

Just run graphics.off() before plotting your data. This instruction solved my error. So, it's harmless to try it before taking a more complex solution.

2
  • When run from PyCharm with the JetBrains R plugin, this resulted in some sort of infinite recursion and stack overflow: "Error: C stack usage 15924416 is too close to the limit" Commented Aug 10, 2021 at 20:54
  • I understand it might not work for all cases. But as I mentioned, before taking any other complex action, it won't be harming you if you try this simple instruction. It worked for my case, and might work for someone else as well.
    – Ali Safari
    Commented Sep 15, 2021 at 13:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.