Specifing order of lattice plot panels

限于喜欢 提交于 2020-01-21 14:59:48

问题


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 from starting in the lower left to starting in the upper left but does nothing about the order of panes within the group.

The data (which are proprietary to my client) have station identifications that are a combination of letters and numbers. When there is a series of sites with the same initial letters within the group of all sites being plotted, they sort by first digit rather than the way we humans count. For example, SW-1, SW-10, SW-11, SW-2, SW-3. I would like them in the order SW-1, SW-2, SW-3, SW-10, SW-11. The code I use is:

 xyplot(as.d$quant ~ as.d$sampdate | as.d$site, ylim=range(as.d$quant), xlim=range(as.d$sampdate), 
 main='Arsenic By Time', ylab='Concentraion (mg/L)', xlab='Time')

I do not know how to attach a .pdf of the resulting plot but will do so if someone shows me how to do this.


回答1:


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')



回答2:


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.



来源:https://stackoverflow.com/questions/15033107/specifing-order-of-lattice-plot-panels

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!