使用geom_jitter扩展scale_x_discrete中的参数?

时间:2018-04-10 23:45:35

标签: r ggplot2

我正在尝试一些在我之前已经困扰很多的东西,但主要是针对连续的尺度,主要是针对y轴。这些问题最接近:

但是,

  • coord_cartesian(expand = ...) 也不是
  • my_dat <- data.frame(x = 'x', y = rnorm(400)) ggplot(my_dat, aes(x, y)) + geom_jitter() 改变一切! (实际上没有发生任何

我做错了什么?

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.3

...

other attached packages:
[1] bindrcpp_0.2     ggbeeswarm_0.6.0 reshape2_1.4.3  
[4] stringr_1.2.0    ggplot2_2.2.1    lubridate_1.7.1 
[7] tidyr_0.7.1      purrr_0.2.4      dplyr_0.7.4 

enter image description here

short[] itemIds = { 9023, 9041 };
int index = rnd.Next(0, itemIds.Length);
short here_is_the_value = itemIds[index];

2 个答案:

答案 0 :(得分:1)

简短回答:在你的情节中,用红色阴影的空格与扩展无关,因此更改expand = ...参数不会影响任何内容。请改为更改geom_jitter中的宽度参数。

解释:如果您检查geom_jitter()的帮助文件,您会发现默认抖动宽度是数据分辨率的40%:

  

...默认为数据分辨率的40%:这意味着   抖动值将占据隐含箱的80%。分类数据是   在整数上对齐,因此0.5的宽度或高度会扩散   因此,不可能看到数据之间的区别   类别。

我们也可以通过改变width参数来说明这一点。用width = 0.4(默认)观察,左边有空格&amp;蓝点的权利。使用width = 0.5时,红点一直到达目的地:

ggplot(my_dat, aes(x, y)) +
  geom_jitter(width = 0.4, color = "blue") +
  geom_jitter(width = 0.5, color = "red")

plot

对于expand = ... / scale_x_discrete中的coord_cartesian参数,它们确实有所作为... 如果您的轴的值超过1:< / p>

my_dat2 <- my_dat
my_dat2$x <- sample(c('y', 'z'), size = 400, replace = TRUE)

p2 <- ggplot(my_dat2, aes(x, y)) +
  geom_jitter(width = 0.4, color = "blue") +
  geom_jitter(width = 0.5, color = "red")

绘图参数相同,但现在沿x轴有两个可能的因子值。观察到虽然红点没有明显的中断(width = 0.5 类别y&amp;类别z,左侧和右侧有 附加 明确的间隙。这是扩张产生的空间:

set.seed(1) # set seed for consistent jitter values in each plot
p2

plot 1

将水平扩展设置为c(0, 0),间隙消失。现在红点一直到左/右边缘:

set.seed(1)
p2 +
  scale_x_discrete(expand = c(0, 0))

plot 2

expand = FALSE中设置coord_cartesian,垂直扩展也会消失。现在,红点一直向四个方向的边缘移动:

set.seed(1)
p2 +
  coord_cartesian(expand = FALSE)

plot 3

(在所有三种情况下,默认抖动宽度为0.4的蓝点不会到达边缘,因为它们只占据隐含箱的80%。

答案 1 :(得分:0)

您还需要更改oob选项。有关示例,请参阅http://www.hafro.is/~einarhj/education/ggplot2/scales.html(在页面上搜索oob。)