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? 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}}
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.
barbara.chaves wrote:> > > 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. > ... >Thanks a lot for the nice self-contained example which certainly has cost you some time. A bit of nitpicking: You could have improved it a little by removing decorative stuff and using a lower degree of indentation to avoid ugly line breaks in email. You were close by, but I believe that you ran into the trap of believing that the dot in ID.TRT is something special in variable names as it is in c or Pascal. The dot in names has sometimes a meaning for functions (try methods("print")) but it is not possible to "get a part of it" in panel.dot. The workaround is simple: pass TRT, not ID.TRT. Everything else was fine, some minor modifications were added. Dieter # Begin code dataset <- expand.grid ( LOC=c("LOC1", "LOC2"), # Better make it a factor immediately to avoid surprises later BLOC=as.factor(c(1,2,3)), ID=c("A", "B", "C", "D","E"), TRT=c("T1", "T2", "T3","T4")) # replaced nested ifelse dataset$COL <- factor(dataset$TRT,labels=c("green", "blue","orange","red")) dataset$ID.TRT <- as.factor(paste(dataset$ID,dataset$TRT,sep=".")) dataset$OUT <- sample(50:60, 120, replace=TRUE) # removed "as.factor". Corrected TRT dotplot (ID ~ OUT | BLOC * LOC, data=dataset,pch=c(19,4,17,15), TRT = dataset$TRT, fill.color = dataset$COL, panel = function(x, y, TRT, ..., subscripts) { panel.dotplot(x, y,fill=fill.color, ...) panel.abline (v=mean(x [y=="E" & TRT =="T1"],na.rm=TRUE),col="green", lty=2) }) -- View this message in context: http://r.789695.n4.nabble.com/Lattice-dotplots-tp3067130p3067382.html Sent from the R help mailing list archive at Nabble.com.