Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R geom_抖动的高度/宽度参数与对数刻度相互作用_R_Ggplot2 - Fatal编程技术网

R geom_抖动的高度/宽度参数与对数刻度相互作用

R geom_抖动的高度/宽度参数与对数刻度相互作用,r,ggplot2,R,Ggplot2,在探索一些数据时遇到这个问题,感觉像是意外的行为,所以我想我会发布 geom_jitter获取抖动宽度的高度/宽度参数,默认值为40%。添加对数刻度时,这40%似乎应用于原始值。但是,如果要调整此参数,则会在日志转换后应用该值 这可以这样说明: library(ggplot2) library(patchwork) set.seed(1) dat <- data.frame(x=round(rlnorm(2000)), y=round(rlnorm(2000))) # THESE T

在探索一些数据时遇到这个问题,感觉像是意外的行为,所以我想我会发布

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)
库(ggplot2)
图书馆(拼凑)
种子(1)

这是一个漂亮的球!我想这是一个文档问题——还不够清楚。抖动不是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)
因此,您需要做的是在将值传递到
width
/
height
之前应用
ggplot2:::分辨率

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')
对我来说似乎是“期待”的。