

To overcome this we can use par() to first increase the left margin and then use mgp to set the axis title line. Sometimes the axis labels may be very long and overlap with the axis title (for example, large numbers in scientific notation on the y axis). The default value of mgp is c(3,1,0), which means that the axis title is drawn in the fourth line of the margin starting from the plot region, the axis labels are drawn in the second line and the axis line itself is the first line. The mgp argument of the par() function is a vector of 3 values which specify the margin line for the axis title, axis labels and axis line. In the above example, we used the mtext() function (which as the name suggests places text in the margins) to label the margin lines. We already know the number of margin lines from par(“mar”) but let’s make a graph to illustrate this point and see how the margin lines are numbered: Margin lines are numbered starting from 0. That was bound to happen because we used up all of the figure region in margins and left no space for the plot to be drawn! You are probably never going to set such large margins, but in my experience errors like that occur when I’m working with multiple plot layouts (using the mfrow argument – I might write a post about that some time). What happens? We get an error saying “figure margins too large”. One way to verify this is by trying to run the following code: So, by default the figure is 35 lines high and wide. You can verify this by typing par(“fin”) at the R prompt. The default size of the figure region is approximately 7 inches wide by 7 inches high. There are ways to change this line height but that’s a useful number to keep in mind. Since we can specify margins both in terms of lines of text and inches, let’s find out how high one line of text is by default: The bottom, left and top margins are the largest because that’s where annotations and titles are most likely to be placed. You should get back a vector with the above values.

You can verify this by firing up the R prompt and typing par(“mar”) or par(“mai”). The numbers used above are the default margin settings in R. Sets the bottom, left, top and right margins respectively of the plot region in number of lines of text.Īnother way is by specifying the margins in inches using the mai argument: It is fairly straightforward to set the margins of a graph in R by calling the par() function with the mar (for margin!) argument.
