gpt4 book ai didi

r - 使用 ggplot2 创建具有 95% CI 的平均加类时间变化的折线图

转载 作者:行者123 更新时间:2023-12-05 04:23:58 36 4
gpt4 key购买 nike

我的数据集是这样的

df <- data.frame(time = c(0,0,1,1,2,2),
mean = c(8, 6, 7 , 6, 6, 5),
Group = c('intervention','control','intervention','control','intervention','control'),
lower = c(7, 5, 5, 4, 4, 4),
upper = c(12, 9, 10, 8, 8, 8))

我想按组创建 3 个时间点的平均值变化图,每个时间点都有相应的 95% CI(下限、上限),类似这样,y 轴是平均值,并且x 轴是时间。

ggplot(df, aes(x = time, y = mean, color = Group)) +
geom_line(aes(lty = as.character(Group)),
position = position_dodge(width = 0.1)) +
geom_errorbar(aes(ymax = upper, ymin = lower), width = 0.1,
position = position_dodge(width = 0.1)) +
geom_point(aes(shape = as.character(Group)),
position = position_dodge(width = 0.1)) +
guides(lty = "none", shape = "none")+
theme_classic()

enter image description here

但是现在,我需要将时间变量更改为分类变量,而不是使用连续变量

df1 <- data.frame(time = c('baseline','baseline','week 1','week 1','week 2','week 2'),
mean = c(8, 6, 7 , 6, 6, 5),
Group = c('intervention','control','intervention','control','intervention','control'),
lower = c(7, 5, 5, 4, 4, 4),
upper = c(12, 9, 10, 8, 8, 8))

那条线消失了。我该如何解决? enter image description here

最佳答案

大概是这样?

ggplot(df, aes(x = time, y = mean, group = group)) +
geom_line(aes(lty = as.character(group)),
position = position_dodge(width = 0.1)) +
geom_errorbar(aes(ymax = upper, ymin = lower), width = 0.1,
position = position_dodge(width = 0.1)) +
geom_point(aes(shape = as.character(group)),
position = position_dodge(width = 0.1)) +
guides(lty = "none", shape = "none")

enter image description here

关于r - 使用 ggplot2 创建具有 95% CI 的平均加类时间变化的折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73595652/

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