Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

Mastering R plot – Part 3: Outer margins

$
0
0

By Lionel Hertzog

oma1

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
op

Here 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 the mtext function we need to set outer=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:
oma2

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:
oma3

And we can also add a common legend:

set.seed(20160228)
#outer margins can also be used for plotting legend in them
x

Here it is the plot:
oma4

An important ...read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles