[R] Help with lattice graphics

baptiste auguie ba208 at exeter.ac.uk
Mon Dec 1 20:00:49 CET 2008


Hi,

The main difference I saw between your two graphs was the stacking,  
which you can obtain by stack=TRUE in lattice. I'm not sure what  
cosmetic issues you had in mind. Perhaps you can try this,

> barchart(y~dfb|dfyr,dataf,layout=c(3,1),stack=T,ylim=c(0, 2.7),

>          groups=dfa, strip = strip.custom(var.name="year",  
> strip.names = c(TRUE, TRUE)),
>          scales=list(x=list(rot=0), y=list(axs="i", at=seq(0, 2.5,  
> by=0.5))),
>          par.settings=list(axis.text=list(font=2,cex=0.75),
>                              par.ylab.text=list(font=2,cex=1.0),
>                              par.xlab.text=list(font=2,cex=1.0),
>                              par.main.text=list(font=2,cex=1.0)),
>          xlab="b",
>          ylab="y",auto.key = list(space="top",  
> columns=length(levels(dataf$dfa))),
>          main="A test lattice plot")

Baptiste

On 1 Dec 2008, at 18:30, ravi wrote:

> Hi,
> I like the formatting and the appearance of lattice plots. But I  
> have not succeeded in gettting the right format in my plots with the  
> lattice package in one of my applications. In the code shown below,  
> I start by constructing a general data frame and show my attempts  
> with the lattice package commands. After that, I use the graphics  
> package and show the kind of plot that I want to get.
>
> I would like to know how the lattice commands can be modified to get  
> the final plot that I have obtained with the graphics package. Any  
> explanatory comments to understand better the modifications are most  
> welcome.
>
> Here is my code :
> ##########################################################
> rm(list=ls())
> # make up a test data frame with 4 columns
> # factors in first 3 cols and a numeric in the 4th
> vec_a<-paste("A",1:4,sep=(''));na<-length(vec_a)
> vec_b<-paste("B",1:5,sep=(''));nb<-length(vec_b)
> yr<-c("2007","2008","2009");ny<-length(yr)
> fac_a<-factor(vec_a);fac_b<-factor(vec_b);fac_yr<-factor(yr)
> n<-na*nb*ny
> # y is the response variable
> y<-runif(n);y<-round(y,digits=1)
> dfa<-rep(fac_a,times=nb*ny)
> dfb<-rep(fac_b,each=na,times=ny)
> dfyr<-rep(fac_yr,each=na*nb)
> dataf<-data.frame(dfa=dfa,dfb=dfb,dfyr=dfyr,y=y);head(dataf)
> # dataf is the test data frame
> # First test with the lattice package
> library(lattice)
> barchart(y~dfb|dfyr,dataf,layout=c(3,1),beside=FALSE,
>          groups=dfa,
>          scales=list(x=list(rot=90)),
>          par.settings=list(axis.text=list(font=2,cex=0.75),
>                              par.ylab.text=list(font=2,cex=1.0),
>                              par.xlab.text=list(font=2,cex=1.0),
>                              par.main.text=list(font=2,cex=1.0)),
>          xlab="b",
>          ylab="y",
>          main="A test lattice plot")
> ## lattice code doesn't give the desired plot
> ## Next try with the graphics package
> ## Seems to be easier to get the desired plot
> ##First, some preprocessing of data
> df2007<-dataf[dfyr==2007,];df2008<-dataf[dfyr==2008,];df2009<- 
> dataf[dfyr==2009,];
> y2007<-matrix(df2007$y,ncol=nb,nrow=na)
> y2008<-matrix(df2008$y,ncol=nb,nrow=na)
> y2009<-matrix(df2009$y,ncol=nb,nrow=na)
> colnames(y2007)<-vec_b;colnames(y2008)<-vec_b;colnames(y2009)<-vec_b;
> rownames(y2007)<-vec_a;rownames(y2008)<-vec_a;rownames(y2009)<-vec_a;
> ysmax<-ceiling(max(colSums(y2007),colSums(y2008),colSums(y2009)))+1
> ## Now, let's plot
> #### the combination of plots desired as a lattice plot
> col_choice=c("blue", "brown","lightgreen", "mistyrose")
> par(mfrow=c(1,3),mar=c(5,5,5,2))
> barplot(y2007,col = col_choice,
>      beside=F,xlab="fac_b",ylab="y",main="year 2007",
>      font.axis=2,font.lab=2,cex.lab=1.5,cex.axis=1.2,ylim=c(0,ysmax))
> legend(x=1,y=ysmax,legend=vec_a,fill = col_choice)
> barplot(y2008,col = col_choice,
>      beside=F,xlab="fac_b",main="year 2008",
>      font.axis=2,font.lab=2,cex.lab=1.5,cex.axis=1.2,ylim=c(0,ysmax))
> barplot(y2009,col =col_choice,
>      beside=F,xlab="fac_b",main="year 2009",
>      font.axis=2,font.lab=2,cex.lab=1.5,cex.axis=1.2,ylim=c(0,ysmax))
> par(mfrow=c(1,1))
> ## Can we get the same plot with the lattice package?
> ## I would like to have the legend on the top in one row
> ##########################################################
>
> Thanking You,
> Ravi
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

_____________________________

Baptiste Auguié

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag



More information about the R-help mailing list