R Errors Explained: R figure margins too large

Dealing with error messages is simply a part of programming. The better you understand them more likely you are to avoid them and the better equipped you will be to handle them when they do occur. The “figure margins too large” error is an interesting one because not all of its occurrences result from your program or data. This error message occurs when there is a problem with the margins while graphing data.

The circumstances of this error.

This error message has two different ways it can occur. One of them is a result of a margin change made in the code and the other the arrangement of your windows in R-Studio.

The plot window is too short.

Here is an example of this error under the situation where the plot window is too short. There is no reason from in the code why you should be getting this error message.

# R error example #1: figure margins too large
 > x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 > y= x^2
 > plot(x, y)
 Error in plot.new() : figure margins too large

If you run the three lines of this code without the top of the graphing window at the bottom of the screen, it will produce a standard graph of the squares of the first ten whole numbers.

The margins are messed up within the program’s code.

Here, the first line in the code messes up the margins for the graphing function, resulting in this error message when you use plot() and other graphing functions.

# R error example #2: figure margins too large 
> par(mar=c(0, 0, 10, 100))
 > x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 > y= x^2
 > plot(x, y)
 Error in plot.new() : figure margins too large

To messes up the margins, this code does not have to be right before the graphic command that is producing the error. It can even be from an earlier run of the code.

How to fix this error.

Fortunately, fixing this error is quite easy. It only requires some simple adjustments, either to your R-Studio plot window or your code.

The plot window is too short.

If this is your problem then just go into R-Studio and move the cursor to the top of the graph window until you get four-way arrows. Then simply drag the top the window upward to the variable list. If this was the source of your problem and running the code unchanged will produce the expected graph.

The margins are messed up within the program’s code.

If the problem was with your code then simply add “par(mar=c(1, 1, 1, 1))” to your code as illustrated below.

# R Error avoided: figure margins too large 
> x = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 > y= x^2
 > par(mar=c(1, 1, 1, 1))
 > plot(x, y)

This should fix the problem producing this error regardless of where the margin change occurs in your program. This solution fixes the problem by resetting the margins to acceptable values, eliminating the error.

Addresses R Error: figure margins too large