r - 如何相对于彼此移动两个几何体的 x 轴位置

标签 r ggplot2

我有一个 ggplot阴谋。我需要相对于抖动点移动误差线。我的代码是:

data("cabbages", package = "MASS")

require("ggplot2")

pos_1 <- position_jitterdodge(
    jitter.width  = 0.25,
    jitter.height = 0,
    dodge.width   = 0.9
)

gg <-
    ggplot(data = cabbages,
           aes(
               x      = Cult,
               y      = HeadWt,
               colour = Cult,
               fill   = Cult
               )) +

    geom_jitter(alpha = 0.4, position = pos_1) +

    stat_summary(fun.y = "mean", geom = "point", size = 3) +

    stat_summary(fun.data = "mean_cl_normal",
                 geom = "errorbar",
                 width = 0.05,
                 lwd = 1,
                 fun.args = list(conf.int = 0.95)
) +

    theme_bw()

print(gg)

目前的结果是:

enter image description here

我需要这样的东西:

enter image description here

最佳答案

您可以向 x 添加偏移量在 aes在每个 stat_summary ( aes(x = as.numeric(Cult) + 0.2) ):

ggplot(data = cabbages,
       aes(x = Cult,
           y      = HeadWt,
           colour = Cult,
           fill   = Cult)) +
  geom_jitter(alpha = 0.4, position = pos_1) +
  stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.y = "mean", geom = "point", size = 3) +
  stat_summary(aes(x = as.numeric(Cult) + 0.2), fun.data = "mean_cl_normal",
               geom = "errorbar",
               width = 0.05,
               lwd = 1,
               fun.args = list(conf.int = 0.95)) +
  theme_bw()

enter image description here

关于r - 如何相对于彼此移动两个几何体的 x 轴位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461993/

相关文章:

r - 如何组合包含 NA 的一个 data.frame 中的列以删除 NA

r - 颜色只是 geom_bar 的顶部边框

r - geom_密度用facet_wrap创建扁平线

r - ggplot 每侧有 2 个 y 轴且具有不同的比例

r - 如何在 R 中的 nls(非线性最小二乘法)函数中使用 'weights'?

r - 重新分类栅格包中的 include.lowest 是什么意思 [r]

r - 在 ggplot 中对齐和排列图表

r - ggplot 更改某些图例项的大小

R 使用 NA 数据类型 dbl 创建 tibble 行

r - 在ggplot中拆分y轴