[R] How to do a pretty panel plot?
Jakson Alves de Aquino
jaksonaquino at gmail.com
Fri May 15 12:12:43 CEST 2009
Ajay Shah wrote:
> 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")
>
I think it's better if the lines are above the grid:
## 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=0, ylab="Cospi level")
grid(col = "lightgrey", lty=1)
lines(M$date, M$cospi, type="l", lwd=2)
axis(1, col="grey", at=NULL, labels=FALSE)
axis(2, col="black", labels=TRUE)
axis(3, col="grey", labels=TRUE)
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=0, ylab="Cospi P/E")
grid(col = "lightgrey", lty=1)
lines(M$date, M$cospi.PE, col="black", lwd=2)
axis(2, col="black", at=NULL, labels=T)
More information about the R-help
mailing list