[R] Lattice dotplots

Peter Ehlers ehlers at ucalgary.ca
Wed Dec 1 15:06:07 CET 2010


On 2010-12-01 04:02, barbara.chaves at bayer.com wrote:
> Dear,
>
> I have a dataset with 4 subjects (see ID in example), and 4 treatment (see
> TRT in example) which are tested on 2 locations and in 3 blocs. By using
> Lattice dotplot, I made a graph that shows the raw data per location and
> per bloc. In that graph, I would like to have a reference line per bloc
> that refers to the first treatment (T1). However, I can not find how to do
> that.
>
> I can make a dotplot with a reference line which is the average of all
> treatments (see 2) in example). Under 3), I used the code "panel.abline
> (v=mean(x [y=="E"&  TRT =="T1"],na.rm=TRUE),col="green", lty=2)" by which
> I hoped to set the reference line at 'T1', but than I get the error that
> 'object TRT is missing'.
>
> Does anybody know a solution?

Try this (I've stripped out the unnecessary stuff; what
is it about *minimal* that seems to elude people?):

   p <- dotplot (ID ~ OUT | as.factor(BLOC) * LOC,
          data=dataset,pch=c(19,4,17,15),ylab="",xlab="OUT",
          fill.color = dataset$COL,
          TRT = dataset$TRT,
          panel = function(x, y, fill.color, TRT, ..., subscripts) {
            fill <- fill.color [subscripts]
            panel.dotplot(x, y,  col = fill, ...)
            panel.abline (v=mean(x[y=="A" & TRT =="T1"], na.rm=TRUE),
              col="green", lty=2)
          }
   )
   print(p)

Peter Ehlers

>
> Thanks in advance!
> regards,
> Barbara
>
>
> EXAMPLE:
> --------
>
>
> library(lattice)
>
> # 1) example dataset
> #-----------------
> dataset<- expand.grid (LOC=c("LOC1", "LOC2"),
>                                                  BLOC=c(1,2,3),
>                                                  ID=c("A", "B", "C", "D",
> "E"),
>                                                  TRT=c("T1", "T2", "T3",
> "T4"))
>
> dataset$COL<- ifelse(dataset$TRT == "T1", "green",
>                                          ifelse(dataset$TRT == "T2", "blue"
> ,
>                                                          ifelse(dataset$TRT
> == "T3","orange","red")))
>
> dataset$ID.TRT<- as.factor(paste(dataset$ID,dataset$TRT,sep="."))
>
> dataset$OUT<- sample(50:60, 120, replace=TRUE)
>
> #edit(dataset)
>
> dataset<- dataset[order(dataset$LOC,dataset$BLOC,dataset$ID), ]
>
> # 2) dotplot with reference line that is average of all treatments
> #------------------------------------------------------------------
> dotplot (ID ~ OUT | as.factor(BLOC) * LOC,
>                  data=dataset,pch=c(19,4,17,15),ylab="",xlab="OUT",
>                  scales=list(alternating=FALSE,cex=0.6),
>                  fill.color = dataset$COL,
>                  panel = function(x, y, fill.color, ..., subscripts) {
>                          fill<- fill.color [subscripts]
>                          panel.dotplot(x, y,  col = fill, ...)
>                          panel.abline (v=mean(x [y=="E"],na.rm=TRUE),col=
> "green", lty=2)
>                  },
>                  main = "Raw OUT Data",
>                  key = list( space ="bottom",
>                                  columns=2,
>                                  cex=0.8,
>                                  pch=19,
>                                  border=TRUE,
>                                  text= list(c("T1","T2","T3","T4")),
>                                  points = list(col=c("green","blue",
> "Orange","Red"),pch=c(19,4,17,15))))
>
> # 3) dotplot with error in reference line because 'TRT is missing'
> #----------------------------------------------------------------
> dotplot (ID ~ OUT | as.factor(BLOC) * LOC,
>                  data=dataset,pch=c(19,4,17,15),ylab="",xlab="OUT",
>                  scales=list(alternating=FALSE,cex=0.6),
>                  fill.color = dataset$COL,
>                  panel = function(x, y, fill.color,ID.TRT, ..., subscripts)
> {
>                          fill<- fill.color [subscripts]
>                          panel.dotplot(x, y,  col = fill, ...)
>                          panel.abline (v=mean(x [y=="E"&  TRT =="T1"]
> ,na.rm=TRUE),col="green", lty=2)
>                  },
>                  main = "Raw OUT Data",
>                  key = list( space ="bottom",
>                                  columns=2,
>                                  cex=0.8,
>                                  pch=19,
>                                  border=TRUE,
>                                  text= list(c("T1","T2","T3","T4")),
>                                  points = list(col=c("green","blue",
> "Orange","Red"),pch=c(19,4,17,15))))
> ________________________________________________________________________
> The information contained in this e-mail is for the excl...{{dropped:14}}
>
> ______________________________________________
> 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.



More information about the R-help mailing list