【问题标题】:Inconsistent spacing in lattice dotplot between y valuesy 值之间的格点图中的间距不一致
【发布时间】:2016-03-19 14:43:51
【问题描述】:

我正在尝试使用“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")))

有人知道发生了什么以及我能做些什么来解决这个问题吗?

【问题讨论】:

    标签: r plot lattice


    【解决方案1】:

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

    引用该帖子中的 Deepayan Sarkar:

    “这有点问题。基本上,您可以使用relation="free"/"sliced",但y的行为与as.numeric(y)一样。因此,如果每个面板中的小子集总是或多或少是连续的(就关卡而言)彼此)那么你会没事的。否则你不会的。在这种情况下,你仍然可以编写自己的prepanelpanel函数,”

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-29
      • 2019-03-29
      • 1970-01-01
      相关资源
      最近更新 更多