I've been building a ranked dot plot for several days now and am sorting the
data using the reorder command. What I don't understand is how reorder
works when mutiple varibles are plotted by grouping. In the example below
I'm using re-order to sort by a variable name Resv_Prop, but I'm
plotting up
to three different values of Resv_prop (different Year values) for each
factor.
The results are not what i expected and I would like to control the sorting
by the 2010 Year values.
Any assistance greatly appreciated (Data file is attached)
(out of interst this code also has a very neat solution for plotting two
different keys - code by Deepayan Sarkar)
## initalise
library("lattice")
library(latticeExtra) # for mergedTrellisLegendGrob()
##read the data to a variable
Cal_dat <- read.table("Calibration2.dat",header = TRUE,sep =
"\t",)
## set up plotting colours
col.pat<-c("violet","cyan","green","red","blue","black","yellow")
sym.pat<-c(19,20,21)
#list(levels(Cal_dat$Year)
##set up the plot keys
key1 <-
draw.key(list(text=list(levels(Cal_dat$Commodity)),
title="Ore type",
points=list(pch=21, cex=1.3, fill=col.pat,
col="black")),
draw = FALSE)
key2 <-
draw.key(list(text=list(levels(factor(Cal_dat$Year))),
title="Year",
points = list(pch = c(21, 22, 23), cex=1.3,
col="black")),
draw = FALSE)
mkey <-
mergedTrellisLegendGrob(list(fun = key2),
list(fun = key1),
vertical = TRUE)
)
##set some parameters for the plot
trellis.par.set(
dot.line=list(col = "grey90", lty="dashed"),
axis.line=list(col = "grey90"),
axis.text=list(col ="grey50", cex=0.8),
panel.background=list(col="transparent"),
par.xlab.text= list(col="grey50")
)
## Create the dot plot
with(Cal_dat,
dotplot(reorder(paste(Mine,Company), Resv_Prop) ~ Resv_Prop,
fill_var = Commodity,
pch_var = factor(Year),
cex=1.2,
pch = c(21, 22, 23),
col = "black",
fill = col.pat,
aspect = 2.0,
## key = plot.key,
legend = list(inside = list(fun = mkey, corner = c(1, 0))),
origin = 0,
type = c("p"),
main = "Reserve Proportion",
xlab= "Proportion of Total Resource",
panel = function(x, y, ..., subscripts,
fill, pch, fill_var, pch_var) {
pch <- pch[pch_var[subscripts]]
fill <- fill[fill_var[subscripts]]
panel.dotplot(x, y, pch = pch, fill = fill, ...)
})) http://r.789695.n4.nabble.com/file/n3768125/Calibration2.dat
Calibration2.dat
--
View this message in context:
http://r.789695.n4.nabble.com/Sorting-order-of-reorder-with-multiple-variables-tp3768125p3768125.html
Sent from the R help mailing list archive at Nabble.com.
Deepayan Sarkar
2011-Aug-27 06:18 UTC
[R] Sorting order of reorder with multiple variables
On Thu, Aug 25, 2011 at 6:15 PM, markm0705 <markm0705 at gmail.com> wrote:> I've been building a ranked dot plot for several days now and am sorting the > data using the reorder command. ?What I don't understand is how reorder > works when mutiple varibles are plotted by grouping. ?In the example below > I'm using re-order to sort by a variable name Resv_Prop, but I'm plotting up > to three different values of Resv_prop (different Year values) for each > factor. > > The results are not what i expected and I would like to control the sorting > by the 2010 Year values.reorder() does have a FUN argument that let's you define a summary measure that is used for sorting when multiple observations are present per group. Unfortunately, this will not help you here. However, once you realize that all you really need are the levels() of your y-variable in the right order, you can finesse the problem as follows: Cal_dat_2010 <- subset(Cal_dat, Year == 2010) tmp <- with(Cal_dat_2010, reorder(paste(Mine,Company), Resv_Prop)) with(Cal_dat, dotplot(factor(paste(Mine,Company), levels = levels(tmp)) ~ Resv_Prop, groups = factor(Year), auto.key = TRUE)) -Deepayan