我有一个 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)
目前的结果是:
我需要这样的东西:
最佳答案
您可以向 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()
关于r - 如何相对于彼此移动两个几何体的 x 轴位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42461993/