在探索一些数据的时候发现了这一点,这感觉像是意外的行为,所以我想我会发帖。
geom_jitter采用高度/宽度参数来表示抖动的宽度,默认值为40%。当添加日志刻度时,这40%似乎应用于原始值。但是,如果要调整此参数,则在日志转换后应用该值。
这可以如下所示:
library(ggplot2)
library(patchwork)
set.seed(1)
dat <- data.frame(x=round(rlnorm(2000)), y=round(rlnorm(2000)))
# THESE TWO PLOTS ARE THE SAME
# with jitter
p1 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1) +
labs(title='regular scale, jitter with default height/width',
subtitle = '')
# with jitter, and explicit (but same as default) jitter size
p2 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1, height=.4, width=.4) +
labs(title='regular scale, jitter with 40% height/width',
subtitle = '<== same as that')
# THESE TWO PLOTS ARE NOT THE SAME
# with jitter and log/log scale
p3 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1) +
scale_x_log10() + scale_y_log10() +
labs(title='log scale, jitter with default height/width',
subtitle = '')
# with jitter and log/log scale, and explicit (but same as default) jitter size
p4 <- ggplot(dat, aes(x,y)) + geom_jitter(alpha=.1, height=.4, width=.4) +
scale_x_log10() + scale_y_log10() +
labs(title='log scale, jitter with 40% height/width',
subtitle = '<== NOT the same as that')
(p1 + p2) / (p3 + p4)

这是预期的行为吗?
如果我想调整基础值的抖动宽度,而不是日志转换值的抖动宽度,该怎么办?
发布于 2019-05-06 23:12:31
这是个不错的机会!我想这是个文档问题--还不够清楚。抖动是而不是40%,它是数据分辨率的40%的。
在ggplot2:::PositionJitter$setup_params中,您可以看到应用了ggplot2:::resolution函数,其结果乘以0.4
list(width = self$width %||% (resolution(data$x, zero = FALSE) *
0.4), height = self$height %||% (resolution(data$y, zero = FALSE) *
0.4), seed = self$seed)因此,您需要做的是在将值传递给ggplot2:::resolution /height之前应用width/height
geom_jitter(
width = ggplot2:::resolution(log10(dat$x), FALSE) * 0.4,
height = ggplot2:::resolution(log10(dat$y), FALSE) * 0.4,
)

所有代码:
ggplot(dat, aes(x, y)) +
geom_jitter(
width = ggplot2:::resolution(log10(dat$x), FALSE) * 0.4,
height = ggplot2:::resolution(log10(dat$y), FALSE) * 0.4,
) +
scale_x_log10() +
scale_y_log10() +
labs(title = 'Scale when resolution is applied')https://stackoverflow.com/questions/56013149
复制相似问题