R语言 ggplot可以在不引入随机噪声的情况下将重叠点隔开吗?

4xrmg8kj  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(83)

我的数据在x轴上是分类的,在y轴上是连续的。我正在尝试制作一个与此类似的情节:
x1c 0d1x的数据
我不知道如何得到的点,以避免重叠-既不是抖动,也不是道奇似乎是相当我正在寻找的。
下面是一个包含一些数据的示例:

A <- c(5.1, 5.2, 4.8)
B <- c(1.3, 2.8, 3.2)
C <- c(4.5, 4.5, 4.5)
D <- c(8.9, 7.6, 7.6)

example <- data.frame(A, B, C, D) %>%
              pivot_longer(c(A,B,C, D),
                           names_to = "Type", 
                           values_to = "Value", 
                           cols_vary = "slowest")

ggplot(example, aes(x = Type, y = Value, fill = Type)) +
  stat_summary(fun = "mean", 
               colour = "black", 
               size = 0.3,
               width = 0.4,
               geom = "crossbar") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar",
               linewidth = 0.8,
               width = 0.3,
               colour = "black") +
  geom_point(size = 3,
             shape = 21,
             #alpha = 0.5,
             colour = "black",
             stroke = 1)

字符串
它生成的图看起来像这样:


我希望能够看到C组和D组中的所有三个点,但我不想移动B组中的点。
我不想引入抖动-如果点不重叠,那么我希望他们保持中心,当他们重叠时,我希望他们均匀间隔。
Position_dodge有效,但适用于所有类别,而不是仅在需要时才应用。
使用geom_dotplot给出了最接近的结果,但我不希望值被分箱-y值的细微差异很重要,点需要在正确的y位置。
有没有办法在R中实现这一点?

jpfvwuh4

jpfvwuh41#

一种选择是使用geom_dotplot()来获得您想要的结果,例如:

library(tidyverse)

A <- c(5.1, 5.2, 4.8)
B <- c(1.3, 2.8, 3.2)
C <- c(4.5, 4.5, 4.5)
D <- c(8.9, 7.6, 7.6)

example <- data.frame(A, B, C, D) %>%
  pivot_longer(c(A,B,C, D),
               names_to = "Type", 
               values_to = "Value", 
               cols_vary = "slowest")

ggplot(example, aes(x = Type, y = Value, fill = Type)) +
  stat_summary(fun = "mean", 
               colour = "black", 
               size = 0.3,
               width = 0.4,
               geom = "crossbar") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar",
               linewidth = 0.8,
               width = 0.3,
               colour = "black") +
  geom_dotplot(stackdir = "center", 
               binaxis = "y", 
               binwidth = .2,
               binpositions = "all",
               stackratio = 1.25,
               dotsize = 1.5)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.

字符串
x1c 0d1x的数据
创建于2023-07-19,使用reprex v2.0.2
另一种可能的选择(更通用的解决方案)是使用来自ggbeeswarm packagegeom_beeswarm(),例如

library(tidyverse)
library(ggbeeswarm)

A <- c(5.1, 5.2, 4.8)
B <- c(1.3, 2.8, 3.2)
C <- c(4.5, 4.5, 4.5)
D <- c(8.9, 7.6, 7.6)

example <- data.frame(A, B, C, D) %>%
  pivot_longer(c(A,B,C, D),
               names_to = "Type", 
               values_to = "Value", 
               cols_vary = "slowest")

ggplot(example, aes(x = Type, y = Value, fill = Type)) +
  stat_summary(fun = "mean", 
               colour = "black", 
               size = 0.3,
               width = 0.4,
               geom = "crossbar") +
  stat_summary(fun.data = mean_sdl, 
               fun.args = list(mult = 1), 
               geom = "errorbar",
               linewidth = 0.8,
               width = 0.3,
               colour = "black") +
  geom_beeswarm(method = "centre",
                shape = 21,
                size = 4,
                cex = 4.5)
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: In `position_beeswarm`, method `center` discretizes the data axis (a.k.a the
#> continuous or non-grouped axis).
#> This may result in changes to the position of the points along that axis,
#> proportional to the value of `cex`.
#> This warning is displayed once per session.



创建于2023-07-19带有reprex v2.0.2

相关问题