8

For use in a scientific publication I would like to add a key (A, B, C) as a 'title' to three plots in one graph generated with par(mfrow=c(3,1)). A very easy solution to add a nice 'title' key to a single plot is to use the 'outer' parameter of the title() (e.g. title(outer=T,adj=0,main="A",cex=1.1, col="black",font=2,line=-1)) however when this would be used for multiple plots, all titles end up in the same place:

par(mfrow=c(3,1))
plot(rnorm(100),col="red")
title(outer=T,adj=0,main="A",cex=1.1,col="black",font=2,line=-1)
plot(rnorm(100),col="blue")
title(outer=T,adj=0,main="B",cex=1.1,col="black",font=2,line=-1)
plot(rnorm(100),col="green")
title(outer=T,adj=0,main="C",cex=1.1,col="black",font=2,line=-1)

With keys above another

I am aware that I could use layout and/or mtext (Common main title of a figure panel compiled with par(mfrow)) however I feel there might be an easier solution that I am overlooking (I have no acces to Adobe Illustrator and would like to use R to generate the graph completely). The solution I can get with the use of layout is not good enough (key is too much separated from the plot and not enough to the left), also I find the playing around with the margins and coordinates a bit too tedious.

par(mar=c(1,2,2,1))
layout(matrix(seq(1,6),ncol=1),heights=rep(c(1,3),3))
plot.new()
text(0,0.5,"A",cex=1.1,font=2)
plot(rnorm(100),col="red")
plot.new()
text(0,0.5,"B",cex=1.1,font=2)
plot(rnorm(100),col="blue")
plot.new()
text(0,0.5,"C",cex=1.1,font=2)
plot(rnorm(100),col="blue")

Undesired result Any help would be much appreciated. Even if the answer would be that this is simply not possible in an easy way, that would still be very helpful.

Community
  • 1
  • 1
FM Kerckhof
  • 1,270
  • 1
  • 14
  • 31

1 Answers1

12

When you use outer=TRUE, you are asking to write the title in the outer margin (common to all sub-plots). To do what you want, just set outer=FALSE:

outer = FALSE

line = -2
cex = 2
adj  = 0.025

par(mfrow=c(3,1))
plot(rnorm(100),col="red")
title(outer=outer,adj=adj,main="A",cex.main=cex,col="black",font=2,line=line)
plot(rnorm(100),col="blue")
title(outer=outer,adj=adj,main="B",cex.main=cex,col="black",font=2,line=line)
plot(rnorm(100),col="green")
title(outer=outer,adj=adj,main="C",cex.main=cex,col="black",font=2,line=line)

thePlot

Also, if you want the labels to be in the side, you can use mtext instead title:

line = 6
cex = 2
las = 2

par(mfrow=c(3,1), oma=c(1,6,1,1))

plot(rnorm(100),col="red")
mtext("A", side=2, line=line, cex=cex, las=las)
plot(rnorm(100),col="blue")
mtext("B", side=2, line=line, cex=cex, las=las)
plot(rnorm(100),col="green")
mtext("C", side=2, line=line, cex=cex, las=las)

plot2

Another option, to have the labels in the corner, is:

line = 1
cex = 2
side = 3
adj=-0.05

par(mfrow=c(3,1), oma=c(1,6,1,1))

plot(rnorm(100),col="red")
mtext("A", side=side, line=line, cex=cex, adj=adj)
plot(rnorm(100),col="blue")
mtext("B", side=side, line=line, cex=cex, adj=adj)
plot(rnorm(100),col="green")
mtext("C", side=side, line=line, cex=cex, adj=adj)

plot3

It is possible to use negative values for adj.

Ricardo Oliveros-Ramos
  • 4,322
  • 2
  • 25
  • 42
  • I appreciate the idea, but the dummy data above are not the plots I am going to use in my publication (I will be using rarefaction plots from vegan's `rarecurve` function that I adapted a bit). My plots have horizontal lines that prohibit upper left corner (logical place) letters. Hence I would like them in the outer margin of each of the separate plots, if that would be feasible. – FM Kerckhof Oct 30 '13 at 20:03
  • 1
    @FMKerckhof That's no problem, just use this answer but set `line = 2` instead of `line = -2`. – Gregor Thomas Oct 30 '13 at 22:44
  • @shujaa The key issue is that I want to label each plot on the side of the plot, and not on top. As such I want to have it above the y axis or even above the y-axis label or even further to the left (in the outer margin). A (not perfect) example is given here: https://www.dropbox.com/s/xragh9g5ng4w4hh/combi%201.svg . But for now I will use the answer of Ricardo. – FM Kerckhof Oct 31 '13 at 09:46
  • @FMKerckhof: I didn't get that from the question, I'm editing the answer. – Ricardo Oliveros-Ramos Nov 01 '13 at 04:03