在点图面板中禁止未使用的标签

时间:2015-02-06 23:55:48

标签: r lattice

我有以下数据框

   ds <- data.frame(iso2c=as.factor(c(rep("AR",3),rep("BR",3),rep("DE",3),rep("US",3))),

             region= as.factor(c(rep("LATAM",6),rep("DEVELOPED",6))),

             year= rep(c(1979,1989,1999),4),

             value= c( 47.0 , 28.6,  20.8, 100.0,  64.2,  35.4,  16.0 ,  9.0,   5.5,  15.6,  11.6,   8.6))                 )

并希望在点图中绘制数据,如下所示,

  library(lattice)
  dotplot(iso2c~value | region, data=ds, groups=year,pch=19,col="dark blue",cex=1.3,ylab="country",,layout=c(1,2))

问题是,region是iso2c的分区,我在每个面板中有两个空行(AR和BR在RADED中是空的,在LATAM中是US和DE)。

更改面板功能以删除该面板中未使用的级别(请参阅下面的代码)不起作用。

dotplot(iso2c~value | region, data=ds, groups=year,pch=19,col="dark blue",cex=1.3,ylab="country",layout=c(1,2),
     panel=function(x,y,...) {
       panel.dotplot(x,droplevels(y),...)    
    },

)

有没有办法在面板中抑制未使用的等级?

1 个答案:

答案 0 :(得分:1)

您可以使用scales函数中的dotplot参数来表示您希望y轴可以自由缩放。然后,这将仅包括每个面板中存在的那些级别:

dotplot(iso2c~value | region, data=ds, groups=year,pch=19,
    col="dark blue",cex=1.3,ylab="country",layout=c(1,2),
    scales = list(y = list(relation = "free")))

enter image description here

相关问题