Thursday 16 October 2014

overlaying graphs from different sources (lattice)

To superimpose graphs (lattice) from different sources, we need "latticeExtra" package.

require("lattice")
require("latticeExtra")


#data source 1

dat<-aggregate(peri~perm,rock,sum)


#data source 2

dat1<-aggregate(rock[,c("peri","shape")],by=list(rock$perm),sum)
colnames(dat1)[1]<-"perm"


Draw barchart using data source 1

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",
main="data: rock",horizontal=FALSE,col="tan",border="tan")






















This graph shows tick marks on the right hand side, but we want to have secondary y-axis with different scales showing there. par.settings=list(axis.components=list(right=list(tck=0))) will remove tick marks from the designated side of axis.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",
par.settings=list(axis.components=list(right=list(tck=0))))























Now, to add the second graph, we use as.layer() function. The controls such as "y.same=FALSE" allows y-axis to have different scales, and control "under" will add the second graph on top (FALSE) or underneath (TRUE) the first graph.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",
par.settings=list(axis.components=list(right=list(tck=0))))+
as.layer(xyplot(shape~factor(perm),dat1,col="brown",type="l",lty=1,lwd=2),
under=FALSE,y.same=FALSE,x.same=TRUE)























This graph now displays the new y-axis scale, but there are no tick marks. To add tick marks on the right hand side, we do the following. tck in the second graph is set to -1 to make the tick mark appear outside the plotting region.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",
par.settings=list(axis.components=list(right=list(tck=0))))+
as.layer(xyplot(shape~factor(perm),dat1,col="brown",type="l",lty=1,lwd=2,
par.settings=list(axis.components=list(right=list(tck=-1)))),
under=FALSE,y.same=FALSE,x.same=TRUE)























This graph now displays the new y-axis scale with tick marks, but there is no axis label. To add y-axis label on the right hand side, we use "ylab.right" in the first graph.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",ylab.right="shape",
par.settings=list(axis.components=list(right=list(tck=0))))+
as.layer(xyplot(shape~factor(perm),dat1,col="brown",type="l",lty=1,lwd=2,
par.settings=list(axis.components=list(right=list(tck=-1)))),
under=FALSE,y.same=FALSE,x.same=TRUE)























This graph still shows tick labels inside the plotting region. To place these labels outside, we can set pad1 to -4 in the second graph. The numeric value determines the position of labels against the axis. To move the axis label further away from axis to avoid overlapping of tick labels and axis label, we can control "axis.key.padding" in the first graph.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",ylab.right="shape",
par.settings=list(axis.components=list(right=list(tck=0)),
layout.widths=list(axis.key.padding=4)))+
as.layer(xyplot(shape~factor(perm),dat1,col="brown",type="l",lty=1,lwd=2,
par.settings=list(axis.components=list(right=list(tck=-1,pad1=-4)))),
under=FALSE,y.same=FALSE,x.same=TRUE)
























To allow bigger margin for right hand side to leave some space beyond axis label, we can control "right.padding" in the first graph.

barchart(peri~factor(perm),dat,xlab="perm",ylab="peri",main="data: rock",
horizontal=FALSE,col="tan",border="tan",ylab.right="shape",
par.settings=list(axis.components=list(right=list(tck=0)),
layout.widths=list(axis.key.padding=4,right.padding=3)))+
as.layer(xyplot(shape~factor(perm),dat1,col="brown",type="l",lty=1,lwd=2,
par.settings=list(axis.components=list(right=list(tck=-1,pad1=-4)))),
under=FALSE,y.same=FALSE,x.same=TRUE)




























No comments:

Post a Comment