p值

2015-08-27 39 views
0

我用這個數據幀p值

library(reshape2) 
library(ggplot2) 
ID <- c("DJ45","DJ46","DJ47","DJ48","DJ49","DJ53","DJ54","DJ55","DJ56","DJ57") 
Tool <- c("Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_A", "Tool_B", "Tool_B", "Tool_B", "Tool_B", "Tool_B") 
Name <- c("CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP", "CMP") 
MS1 <- c(51,55,50,59,50,47,48,42,43,46) 
MS2 <- c(13,11,14,11,10,17,18,17,20,21) 
MS3 <- c(2,3,2,5,6,4,9,6,4,4) 
MS4 <- c(16,13,14,11,16,16,18,16,19,15) 
MS5 <- c(3,6,3,6,3,4,4,8,5,4) 
MS6 <- c(7,7,5,5,8,9,8,6,6,9) 

df1 <- data.frame(ID,Tool,Name,MS1,MS2,MS3,MS4,MS5,MS6) 
df2<-melt(df1,id.var=c("ID","Tool","Name")) 

p2 <- ggplot(df2, aes(x=factor(Tool),y=value,fill=factor(Tool)))+ 
     geom_boxplot() + labs(title="CMP") +facet_wrap(~variable) 
p2 

我們得到這個情節得到箱線圖,

enter image description here

我然後使用T-比較不同的工具測試並獲得像這樣的每個測量的p值。

t.test(MS1 ~ Tool, df1) 
t.test(MS2 ~ Tool, df1) 

我想包括每個組在圖中的p值。我提到this link,但困惑如何編碼到我的問題。請提供有關如何解決此問題的輸入/指導。

回答

2

也許您在初始aes調用中定義的fill事實導致其他解決方案失敗。在這種情況下,只需在撥打aesgeom_text中設置fill=NA即可。

## Make pvalue data 
dat <- data.frame(
    x=1.5, y=55, 
    pval=sapply(split(df2, df2$variable), function(x) t.test(value ~ Tool, x)$p.value), 
    variable=factor(paste0("MS", 1:6)) 
) 

## Plot 
p2 <- ggplot(df2, aes(x=factor(Tool),y=value,fill=factor(Tool)))+ 
    geom_boxplot() + labs(title="CMP") + 
    geom_text(data=dat, aes(x=x, y=y, label=round(pval, 3), fill=NA)) + 
    facet_wrap(~variable) 
+0

感謝您的解決方案。你能告訴我你爲什麼使用x = 1.5和y = 55嗎?這些是什麼意思? – Sharath

+0

@Sharath這些是數字文本的位置(由我任意選擇)。 – jenesaisquoi

+0

好的。感謝您的解釋。像魅力一樣工作。我試圖在數字前面加上「pvalue =」標籤。你能幫我做到嗎? – Sharath