I have looked at the two similar questions on this topic but do not find the answer I\'m looking for in either of the two. The as.table function alters the alphabetic sequence f
You need to specify the levels of that factor variable in the sequence you expect. The default is lexigraphic as you noticed:
xyplot(as.d$quant ~ as.d$sampdate | factor( as.d$site,
levels=1:length(unique(as.d$site))) ,
ylim=range(as.d$quant), xlim=range(as.d$sampdate),
main='Arsenic By Time', ylab='Concentration (mg/L)', xlab='Time')
Based on how the question currently stands, you might need:
require(gtools)
xyplot(as.d$quant ~ as.d$sampdate | factor( as.d$site,
levels=mixedsort( as.character(unique(as.d$site)) ) ) ,
ylim=range(as.d$quant), xlim=range(as.d$sampdate),
main='Arsenic By Time', ylab='Concentration (mg/L)', xlab='Time')
There are a couple of points here.
First is that in R things like the order of factor levels are considered to be a property or attribute of the data rather than a property of the graph/analysis. Because of that there is not generally arguments in the plotting or analysis functions for specifying the order, rather you specify that order in the data object itself, then all plots and analyses use that order.
To change the order you can specify the desired order using the factor
function, or you can use functions like relevel
and reorder
to change the order of the levels of a factor. If you want the levels to be in the same order that they appear in the data then the unique
function works well. For sorting with characters and numbers mixed the mixedsort
function in the gtools package can be useful.