Tim
Tim

Reputation: 99408

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

In R, I met a running error as follows:

> png("p3_sa_para.png", 4, 2)
> par(mfrow=c(1,2))
> plot(c(1:10), ylab="Beta",xlab="Iteration")
Error in plot.new() : figure margins too large
> plot(c(1:10), ylab="Gamma",xlab="Iteration")
Error in plot.new() : figure margins too large
> dev.off()
X11cairo 
       2 

I have already made the image size small to be 4 by 2, why it still complains "figure margins too large"? How can I solve this problem with png?

It is strange that if I change png to pdf, then it will work. I also wonder why?

Thanks and regards!

Upvotes: 19

Views: 48468

Answers (4)

Mehrad
Mehrad

Reputation: 3829

This is a common issue for plotting specially when you are using IDE which has a place for generating and showing you the plot, thought it's a general issue and there is a logic behind it: when you tell R to plot something, R first look at the data and then looks at the area it has at it's disposal so that it cal do the plotting.

The png() and similar commands:

In your case you gave the plot a 4 by 2 pixel area to plot it, so you can solve it by increasing the area in a size that can fit your plot. (as Dirk Eddelbuettel mentioned)

In case of IDE

This is much simpler in most cases, just increase the plotting area by dragging the margins and then re-run your code (close any par() if you have any opened before and create new one) enter image description here

Upvotes: 3

user2781881
user2781881

Reputation: 51

Even I was getting the error on R-Studio, while the plot was appearing fine on the console. A simple restart of RStudio solved the problem! Having said that, RStudio's support page suggests that resetting graphics device dev.off() may help. http://support.rstudio.org/help/kb/troubleshooting/problem-with-plots-or-graphics-device

Upvotes: 4

RMurphy
RMurphy

Reputation: 303

The problem can simply arise from using a certain IDE. I was using Rstudio, and I got a slew of errors. My exact same code worked fine in the console.

Upvotes: 7

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

The png() function uses pixels not inches, so try something like

png("p3_sa_para.png", 640, 480)

And to answer your second question, yes, pdf() uses inches because a vector-graphics format has no notion of pixels. The help(png) and help(pdf) functions are your friends.

Upvotes: 28

Related Questions