[R] How to do a pretty panel plot?

Ajay Shah ajayshah at mayin.org
Fri May 15 06:51:23 CEST 2009


On Thu, May 14, 2009 at 05:09:59PM -0700, koday at processtrends.com wrote:
> Ajay
> 
> I have added a few minor adjustments to your code to make the panel chart you want.
> You need to adjust the par(mar) to control space between 1st and 2nd plot.

Thanks! This is a step forward for me. But now:

  * The dates on the x axis are wrongly labelled
  * The boxes look incomplete - the right hand side is open.

Here's my best version of your code:

## Data
M <- structure(list(date = structure(c(13634, 13665, 13695, 13726, 
                      13757, 13787, 13818, 13848, 13879, 13910, 13939, 13970, 14000, 
                      14031, 14061, 14092, 14123, 14153, 14184, 14214, 14245, 14276, 
                      14304, 14335), class = "Date"),
                    cospi = c(1987.31, 2033.37, 2140.13, 
                      2120.66, 2427.09, 2917.7, 2915.28, 3262.06, 2616.26, 2617.75, 
                      2277.69, 2538.13, 2374.09, 1911.22, 2063.73, 2081.28, 1813.58, 
                      1304.96, 1219.73, 1361.74, 1299.2, 1242.74, 1339.18, 1557.29), 
                    cospi.PE = c(19.2, 19.69, 20.13, 24.08, 27.61, 30.9, 30.69, 
                      34.92, 26.95, 27.63, 23.86, 26.14, 23.72, 19.5, 23.43, 23.73, 
                      20.69, 16.4, 16.12, 18.04, 18.46, 18.86, 20.24, 23.53)),
               .Names = c("date", "cospi", "cospi.PE"),
               row.names = 209:232, class = "data.frame")

## Set up par's to make 2 panel chart 
par(bty="l"); par(ps=10)
par(mfrow=c(2,1))           # try to get two plots, one above the other 
par(mar=c(0,4,0,1))         ## Set par(mar) to eliminate X axis gap 
par(oma=c(2,2,2,2))

## Make Plot 1
plot(M$date, M$cospi, type="l", log="y", 
     xaxs="i", yaxs="i", axes=F, lwd=2,
     ylab="Cospi level") 
axis(1, col="grey", at=NULL, labels=FALSE) 
axis(2, col="black", labels=TRUE) 
axis(3, col="grey", labels=TRUE)
grid(col = "lightgrey", lty=1)
box(col = "grey")

## Adjust par(mar) for 2nd plot
par(mar=c(2,4,0,1))

## Second plot
plot(M$date, M$cospi.PE, type="l", col="black", log="y", 
     xaxs="i", yaxs="i", axes=F, lwd=2,
     ylab="Cospi P/E") 
axis(2, col="black", at=NULL, labels=T) 
axis(1, col="lightgrey", at=NULL, labels=T)
grid(col = "lightgrey", lty=1)
box(col = "grey")

-- 
Ajay Shah                                      http://www.mayin.org/ajayshah  
ajayshah at mayin.org                             http://ajayshahblog.blogspot.com
<*(:-? - wizard who doesn't know the answer.




More information about the R-help mailing list