This is the third post in our series Mastering R Plot, in this one we will cover the outer margins. To know more about plot customization read my first and second post.
Let’s directly dive into some code:
#a plot has inner and outer margins #by default there is no outer margins par()$oma [1] 0 0 0 0 #but we can add some opHere it is the plot:
From this plot we see that we can control outer margins just like we controlled inner margins using the
par
function. To write text in the outer margins with themtext
function we need to setouter=TRUE
in the function call.Outer margins can be handy in various situations:
#Outer margins are useful in various context #when axis label is long and one does not want to shrink plot area par(op) #example par(cex.lab=1.7) plot(1,1,ylab="A very very long axis titlenthat need special care",xlab="",type="n") #one option would be to increase inner margin size par(mar=c(5,7,4,2)) plot(1,1,ylab="A very very long axis titlenthat need special care",xlab="",type="n") #sometime this is not desirable so one may plot the axis text outside of the plotting area par(op) par(oma=c(0,4,0,0)) plot(1,1,ylab="",xlab="",type="n") mtext(text="A very very long axis titlenthat need special care",side=2,line=0,outer=TRUE,cex=1.7)Here it is the plot:
With outer margins we can write very long or very big axis labels or titles without having to “sacrifice” the size of the plotting region.
This comes especially handy for multi-panel plots:#this is particularly useful when having a plot with multiple panels and similar axis labels par(op) par(oma=c(3,3,0,0),mar=c(3,3,2,2),mfrow=c(2,2)) plot(1,1,ylab="",xlab="",type="n") plot(1,1,ylab="",xlab="",type="n") plot(1,1,ylab="",xlab="",type="n") plot(1,1,ylab="",xlab="",type="n") mtext(text="A common x-axis label",side=1,line=0,outer=TRUE) mtext(text="A common y-axis label",side=2,line=0,outer=TRUE)Here it is the plot:
And we can also add a common legend:
set.seed(20160228)
#outer margins can also be used for plotting legend in them
xHere it is the plot:
An important ...read more
Source:: r-bloggers.com