On Fri, Jun 22, 2012 at 5:50 PM, maxbre <mbres...@arpa.veneto.it> wrote: > Given my reproducible example > > myexample<-structure(list(site = structure(c(4L, 2L, 2L, 4L, 2L, 4L, 4L, > 3L, 1L, 3L, 1L, 1L, 3L, 4L, 5L, 2L), .Label = c("A", "B", "C", > "D", "E"), class = "factor"), obs = c(0.302, 0.956, 0.72, 1.21, > 0.887, 0.728, 1.294, 20.493, 0.902, 0.031, 0.468, 2.318, 4.795, > 89.581, 4.59, 3618.353), sample = structure(c(6L, 6L, 2L, 8L, > 7L, 7L, 9L, 4L, 4L, 1L, 1L, 3L, 3L, 10L, 11L, 5L), .Label = c("18/08/2009", > "20/04/2009", "03/12/2008", "17/03/2009", "05/01/2012", "21/04/2009", > "17/07/2009", "17/04/2009", "21/07/2009", "29/01/2009", "16/07/2008" > ), class = "factor", scores = structure(c(2, 3, 2, 3, 4, 4, 2, > 5, 2, 4, 2), .Dim = 11L, .Dimnames = list(c("18/08/2009", "21/04/2009", > "20/04/2009", "17/07/2009", "17/04/2009", "21/07/2009", "03/12/2008", > "16/07/2008", "17/03/2009", "29/01/2009", "05/01/2012"))))), .Names = > c("site", > "obs", "sample"), row.names = c(NA, -16L), class = "data.frame") > > > I want to dotplot with lattice the observations (obs) against sampling dates > (sample) for each site but I need to reorder the factor levels of sampling > dates based on the value of observations within each site (rather than > keeping the original arbitrary data) > > These are my two best (?) attempts both of them not properly working for > different reasons > > #start > > library(lattice); library(latticeExtra) > > #first attempt > myexample$sample<- > with(myexample, reorder(sample,obs)) > > > dotplot(sample ~ obs | site, data=myexample, > scales=list(x=list(log=TRUE), y=list(relation="free")), > xscale.components = xscale.components.logpower, > strip=FALSE, strip.left=TRUE, layout=c(1,5), > > index.cond= function(x,y){median(x)}, > > panel = function(x,y,...) { > panel.dotplot(x,y,...) > panel.abline(v = median(x), col.line="red", lty="dotted") > } > ) > > > #second attempt > myexample$sample<- > with(myexample, reorder(reorder(sample,obs), as.numeric(site))) > > > dotplot(sample ~ obs | site, data=myexample, > scales=list(x=list(log=TRUE), y=list(relation="free")), > xscale.components = xscale.components.logpower, > strip=FALSE, strip.left=TRUE, layout=c(1,5), > > index.cond= function(x,y){median(x)}, > > panel = function(x,y,...) { > panel.dotplot(x,y,...) > panel.abline(v = median(x), col.line="red", lty="dotted") > } > ) > > #end > > There is to note the presence of some ties (i.e. same sampling dates, > particularly noticeable for site A and B). > The number of factor levels related to sampling dates (11) is different than > total number of observations (17): is this responsible for the lack of > reordering for factor sample in the dotplot? > How to fix this ? How to get a neat y axis without that “holes” in between > of the sampling dates within each site? > > Should I try to make somehow as much factor levels as the observations so > that to avoid this sort of problem? Is there any alternative solution?
Yes, you need to avoid duplicates. Here is one way to do that: myexample$sampleLabel <- as.character(myexample$sample) myexample$sampleId <- gl(length(myexample$sample), 1) myexample$sample2 <- with(myexample, reorder(reorder(sampleId, obs), as.numeric(site))) ## correct plot, but useless y-axis labels dotplot(sample2 ~ reorder(site, obs) | site, data = myexample, scales=list(x=list(log=TRUE), y=list(relation="free")), xscale.components = xscale.components.logpower, strip=FALSE, strip.left=TRUE, layout=c(1,5)) ## match reordered levels with original order, and set axis labels nl <- as.numeric(levels(myexample$sample2)) dotplot(sample2 ~ obs | reorder(site, obs), data = myexample, scales=list(x=list(log=TRUE), y=list(relation="free")), ylim = myexample$sampleLabel[nl], xscale.components = xscale.components.logpower, strip=FALSE, strip.left=TRUE, layout=c(1,5)) -Deepayan ______________________________________________ R-help@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.