Error in plot.new() : figure margins too large, Scatter plot
Asked Answered
C

8

173

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?

Castellan answered 14/4, 2014 at 2:14 Comment(3)
possible duplicate of Error in plot.new() : figure margins too large in RLocation
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.Duval
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://mcmap.net/q/144662/-error-in-plot-new-figure-margins-too-large-in-rCitation
C
237

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.

Checkerbloom answered 27/9, 2014 at 11:34 Comment(7)
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?Ionia
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.Detta
Running into the same problem in R markdown in RStudio as well. Neither Guest R's solution or @Detta restart fixed it for me though.Padrone
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.Glossotomy
@Nicole Sullivan I got this error also without RStudio. I did as described and it works. Thanks @djhurio!Syblesybley
This solution did not work for me. The next solution below (dev.off()) did work for me.Hygrophilous
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.Cerberus
C
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

Christianechristiania answered 14/4, 2014 at 2:22 Comment(5)
It indeed works.. simply expanding the plot area helpsNarcolepsy
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.Glossotomy
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 problemRatable
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. :(Rubbery
This should have been answer number oneLanguage
C
40

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

Crumble answered 6/10, 2014 at 12:49 Comment(4)
Could you please explain how to do that?Wimsatt
Type dev.off() in the R console, hit Enter.Adorn
I ran into that error with an @example when building a package. Adding dev.off() in the example solved the problem. Thx. :-)Instantly
But if your are making more than one figure, try to add this as well: par(mar=rep(2, 4))Emetine
A
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()
Arrowwood answered 17/6, 2016 at 13:15 Comment(1)
write this three lines graphics.off() par("mar") par(mar=c(1,1,1,1))Aphasia
T
18

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

Tiptop answered 19/11, 2019 at 10:56 Comment(0)
G
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()
Grate answered 23/11, 2019 at 0:42 Comment(0)
N
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

Nabila answered 4/10, 2021 at 16:28 Comment(0)
W
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.

Wrapper answered 26/10, 2020 at 15:9 Comment(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"Cornaceous
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.Wrapper

© 2022 - 2024 — McMap. All rights reserved.