2013-06-19 43 views
3

我有以下的數據幀GGPLOT2添加的geom線在溫和的不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, 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") 

我要繪製平淡奧特曼圖表,針對平均差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') 

我怎麼能磨製我的數據時,包含上述?

回答

5
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

相關問題