gpt4 book ai didi

r - 使用 ggplot2 显示点和水平线(线段)

转载 作者:行者123 更新时间:2023-12-02 16:34:05 25 4
gpt4 key购买 nike

我想绘制三组数据。具体来说,我想显示单个数据点,包括三组的均值。这是我到目前为止所拥有的:

library(ggplot2)

df <- data.frame(group=rep(c("A", "B", "C"), each=10), value=rnorm(30))

ggplot(data=df, mapping=aes(x=group, y=value)) +
geom_point() +
stat_summary(fun="mean", geom="point", color="red", size=5) +
stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. + 0.25, yend=..y..))

这会产生下图:

This is what I have

但是,我希望水平线段从每组平均值的左侧开始,而不是从中心开始。我尝试指定 mapping=aes(x=..x.. - 0.25, xend=..x.. + 0.25, yend=..y..),但这只会给我一个错误:

Error: stat_summary requires the following missing aesthetics: x

我不明白为什么我不能使用 ..x.. 来指定 x 美学,而它适用于 xend一个。

知道如何使水平线段围绕组中心对称吗?

最佳答案

试试这个(也许不是最优雅的解决方案):

library(ggplot2)

df <- data.frame(group=rep(c("A", "B", "C"), each=10), value=rnorm(30))

ggplot(data=df, mapping=aes(x=group, y=value)) +
geom_point() +
stat_summary(fun="mean", geom="point", color="red", size=5) +
stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. - 0.25, yend=..y..))+
stat_summary(fun="mean", geom="segment", mapping=aes(xend=..x.. + 0.25, yend=..y..))

e chec

关于r - 使用 ggplot2 显示点和水平线(线段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63034893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com