Translate

Monday, 13 October 2014

Barchart - lattice package

Barchart is one of graph types available from 'lattice' package.

Not so complex horizontal barchart:

Dat<-aggregate(uptake~Plant+Treatment+Type,CO2,sum)

barchart(Plant~uptake|Treatment,Dat,groups=Dat$Type,horizontal=TRUE,
stack=FALSE,layout=c(1,2))
























For vertical barchart, sometimes incorrectly referred as histogram:

barchart(uptake~Plant|Treatment,Dat,groups=Dat$Type,horizontal=FALSE,stack=FALSE)






















For stacked barchart:

barchart(uptake~Type|Treatment,Dat,groups=Dat$Plant,horizontal=FALSE,
stack=TRUE,auto.key=list(space="right"))
























For more controls around the margins and inserting text and lines outside the plotting region, use grid.text and gird.lines from the package "grid".

library(grid)

barchart(uptake~Plant|Treatment,Dat,groups=Dat$Type,
horizontal=FALSE,
stack=FALSE,col=c("salmon","navyblue"),
border="white",
main=list(label="CO2",cex=1.1),
ylab=list(label="concentration",cex=0.8),
xlab=list(label="Plant",cex=0.7),
par.settings=list(layout.widths=list(right.padding=1,axis.key.padding=2,key.right=1.2),
layout.heights=list(bottom.padding=2,xlab.key.padding=5)),
par.strip.text=list(cex=0.8),
key=list(space="right",
text=list(labels=levels(Dat$Type),cex=0.7,col="black"),
points=list(pch=15,cex=1.5,col=c("salmon","navyblue"))),
scales=list(x=list(cex=0.7,rot=20),y=list(relation="free",cex=0.7)))

grid.text(x=unit(0.15,"npc"),y=unit(0.07,"npc"),label=c("Q"),rot=0,
gp=gpar(fontsize=9,col="darkgrey"),draw=TRUE)

grid.text(x=unit(0.33,"npc"),y=unit(0.07,"npc"),label=c("M"),rot=0,
gp=gpar(fontsize=9,col="darkgrey"),draw=TRUE)

grid.lines(x=c(0.06,0.24),y=0.09,gp=gpar(col="salmon",lty=1),draw=TRUE,
arrow=arrow(ends="both",angle=15,length=unit(0.13,"inches")))

grid.lines(x=c(0.24,0.44),y=0.09,gp=gpar(col="navyblue",lty=1),draw=TRUE,
arrow=arrow(ends="both",angle=15,length=unit(0.13,"inches")))

grid.text(x=unit(0.55,"npc"),y=unit(0.07,"npc"),label=c("Q"),rot=0,
gp=gpar(fontsize=9,col="darkgrey"),draw=TRUE)

grid.text(x=unit(0.73,"npc"),y=unit(0.07,"npc"),label=c("M"),rot=0,
gp=gpar(fontsize=9,col="darkgrey"),draw=TRUE)

grid.lines(x=c(0.46,0.64),y=0.09,gp=gpar(col="salmon",lty=1),draw=TRUE,
arrow=arrow(ends="both",angle=15,length=unit(0.13,"inches")))

grid.lines(x=c(0.64,0.84),y=0.09,gp=gpar(col="navyblue",lty=1),draw=TRUE,
arrow=arrow(ends="both",angle=15,length=unit(0.13,"inches")))










No comments:

Post a Comment