r - 如何设置中位横线以在因子内对齐?

标签 r ggplot2 scatter-plot

我有一个像这样的数据框:

my_df <- structure(list(SampleID = c("sample01", "sample02", "sample03", 
"sample04", "sample05", "sample06", "sample07", "sample08", "sample09", 
"sample10", "sample11", "sample12", "sample13", "sample14", "sample15", 
"sample16", "sample17", "sample18", "sample19", "sample20"), 
    y = c(1.68547922357333, 0.717650914301956, 1.18156420566867, 
    1.31643130248052, 1.2021341615705, 0.946937741954258, 1.75576099871947, 
    0.952670480793451, 2.00921185693852, 0.968642950473789, 1.65243482711174, 
    2.14332269635055, 0.30556964944383, 0.860605616591314, 0.933339331803171, 
    1.31797519903504, 0.857873539291964, -0.328227710452388, 
    -0.22023346428776, 1.6600566728651), week = structure(c(1L, 
    2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 3L, 1L, 2L, 
    3L, 1L, 2L, 3L), .Label = c("0", "3", "6"), class = "factor"), 
    grumpy = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 
    2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L), .Label = c("No", 
    "Yes"), class = "factor"), week_grumpy = structure(c(2L, 
    4L, 6L, 2L, 4L, 6L, 1L, 3L, 5L, 2L, 4L, 6L, 1L, 5L, 2L, 4L, 
    6L, 1L, 3L, 5L), .Label = c("0 No", "0 Yes", "3 No", "3 Yes", 
    "6 No", "6 Yes"), class = "factor")), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -20L))

#packages needed if you don't have
install.packages("ggbeeswarm")
install.packages("ggplot2")

这通常是我绘制图表的方式:

library(ggplot2)
library(ggbeeswarm)

ggplot(data = my_df, aes(x=week, y=y, color=grumpy)) +
    geom_quasirandom(dodge.width = 0.75)

Typical plot

这很好,因为它可以很好地分离颜色。现在,我喜欢添加一个中线横杆来进一步显示组之间的差异。就像这样:

ggplot(data = my_df, aes(x=week, y=y, color=grumpy)) +
    geom_quasirandom(dodge.width = 0.75) +
    stat_summary(aes(group = grumpy), fun = median, fun.min = median, fun.max = median, geom = "crossbar", color = "black", width = 0.7, lwd = 0.2)

typical plot with median lines

现在,我想要的是中位横线与 x 轴上每个因子内的颜色对齐。有没有办法在 R 中做到这一点?或者我只能手动编辑横杆来排列?

这是我尝试过的一件事:

ggplot(data = my_df, aes(x=week_grumpy, y=y, color=grumpy)) +
    geom_jitter(width = 0.1) +
    stat_summary(aes(group = grumpy), fun = median, fun.min = median, fun.max = median, geom = "crossbar", color = "black", width = 0.7, lwd = 0.2)

median bars lined up but x-axis messy

但现在 x 轴不是我想要的方式(但是,在 Inkscape 等中手动编辑会比前面的示例更容易)。

我发现了一些提示herehere但尚未达成令人满意的解决方案。

最佳答案

您正在寻找的是躲避横杆几何。例如:

ggplot(data = my_df, aes(x=week, y=y, color=grumpy)) +
  geom_quasirandom(dodge.width = 0.75) +
  stat_summary(
    aes(group = grumpy), fun = median, fun.min = median, fun.max = median,
    geom = "crossbar", color = "black", width = 0.7, lwd = 0.2,

    # add this bit here to your stat_summary function
    position=position_dodge(width=0.75)
  )

enter image description here

看起来geom_quasirandom()在这里的行为与geom_point(position=position_jitterdodge(dodge.width=0.75))非常相似。在本例中,由于在 geom_quasirandom() 中指定了 dodge.width,因此您可以对 position_dodge 使用相同的 width在横梁几何中。

注意:您可能想要尝试一下美观的格式,以便能够更清楚地区分横杆告诉您的内容,但这应该可以回答您的问题。

关于r - 如何设置中位横线以在因子内对齐?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63674531/

相关文章:

r - 如何一次为所有点设置ggplot alpha(透明度)值

ios - 多个绘图空间或移动的轴/图

python - Matplotlib 散点标记大小

r - S4 方法参数的延迟评估

r - 一种更优雅的方式,将两个向量组合为单独的列(或数据帧),匹配行,并在不匹配的地方具有 NA

r 如何为图表设置 par ("usr")

r - N=50 和 K=3 的所有不同组合

r - 水平 ggplot - 如何在条内定位数据标签

将 stat_ellipse 限制为某些数据点

Python根据距离均匀分散固定中心周围的坐标