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 “error in plot.new() : 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.
When Does This Error Occur?
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 plot.new error in r 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 when plotting your data. Your plot margins should be correct, however your r plot size just is not working.
# R error example #1: figure margins too large # error in plot new 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 on the plot. However, here we are seeing the same error, “error in plot.new() : figure margins too large.”
The Plot Area 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 to make a boxplot, histogram, or other plot function.
# 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 mess up the margins, this code does not have to be right before the graphic command that is producing the error value. It can even be from an earlier run of the code. Your R margins can be a tricky thing to work with, so knowing how to change plot dimensions, axes, and your plotting region in R is an invaluable skill for any programmer.
How to Fix This Error.
How do you fix too big margins in R? Fortunately, the solution to this error is quite easy. It only requires some simple adjustments, either to your R-Studio plot window or your code.
If 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 plot region upward to the variable list. If this was the source of your figure region problem, running the code unchanged will produce the expected graph. Your outer default margins should be the correct size and your R plot area labels should not be cut off.
If The Plot Margins Are Messed Up In The Code…
If the problem was with the plotting area in 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. What is par mar in R? This par function solution fixes the problem by resetting the plotting area margins to acceptable values, eliminating the error. Your default margins should be fixed, and your R plots should now be nice and readable.
Addresses R Error: figure margins too large
Having Trouble? Check Out These Fixes For Other R Errors:
- error in cov.wt(z): “x” must contain finite values only
- R error message: argument is of length zero
- R error message: not meaningful for factors in r
- Incorrect Number of Dimensions