gpt4 book ai didi

r - ggplot2 在平淡的奥特曼图中为每个方面添加几何线

转载 作者:行者123 更新时间:2023-12-01 23:24:27 24 4
gpt4 key购买 nike

我有以下数据框

structure(list(Lightbox = c(84L, 67L, 80L, 63L, 76L, 66L, 79L, 
81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L,
84L, 67L, 80L, 63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L,
63L, 76L, 66L, 79L, 81L, 77L, 82L, 84L, 67L, 80L, 63L, 76L, 66L,
79L, 81L, 77L, 82L), variable = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("S1",
"S2", "S3", "S4", "S5"), class = "factor"), value = c(82L, 65L,
73L, 50L, 50L, 50L, 72L, 56L, 76L, 78L, 88L, 66L, 71L, 60L, 54L,
55L, 63L, 68L, 73L, 75L, 73L, 65L, 76L, 57L, 51L, 57L, 75L, 65L,
69L, 66L, 77L, 67L, 79L, 58L, 55L, 56L, 77L, 66L, 73L, 80L, 78L,
62L, 78L, 52L, 63L, 59L, 71L, 64L, 69L, 89L), mean = c(83, 66,
76.5, 56.5, 63, 58, 75.5, 68.5, 76.5, 80, 86, 66.5, 75.5, 61.5,
65, 60.5, 71, 74.5, 75, 78.5, 78.5, 66, 78, 60, 63.5, 61.5, 77,
73, 73, 74, 80.5, 67, 79.5, 60.5, 65.5, 61, 78, 73.5, 75, 81,
81, 64.5, 79, 57.5, 69.5, 62.5, 75, 72.5, 73, 85.5), diff = c(2L,
2L, 7L, 13L, 26L, 16L, 7L, 25L, 1L, 4L, -4L, 1L, 9L, 3L, 22L,
11L, 16L, 13L, 4L, 7L, 11L, 2L, 4L, 6L, 25L, 9L, 4L, 16L, 8L,
16L, 7L, 0L, 1L, 5L, 21L, 10L, 2L, 15L, 4L, 2L, 6L, 5L, 2L, 11L,
13L, 7L, 8L, 17L, 8L, -7L)), .Names = c("Lightbox", "variable",
"value", "mean", "diff"), row.names = c(NA, -50L), class = "data.frame")

我想绘制一个平淡的 altman 图,5 个方面组 S1->S5 与平均值的差异,这很容易

p <- ggplot(df_melt, aes(mean, diff))+ geom_point(na.rm=TRUE)+ facet_wrap(~variable)

不过,我还想在每个方面添加一些 geom_hline,显示每个组的平均值和标准差。如果我只有一组,我会执行以下操作:

yintercepts_mean <- c(mean(df_melt$diff, na.rm = TRUE))
yintercepts_mean_r <- round(yintercepts_mean,3)
yintercepts_sd_p <- c(mean(df_melt$diff, na.rm = TRUE) + c(2) * sd(df_melt$diff, na.rm = TRUE))
yintercepts_sd_n <- c(mean(df_melt$diff, na.rm = TRUE) + c(-2) * sd(df_melt$diff, na.rm = TRUE))
yintercepts_sd_p_r <- round(yintercepts_sd_p,3)
yintercepts_sd_n_r <- round(yintercepts_sd_n,3)

#ylabels <- c("- 2SD", "+ 2SD", "Mean")
ylabels <- c("mean")
ylabels2 <- c("+ 2SD")
ylabels3 <- c("- 2SD")

p + geom_hline(yintercept = yintercepts_mean_r, linetype=1, color='blue') +
geom_hline(yintercept = yintercepts_sd_p_r, linetype=2, color='blue') +
geom_hline(yintercept = yintercepts_sd_n_r, linetype=2, color='blue')

在对我的数据进行分面时,如何合并上述内容?

最佳答案

library(plyr)

df2 <- ddply(df_melt,.(variable),summarise,mean=mean(diff, na.rm = TRUE),
sd=sd(diff, na.rm = TRUE))

library(ggplot2)
p <- ggplot(df_melt, aes(mean, diff)) +
geom_point(na.rm=TRUE) +
geom_hline(data=df2,aes(yintercept=c(round(mean,3),
round(mean+2*sd,3),
round(mean-2*sd,3))),
linetype=c(1,2,2), color='blue') +
facet_wrap(~variable)

print(p)

enter image description here

关于r - ggplot2 在平淡的奥特曼图中为每个方面添加几何线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17187287/

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