Mel
Mel

Reputation: 510

Adjusting margins for R plots

I'm interesting in adjusting margins for R plots. I use R Studio on MacOS, run on a 2013 intel CPU Macbook pro.

Here is the data I used for generating the plot:

spins <- runif(50, min = 0, max = 50)

Here is the code I used to generate the plot:

hist(spins)

Here is the results of the console:

> hist(spins)
Error in plot.new() : figure margins too large

Here are my questions:

  1. How do I find out the current margins of this specific plot?
  2. How do I modify the margins for this specific plot?
  3. Is there a method to modify the margins for plots in general (e..g., Can I use 1 set of code to find the margins of the object and then use a second set of code to modify the margins so it will display in the plots area)?
  4. Is there code I can use to automatically adjust the margins of the plots so that the desired plot displays every time?

Upvotes: 3

Views: 8449

Answers (1)

Greg Snow
Greg Snow

Reputation: 49640

The par function can be used to see the size of current margins and to set margin sizes (plus a whole bunch of other things).

Running par(c("mar", "mai")) will report the current margins (starting at bottom and going clockwise) in lines of text (mar) and inches (mai).

You can then set the margins using code like: par(mar=c(2,2,2,0)) or par(mai=rep(0.4, 4)).

But the problem is probably that you have made the plots pane in RStudio too small in at least one dimension, so when the hist function tries to create the plot there is not enough room for the plot and margin information. Try dragging the appropriate dividers within RStudio to make the plot panel larger then try your code again. Another option is to run the command dev.new() which will open a new window for the plots (using the default for your OS) and by default send the plot to that new window instead of the plot pane in RStudio.

Upvotes: 7

Related Questions