The error in plot.new() : figure margins too large occur if the plot panel in the RStudio is too small for the margins you are trying to create.
In this tutorial, we will learn how to resolve error in plot.new() : figure margins too large issue using several ways
What is error in plot.new() : figure margins too large?
Let us create a simple plot to demonstrate this issue.
# Draw a simple scatter plot plot(1:40)
Error in plot.new() : figure margins too large
When we run the above code in our R Studio, we will be getting an error in plot.new() : figure margins too large.
If you observe the plot window in the screenshot, it is too small to plot the figure in the given space.
How to fix error in plot.new() : figure margins too large?
There are several ways to fix the issue. Let us look at each of these solutions in detail.
Solution 1 – Increase the plot panel size
The quickest and easiest way to resolve the issue is to increase the size of the plot panel in RStudio.
Since it is not a code issue and has something to do with the plot panel size, after increasing the panel size, you can run the code once again and see that issue is fixed.
Solution 2 – Use the par() function
par() method sets the margin for the plots as shown below.
par(mar=c(5.1, 4.1, 4.1, 2.1), mgp=c(3, 1, 0), las=0)
- mar – A numeric vector of length 4, which sets the margin sizes in the following order: bottom, left, top, and right. The default is c(5.1, 4.1, 4.1, 2.1).
These are the default values; however, we can tweak these values to make the margin much smaller so the plot can fit properly.
#adjust plot margins par(mar = c(1, 1, 1, 1)) # Draw a simple scatter plot plot(1:40)
Solution 3- Using dev.off() method
dev.off() method will remove any plot settings used earlier and create a graphics device with default settings.
Alternatively, you can also execute the below command in the R Console to shut down all open graphics devices.
The broomstick icon in the plot panel will help you to Clear All Plots in the Plots tab, and you can run the code once again.