ggplot 中的抖动是如何确定的?

How is jitter determined in ggplot?

我在制作一些绘图时查看了 ggplot 中关于抖动的 documentation,我意识到我并不真正理解这个论点。

它指出参数是:

Width: degree of jitter in x direction. Defaults to 40% of the resolution of the data.
height: degree of jitter in y direction. Defaults to 40% of the resolution of the data.

我的问题是,到底什么是分辨率,它是如何确定的?

此外,您可以覆盖它并提供一个值,例如在下面我们使用 0.1 的示例中:

geom_point(position = position_jitter(w = 0.1, h = 0.1))

0.1属于什么单位?我认为这部分分辨率是否正确?

如果我们查看 source,我们首先会发现:

PositionJitter <- proto(Position, {
  objname <- "jitter"

  adjust <- function(., data) {
    if (empty(data)) return(data.frame())
    check_required_aesthetics(c("x", "y"), names(data), "position_jitter")

    if (is.null(.$width)) .$width <- resolution(data$x, zero = FALSE) * 0.4
    if (is.null(.$height)) .$height <- resolution(data$y, zero = FALSE) * 0.4

    trans_x <- NULL
    trans_y <- NULL
    if(.$width > 0) {
      trans_x <- function(x) jitter(x, amount = .$width)
    }
    if(.$height > 0) {
      trans_y <- function(x) jitter(x, amount = .$height)
    }

    transform_position(data, trans_x, trans_y)
  }

})

你不知道吗,resolution 是一个导出函数(或者你可以搜索它的源代码以找到它 here):

function (x, zero = TRUE) 
{
    if (is.integer(x) || zero_range(range(x, na.rm = TRUE))) 
        return(1)
    x <- unique(as.numeric(x))
    if (zero) {
        x <- unique(c(0, x))
    }
    min(diff(sort(x)))
}

所以...好了!

"resolution" 在此上下文中则大致表示 "the smallest distance between any two elements in a vector".

此值(分辨率的 40%)然后作为 factor 参数传递给 jitter,它有自己的小歌舞:

The result, say r, is r <- x + runif(n, -a, a) where n <- length(x) and a is the amount argument (if specified).

Let z <- max(x) - min(x) (assuming the usual case). The amount a to be added is either provided as positive argument amount or otherwise computed from z, as follows:

If amount == 0, we set a <- factor * z/50 (same as S).

If amount is NULL (default), we set a <- factor * d/5 where d is the smallest difference between adjacent unique (apart from fuzz) x values.