我在这里第一次代表。无法添加库(ggplot);希望那好吧。 我创建了一个乐队曼-奥特曼图。现在我想将平均值、标准差、上限和下限的计算结果写在图例上,如下所示:
。
只能用scale_color_manual()吗?
感谢您的帮助
df <- data.frame(A=c(494, 395, 516, 434, 476,
557, 413, 442, 650, 433,
417, 656, 267, 478, 178,
423, 427),
B=c(512, 430, 520, 428, 500,
600, 364, 380, 658, 445,
432, 626, 260, 477, 259,
350, 451))
#create new column for average measurement
df$avg <- rowMeans(df)
#create new column for difference in measurements
df$diff <- df$A - df$B
#mean difference
mean_diff <- mean(df$diff)
#- 95% confidence interval limits
lower <- mean_diff - 1.96*sd(df$diff)
upper <- mean_diff + 1.96*sd(df$diff)
#create Bland-Altman plot
ggplot(df, aes(avg, diff)) +
geom_point(size=3) +
geom_hline(yintercept = mean_diff,, linetype="dashed") +
geom_hline(yintercept = 0) +
geom_hline(yintercept = lower, color = "red", linetype="dashed") +
geom_hline(yintercept = upper, color = "red", linetype="dashed") +
ggtitle("Bland-Altman Plot") +
annotate("text", label ="Upper Limit", y=upper, x = 600) +
annotate("text", label ="Lower Limit", y=lower, x = 600) +
annotate("text", label ="Mean", y=mean_diff, x = 600) +
ylab("Difference Between Measurements") +
xlab("Average Measurement")
#> Error in ggplot(df, aes(avg, diff)): konnte Funktion "ggplot" nicht finden
创建于 2024 年 10 月 18 日,使用 reprex v2.1.1
您无法使用内置的 ggplot 比例创建您正在寻找的图例类型。这种类型的图例必须使用
annotate
: 手动创建
ggplot(df, aes(avg, diff)) +
geom_point(size = 3) +
geom_hline(yintercept = mean_diff,, linetype="dashed") +
geom_hline(yintercept = 0) +
geom_hline(yintercept = lower, color = "red", linetype="dashed") +
geom_hline(yintercept = upper, color = "red", linetype="dashed") +
ggtitle("Bland-Altman Plot") +
annotate("text", label ="Upper Limit", y=upper, x = 600, vjust = -0.5) +
annotate("text", label ="Lower Limit", y=lower, x = 600, vjust = -0.5) +
annotate("text", label ="Mean", y=mean_diff, x = 600, vjust = 1.5) +
ylab("Difference Between Measurements") +
xlab("Average Measurement") +
theme_bw() +
annotate(geom = "text", x = 500, y = 50, hjust = 0, parse = TRUE,
label = deparse(bquote(Mean[diff] == .(mean_diff)))) +
annotate(geom = "text", x = 500, y = 45, hjust = 0, parse = TRUE,
label = deparse(bquote(SD[diff] == .(sd(df$diff))))) +
annotate(geom = "text", x = 500, y = 40, hjust = 0, parse = TRUE,
label = deparse(bquote(LL == .(lower)))) +
annotate(geom = "text", x = 500, y = 35, hjust = 0, parse = TRUE,
label = deparse(bquote(UL == .(upper))))