ggplot2在平淡的altman阴谋的每个方面添加几何线 [英] ggplot2 Add geom line for each facet in bland altman plot

查看:113
本文介绍了ggplot2在平淡的altman阴谋的每个方面添加几何线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框架:

  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 (1L,1L,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,5L,5L,5L,5L,5L,5L,5L,5L,5L,5L)。标签= c(S1,
S2,S3 (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 (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),summarize,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)


I have the following data frame

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")

I wish to plot a bland altman graph, difference against mean for 5 facet groups S1->S5 which is easy enough

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

However, I would also like to add some geom_hline to each facet showing the mean for each group and the standard deviations. If I had only one group I would do the following:

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') 

How can I incorporate the above when facetting my data?

解决方案

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)

这篇关于ggplot2在平淡的altman阴谋的每个方面添加几何线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆