Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

control of title parameters of a plot in R

Tags:

plot

r

I was trying to set a single common title, and a unique common x-y-axis labels for 4 combined subplots:

dev.new( width =  9, height = 10)
layout( matrix( c( 0, 1, 1, 2, 3, 4, 2, 5, 6, 0, 7, 7 ), 4, 3, byrow = TRUE), widths = c( 1, 4, 4 ), heights = c( 1, 4, 4, 1 ) )
par( mar = c( 1, 0, 1, 0 ) )
plot( c(1:2), type = "n", xlab = "", ylab = "", axes = F, cex = 0.7 ) #general title 
title( main = "title", ps = 2 )
par( mar = c( 1, 0, 1, 1) )
plot( c(1:2), type = "n", xlab = "", ylab = "", axes = F, las = 2, cex = 0.7 ) #general y-label  
title( main = " y-label ", las = 0 )
par( cex= 0.9,  mar = c( 5, 1, 1, 2 ) )
plot( c(1:10), type="l", xlab = "A", ylab = "", axes = T, las = 1, cex = 0.7 ) # first    subplot 
par( cex= 0.9,  mar = c( 5, 1, 1, 3 ) )
plot( c(10:1), type ="l", xlab = "B", ylab = "", axes = T, las = 1, cex = 0.7 ) # second subplot
par( cex= 0.9,  mar = c( 5, 1, 1, 2 ) )
plot( c(1:10), type="l", xlab = "C", ylab = "", axes = T, las = 1, cex = 0.7 ) # third subplot
par( cex= 0.9,  mar = c( 5, 1, 1, 3 ) )
plot( c(1:2), type="l", xlab = "D", ylab = "", axes = T, las = 1, cex = 0.7 ) # fourth subplot
par(mar = c( 1, 0, 1, 0 ) )
plot( c(1:2), type = "n", xlab = "", ylab = "", axes = F, cex = 0.7 ) #general x-label
title( main = " x-label " )

How can I control for font size, position and orientation of these titles?

like image 899
Santi XGR Avatar asked Aug 29 '12 10:08

Santi XGR


1 Answers

What you are looking for is a an outer margin for the entire figure. Set it with par(oma=...) and add the axis labels and title in it with mtext(..., outer=TRUE).

par(mfrow=c(2,2), oma=c(3,3,4,0), mar=c(4,2,1,1), las=1, cex=0.7)
plot(1:10, type="l", xlab="A", ylab="")
plot(10:1, type ="l", xlab="B", ylab="")
plot(1:10, type="l", xlab="C", ylab="")
plot(1:2, type="l", xlab="D", ylab="")
mtext("X-label", 1, 1, outer=TRUE)
mtext("Y-label", 2, 1, outer=TRUE, las=0)
mtext("Title", 3, 1, outer=TRUE, cex=2)

enter image description here

Note that 1:10 is equivalent to c(1:10) and that all par settings that are constant across panels A-D only need to be set once, in the par call.

like image 129
Backlin Avatar answered Sep 30 '22 11:09

Backlin