19

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!

smci
  • 32,567
  • 20
  • 113
  • 146
Tim
  • 1
  • 141
  • 372
  • 590
  • 6
    you would save yourself a lot of hassle if you read the help for the R functions you are using. What `width` and `height` are on a `png` device is clearly stated in `?png` – Gavin Simpson Dec 10 '10 at 15:54
  • 5
    @gavin I disagree, I encountered exact problem and the first thing I was looking for is help for plot and then help for par, and after all useless information, I went for png last. Help is good only when you have idea where you should looking at from. – Tg. Jul 28 '11 at 04:08
  • 2
    @Tg the logical thing to do in a situation where you get an error is to break it down. If the plot works without `png()` we know the problem lies with `png()`. If removing `png()` makes no difference then we've narrowed the scope of the problem down. There shouldn't be any real need for flailing around reading multiple help files etc for things like this, *if* you adopt a logical approach to working through the problem, isolate where the problem is and then read the relevant help file. Are you disagreeing about the need to read the help or something else? – Gavin Simpson Jul 28 '11 at 07:40
  • 9
    @gavin I'm not saying that people shouldn't read a help, but I said that you should't assume that he didn't read any help. – Tg. Aug 18 '11 at 11:48

4 Answers4

28

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.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • +1, but you can also specify the units argument if desired, e.g. for 2 by 4 inches try `png("p3_sa_para.png", 4, 2, units="in")` – knrumsey Oct 11 '22 at 18:52
7

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.

Perception
  • 79,279
  • 19
  • 185
  • 195
RMurphy
  • 293
  • 6
  • 12
4

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

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
3

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

Community
  • 1
  • 1
Mehrad Mahmoudian
  • 3,466
  • 32
  • 36