如何将两个geom的x轴位置相对于彼此移动

我有一个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

您可以在eachstat_summary(aes(x = as.numeric(Cult)0.2))中为aes中的x添加偏移量:

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

https://stackoverflow.com/questions/42461993/how-to-shift-x-axis-positions-of-two-geoms-relative-to-each-other

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何将两个geom的x轴位置相对于彼此移动