gpt4 book ai didi

r - ggplot 抖动 geom_errorbar?

转载 作者:行者123 更新时间:2023-12-04 06:56:59 27 4
gpt4 key购买 nike

我的数据看起来像这样:

df1 <-
structure(
list(
y = c(-0.19, 0.3,-0.05, 0.15,-0.05, 0.15),
lb = c(-0.61,
0.1,-0.19,-0.06,-0.19,-0.06),
ub = c(0.22, 0.51, 0.09, 0.36,
0.09, 0.36),
x = structure(
c(1L, 2L, 1L, 2L, 1L, 2L),
.Label = c("X1",
"X2"),
class = "factor"
),
Group = c("A", "A", "B", "B", "C",
"C")
),
.Names = c("y", "lb", "ub", "x", "Group"),
row.names = c(NA,-6L),
class = "data.frame"
)

我想用 ggplot2绘制点 x,ygroup 着色带有误差线 lb, ub .因为 x是离散的,我要 jitter所以点和条不重叠。现在,我可以 jitter点而不是线。另外,我希望点的顺序是 A、B、C
ggplot(data = df1, aes(x, y, color = Group)) + geom_point(size = 4, position = "jitter") +
geom_errorbar(
aes(ymin = lb, ymax = ub),
width = 0.1,
linetype = "dotted"
) +
geom_hline(aes(yintercept = 0), linetype = "dashed") + theme_bw()

enter image description here

最佳答案

您可以使用 position_dodge实现所需的顺序和在点位置绘制的误差线

ggplot(data = df1, aes(x, y, color = Group)) +
geom_point(size = 4, position=position_dodge(width=0.5)) +
geom_errorbar(
aes(ymin = lb, ymax = ub),
width = 0.1,
linetype = "dotted",
position=position_dodge(width=0.5)) +
geom_hline(aes(yintercept = 0), linetype = "dashed") +
theme_bw()

enter image description here

关于r - ggplot 抖动 geom_errorbar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35654364/

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