[R] Reordering levels of a factor within a lattice dotplot
Rui Barradas
ruipbarradas at sapo.pt
Fri Jun 22 16:03:34 CEST 2012
Hello,
Try the following
myex <- myexample
myex$sample <- as.character(myex$sample)
myex$sample <- as.Date(myex$sample, format="%d/%m/%Y")
myex
Then, run the first attempt without bothering to order, lattice is
clever and will do it for you.
Hope this helps,
Rui Barradas
Em 22-06-2012 13:20, maxbre escreveu:
> 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?
>
> thank you
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Reordering-levels-of-a-factor-within-a-lattice-dotplot-tp4634201.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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