views:

183

answers:

2

If I create a plotting window in R with m rows and n columns, how can I give the "overall" graphic a main title?

For example, I might have three scatterplots showing the relationship between GPA and SAT score for 3 different schools. How could I give one master title to all three plots, such as, "SAT score vs. GPA for 3 schools in CA"?

+8  A: 

The most obvious methods that come to my mind are to use either Lattice or ggplot2. Here's an example using lattice:

 library(lattice)
 depthgroup<-equal.count(quakes$depth, number=3, overlap=0)
 magnitude<-equal.count(quakes$mag, number=2, overlap=0)
 xyplot(lat ~ long | depthgroup*magnitude,
 data=quakes,
 main="Fiji Earthquakes",
 ylab="latitude", xlab="longitude",
 pch=".",
 scales=list(x=list(alternating=c(1,1,1))),
 between=list(y=1),
 par.strip.text=list(cex=0.7),
 par.settings=list(axis.text=list(cex=0.7)))

In lattice you would change the main= parameter.

The above example was lifted from here.

I don't have a good ggplot2 example, but there are a metricasston of examples with ggpolot2 over at the learn r blog.

One option might be this example where they use ggplot2 and

opts (title = "RSS and NINO3.4 Temperature Anomalies \nand SATO Index Trends Since 1980")

But you would have to have all three graphs created in gg2plot, naturally.

I think you should be fine with either lattice or ggplot2.

JD Long
WONDERFUL!!!! Thanks!!!!
Ryan Rosario
how much is a metricasston? :)
dalloliogm
What do you mean when you say that ggplot2 has not support for secondary axis labels? You can customize them when you set the scale, e.g. scale_y_continous('y axis label').
dalloliogm
oh that's a good point dalloliogm. I didn't know that so I will remove that from my answer. Thanks! BTW, a metricasston is exactly 1.42 imperialasstons.
JD Long
+9  A: 

Using the traditional graphics system, here are two ways:

(1)

par(mfrow=c(2,2))
for( i in 1:4 ) plot(1:10)
mtext("Title",side=3,outer=TRUE,padj=3)

(2)

par(mfrow=c(2,2))
for( i in 1:4 ) plot(1:10)
par(mfrow=c(1,1),mar=rep(0,4),oma=rep(0,4))
plot.window(0:1,0:1)
text(.5,.98,"Title")
Stephen
This is a great answer too, and doesn't require ggplot2 or lattice.
Ryan Rosario
Wonderful, I was just looking for that - thanks!
Tal Galili