[R] Lattice different colours for bars

David Winsemius dwinsemius at comcast.net
Thu Jun 13 08:47:51 CEST 2013


On Jun 12, 2013, at 6:43 PM, Andrew McFadden (Andy) wrote:

> Hi all
> 
> Perhaps this is torturous methodology. I was trying to use lattice to produce a barchart showing the number positive and negative over time. I wasn't quite sure how create a different colour for values of arbo$Ikeda in the example below ie red for ikeda and green for neg.
> 
> 
> library(reshape)
> library(lattice)
> 
> Time=c(rep(6,17), rep(5,17), rep(4,17),
>       rep(3,17),rep(2,17), rep(1,17))
> Ikeda=c(rep("Ikeda",6),rep("Neg",11),
>        rep("Ikeda",0),rep("Neg",17),
>        rep("Ikeda",1),rep("Neg",16),
>        rep("Ikeda",0),rep("Neg",17),
>        rep("Ikeda",0),rep("Neg",17),
>        rep("Ikeda",0),rep("Neg",17))
> Theileria=c(rep("Other",6),rep("Neg",11),
>         rep("Other",12),rep("Neg",5),
>         rep("Other",12),rep("Neg",5),
>         rep("Other",14),rep("Neg",3),
>         rep("Other",14),rep("Neg",3),
>         rep("Other",13),rep("Neg",4))
> value=c(rep(1,102))
> arbo=data.frame(Time, Ikeda,Theileria,value)
> 
> arbo$Time=as.factor(arbo$Time)
> levels(arbo$Time)
> 
> arbo$Time=factor(arbo$Time,
>                    levels=c(1,2,3,4,5,6),
>                    labels=c("Dec 2008", "Dec 2009", "Dec 2010",
>                             "Dec 2011", "Jun 2012", "Dec 2012")
> )
> 
> mdat=melt(arbo,measure.var=c(4),id.var=c(1:3),na.rm=FALSE)
> mdat=cast(mdat,Time +Ikeda~variable,fun.aggregate = c(sum))

barchart(value~Ikeda|Time, data = mdat,
       type="count",
             col=c("green","red")[1+(mdat$Ikeda=="Ikeda")],
       cex=1.1,
       xlab="PCR positive over time",
       aspect = c(1.5),layout = c(6, 1),
       stack=FALSE,
       strip=strip.custom(strip.names=FALSE, strip.levels=TRUE, bg="light blue"),
       par.strip.text=list(cex=1.1), scales=list(cex=c(1.1)))
Any suggestions on how to do this would be appreciated.

> 
> Regards
> 
> Andrew
> 
> Investigation and Diagnostic Centre- Upper Hutt
> 
-- 
David Winsemius
Alameda, CA, USA



More information about the R-help mailing list