具有 95% 置信区间带的折线图
Line graph with 95% confidence interval band
您好,我正在尝试制作一个折线图来显示特定日期之前治疗组和对照组的人平均每月费用的变化。我的数据看起来像这样
ID <- c("A", "B", "C", "D", "E", "F")
Treatment <- c(1, 1, 1, 0, 0 ,0)
last_3_month <- c(300, 300, 400, 0, 100, 20)
last_2_month <- c(400, 600, 500, 10, 40, 30)
last_1_month <- c(300, 600, 600, 20, 30, 40)
df <- data.frame(ID, Treatment, last_3_month, last_2_month, last_1_month)
ID Treatment last_3_month last_2_month last_1_month
1 A 1 300 400 300
2 B 1 300 600 600
3 C 1 400 500 600
4 D 0 0 10 20
5 E 0 100 40 30
6 F 0 20 30 40
除了每个月的平均成本外,我还想绘制每个月的 95% 置信区间。在这个例子中,平均值和间隔是:
Treatment last_3_month last_2_month last_1_month
0 40 26.67 30
[-91.45 171.45] [-11.28 64.61] [5.16 54.84]
1 333.33 500 500
[189.91 476.76] [251.59 748.41] [69.73 930.27]
其中每个单元格中的数字是均值,方括号中的数字是 95% 置信区间。我使用命令 t.test
.
计算了这些值
我想生成一个图表,其中 X 轴表示特定日期之前的月份,Y 轴表示成本。应该有两条线分别连接每月成本的平均值以及治疗组和对照组的 95% 置信区间。
非常感谢你在这方面的帮助。
这就是您使用 t.test 的输出要查找的内容吗?
您可以查看 Broom 包,它具有用于各种测试输出的整理器。
library(tidyverse)
tribble(
~treatment, ~measure, ~last_3_month, ~last_2_month, ~last_1_month,
0, "mean", 40, 26.67, 30,
0, "lower", -91.45, -11.28, 5.16,
0, "upper", 171.45, 64.61, 54.84,
1, "mean", 333.33, 500, 500,
1, "lower", 189.91, 251.59, 69.73,
1, "upper", 476.76, 748.41, 930.27
) |>
pivot_longer(-c(treatment, measure)) |>
pivot_wider(names_from = measure, values_from = value) |>
mutate(
name = factor(name),
treatment = str_c("Treatment ", treatment)
) |>
ggplot(aes(name, mean, colour = treatment, group = treatment)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey90") +
geom_line()
由 reprex package (v2.0.1)
于 2022-04-27 创建
您好,我正在尝试制作一个折线图来显示特定日期之前治疗组和对照组的人平均每月费用的变化。我的数据看起来像这样
ID <- c("A", "B", "C", "D", "E", "F")
Treatment <- c(1, 1, 1, 0, 0 ,0)
last_3_month <- c(300, 300, 400, 0, 100, 20)
last_2_month <- c(400, 600, 500, 10, 40, 30)
last_1_month <- c(300, 600, 600, 20, 30, 40)
df <- data.frame(ID, Treatment, last_3_month, last_2_month, last_1_month)
ID Treatment last_3_month last_2_month last_1_month
1 A 1 300 400 300
2 B 1 300 600 600
3 C 1 400 500 600
4 D 0 0 10 20
5 E 0 100 40 30
6 F 0 20 30 40
除了每个月的平均成本外,我还想绘制每个月的 95% 置信区间。在这个例子中,平均值和间隔是:
Treatment last_3_month last_2_month last_1_month
0 40 26.67 30
[-91.45 171.45] [-11.28 64.61] [5.16 54.84]
1 333.33 500 500
[189.91 476.76] [251.59 748.41] [69.73 930.27]
其中每个单元格中的数字是均值,方括号中的数字是 95% 置信区间。我使用命令 t.test
.
我想生成一个图表,其中 X 轴表示特定日期之前的月份,Y 轴表示成本。应该有两条线分别连接每月成本的平均值以及治疗组和对照组的 95% 置信区间。
非常感谢你在这方面的帮助。
这就是您使用 t.test 的输出要查找的内容吗?
您可以查看 Broom 包,它具有用于各种测试输出的整理器。
library(tidyverse)
tribble(
~treatment, ~measure, ~last_3_month, ~last_2_month, ~last_1_month,
0, "mean", 40, 26.67, 30,
0, "lower", -91.45, -11.28, 5.16,
0, "upper", 171.45, 64.61, 54.84,
1, "mean", 333.33, 500, 500,
1, "lower", 189.91, 251.59, 69.73,
1, "upper", 476.76, 748.41, 930.27
) |>
pivot_longer(-c(treatment, measure)) |>
pivot_wider(names_from = measure, values_from = value) |>
mutate(
name = factor(name),
treatment = str_c("Treatment ", treatment)
) |>
ggplot(aes(name, mean, colour = treatment, group = treatment)) +
geom_ribbon(aes(ymin = lower, ymax = upper), fill = "grey90") +
geom_line()
由 reprex package (v2.0.1)
于 2022-04-27 创建