y 值之间的点阵图中的间距不一致

Inconsistent spacing in lattice dotplot between y values

我正在尝试使用“lattice”中的 dotplot() 来绘制一个数据集,其中类别仅存在于一个子集中,我正在调用 scales = list(y = list(relation = "free")) 以避免不必要的垂直间距。然而,这样做似乎搞砸了项目之间的垂直间距。更重要的是,这似乎与类别是否重叠有关,因为只有在重叠时才会出现错误。

library(lattice)

variables <- c(rep("Age", 4), rep("Sex", 2), rep("Children", 3))
levels <- c(1, 5, 100, 101, "Females", "Males", 2, 3, 90)
values <- rnorm(9)    

dotplot(levels ~ values | variables, layout = c(1,3),
        scales = list(y = list(relation = "free")))

你可以清楚地看到,例如 90 和 3 之间的间距是关闭的,而男性和女性之间没有问题。现在,如果我更改具有数值的类别以使它们不重叠,我就会得到正确的间距。

levels <- c(1:4, "Females", "Males", 5:7)

dotplot(levels ~ values | variables, layout = c(1,3),
        scales = list(y = list(relation = "free")))

有人知道这是怎么回事吗?我能做些什么来解决这个问题?

您可以使用 lattice 作者的函数(参见 dotplot, dropping unused levels of 'y')。

引述 post 中的 Deepayan Sarkar:

"It's a bit problematic. Basically, you can use relation="free"/"sliced", but y behaves as as.numeric(y) would. So, if the small subset in each panel are always more or less contiguous (in terms of the levels being close to each other) then you would be fine. Otherwise you would not. In that case, you can still write your own prepanel and panel functions,"

dotplot(levels ~ values | variables, layout = c(1,3),
        scales = list(y = list(relation = "free")),
        prepanel = function(x, y, ...) {
          yy <- y[, drop = TRUE]
          list(ylim = levels(yy),
               yat = sort(unique(as.numeric(yy))))
        },
        panel = function(x, y, ...) {
          yy <- y[, drop = TRUE]
          panel.dotplot(x, yy, ...)
        })