如何指定一组格子xypots中的绘图顺序?

时间:2012-07-10 09:01:52

标签: r lattice

我的代码生成9个可爱的散点图。然而,它们以奇怪的顺序出现(看似从右到左,从列的列表底部开始绘制)。

有没有办法指定它们应该绘制的顺序?

我的代码如下。

Z <- as.vector(as.matrix(sample_clean_data[, c("agri_dist", "allroads_dist", "DES_dist",
                                "elevation", "river_dist", "pop_dens",
                                "slope", "urban_dist", "LS_dist_to_edge")]))


Y10 <- rep(sample_clean_data$change, 9)

MyNames <- names(sample_clean_data[,c("agri_dist", "allroads_dist", "DES_dist",
                       "elevation", "river_dist", "pop_dens",
                       "slope", "urban_dist", "LS_dist_to_edge")])

ID10 <- rep(MyNames, each = length(sample_clean_data$change))
library(lattice)


ID11 <- factor(ID10, labels = c("Distance to nearest agriculture",  
                            "Distance to nearest road", 
                            "Distance to Dar es Salaam", 
                            "Elevation (m)", 
                            #"Distance to nearest main road", 
                            "Distance to nearest major river", 
                            "Population density (indiv./km2)",
                            "Slope",
                            "Distance to nearest urban centre",
                            "Landsat - Distance to forest edge"
),
           levels = c("agri_dist", "allroads_dist", "DES_dist",
                      "elevation", "river_dist", "pop_dens",
                      "slope", "urban_dist", "LS_dist_to_edge"))    

 xyplot(Y10 ~ Z | ID11, col = 1,
   strip = function(bg='white',...) strip.default(bg='white',...),
   scales = list(alternating = F,
                 x = list(relation = "free"),
                 y = list(relation = "same")),
   xlab = "Covariates",
   par.strip.text = list(cex = 0.8),
   ylab = "Change",
   panel=function(x, y, subscripts,...){
     panel.grid(h =- 1, v = 2)
     panel.points(x, y, col = 1, pch = 16)
     })

我认为他们会按照他们进入的顺序从列表的顶部出来,并从左上角绘制。

1 个答案:

答案 0 :(得分:2)

要获得您期望的绘图顺序,可以尝试在as.table=TRUE的通话中添加xyplot()

来自?xyplot

as.table:

    A logical flag that controls the order in which panels should be displayed: 
    if FALSE (the default), panels are drawn left to right, bottom to top (as in 
    a graph); if TRUE, left to right, top to bottom (as in a table).