164

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?

lmo
  • 37,904
  • 9
  • 56
  • 69
user3530361
  • 1,651
  • 2
  • 11
  • 5
  • 3
    possible duplicate of [Error in plot.new() : figure margins too large in R](http://stackoverflow.com/questions/12766166/error-in-plot-new-figure-margins-too-large-in-r) – Calimo Feb 06 '15 at 08: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. – Roman Luštrik Feb 06 '15 at 09: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: https://stackoverflow.com/a/17074060/6018688 – fabianegli Oct 17 '19 at 15:41

8 Answers8

235

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.

djhurio
  • 5,437
  • 4
  • 27
  • 48
Guest R
  • 2,407
  • 1
  • 10
  • 2
  • 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? – Herman Toothrot May 27 '16 at 00: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 Jul 28 '16 at 00: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. Dec 26 '16 at 18:00
  • You are getting this error because of an RStudio UI layout issue, not something wrong with the code. The second answer fixed it for me. – Nicole Sullivan Mar 08 '18 at 20:00
  • 1
    @Nicole Sullivan I got this error also without RStudio. I did as described and it works. Thanks @djhurio! – Gwang-Jin Kim Apr 09 '19 at 15:37
  • This solution did not work for me. The next solution below (dev.off()) did work for me. – Joseph Kreke Oct 26 '19 at 20:38
  • 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. – young_souvlaki Mar 16 '21 at 01:02
156

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

Nicole Sullivan
  • 783
  • 6
  • 7
Csislander
  • 2,100
  • 1
  • 13
  • 16
41

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

PGreen
  • 3,239
  • 3
  • 24
  • 29
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()
Prakhar Agarwal
  • 2,724
  • 28
  • 31
17

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

Shravya Mutyapu
  • 278
  • 2
  • 9
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()
Guannan Shen
  • 649
  • 8
  • 12
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

Kiki Yang
  • 81
  • 1
  • 4
-1

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.

Ali Safari
  • 1,535
  • 10
  • 19
  • 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" – Josiah Yoder Aug 10 '21 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 Sep 15 '21 at 13:17
  • It worked for me, Nov 24th, 2021 – Yifangt Nov 24 '21 at 23:09