在点图面板中抑制未使用的标签

Supress unused labels in dotplot panel

我有以下数据框

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

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

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

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),...)    
    },

)

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

您可以在 dotplot 函数中使用 scales 参数来表示您希望 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")))