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.
Reordering levels of a factor within a lattice dotplot
7 messages · Massimo Bressan, Rui Barradas, Deepayan Sarkar
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.
thanks for your reply but I don't think your solution can accomplish to what I need... (I can't see any ordering of the variable "sample" with reference to the variable "obs") please keep in mind that for a numer of reasons that I do not mention here for the sake of conciseness in my case the variable "sample" must be espressed as a charachter (because what I proposed here is a simplification of the original variable composed by a concatenation of more information strings than the sampling date itself) and finally the labels on y axis are still not correctly disposed : same problem as in my example (also this feature must be properly sorted out) I think it's necessary to stick on the reordering of varibles but I can't figure how jet... m -- View this message in context: http://r.789695.n4.nabble.com/Reordering-levels-of-a-factor-within-a-lattice-dotplot-tp4634201p4634220.html Sent from the R help mailing list archive at Nabble.com.
Hello, Ok, I hadn't understood. I still don't, not completely. Why do you want to order 'sample' with reference to 'obs'? It would make sense only in the case of 'obs' ties. Doesn't it make more sense to order 'sample' within 'site'? As for keeping 'sample' character it's very simple, I was just trying to not using facotrs for it. The line that makes of it a Date should change to myex$sample <- as.character(as.Date(myex$sample, format="%d/%m/%Y")) Then, to order, within 'site' use # first attempt, (Rui's 2nd) ord <- with(myex, order(site, sample)) And if you now plot it, you'll see the samples ordered and a better y axis labels placing. Rui Barradas Em 22-06-2012 16:02, maxbre escreveu:
thanks for your reply but I don't think your solution can accomplish to what I need... (I can't see any ordering of the variable "sample" with reference to the variable "obs") please keep in mind that for a numer of reasons that I do not mention here for the sake of conciseness in my case the variable "sample" must be espressed as a charachter (because what I proposed here is a simplification of the original variable composed by a concatenation of more information strings than the sampling date itself) and finally the labels on y axis are still not correctly disposed : same problem as in my example (also this feature must be properly sorted out) I think it's necessary to stick on the reordering of varibles but I can't figure how jet... m -- View this message in context: http://r.789695.n4.nabble.com/Reordering-levels-of-a-factor-within-a-lattice-dotplot-tp4634201p4634220.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.
Sorry, missed the plot instruction.
dotplot(sample[ord] ~ obs[ord] | site, data=myex,
[... etc ...]
Rui Barradas
Em 22-06-2012 16:43, Rui Barradas escreveu:
Hello, Ok, I hadn't understood. I still don't, not completely. Why do you want to order 'sample' with reference to 'obs'? It would make sense only in the case of 'obs' ties. Doesn't it make more sense to order 'sample' within 'site'? As for keeping 'sample' character it's very simple, I was just trying to not using facotrs for it. The line that makes of it a Date should change to myex$sample <- as.character(as.Date(myex$sample, format="%d/%m/%Y")) Then, to order, within 'site' use # first attempt, (Rui's 2nd) ord <- with(myex, order(site, sample)) And if you now plot it, you'll see the samples ordered and a better y axis labels placing. Rui Barradas Em 22-06-2012 16:02, maxbre escreveu:
thanks for your reply but I don't think your solution can accomplish to what I need... (I can't see any ordering of the variable "sample" with reference to the variable "obs") please keep in mind that for a numer of reasons that I do not mention here for the sake of conciseness in my case the variable "sample" must be espressed as a charachter (because what I proposed here is a simplification of the original variable composed by a concatenation of more information strings than the sampling date itself) and finally the labels on y axis are still not correctly disposed : same problem as in my example (also this feature must be properly sorted out) I think it's necessary to stick on the reordering of varibles but I can't figure how jet... m -- View this message in context: http://r.789695.n4.nabble.com/Reordering-levels-of-a-factor-within-a-lattice-dotplot-tp4634201p4634220.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.
______________________________________________ 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.
On Fri, Jun 22, 2012 at 5:50 PM, maxbre <mbressan at 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
thanks a lot deepayan, I will study carefully your code! thanks max -- View this message in context: http://r.789695.n4.nabble.com/Reordering-levels-of-a-factor-within-a-lattice-dotplot-tp4634201p4634277.html Sent from the R help mailing list archive at Nabble.com.