2011-10-21 20 views
5

Se ho alcuni dati e faccio un test ANOVA e post-hoc, come faccio a fare un boxplot che aggiunge automaticamente la classificazione post-hoc, piuttosto che dover modificare la figura all'esterno di R ?Etichette post-hoc su anova boxplot in R

Per esempio, ecco alcuni dati per iniziare:

install.packages("reshape", dependencies=T) 
library(reshape) 

x <- rnorm(30) 
y <- rnorm(30)+1 
z <- rnorm(30)+0.5 

data.1 <- data.frame(x, y, z) 
data.2 <- melt(data.1) 

Ecco il codice per l'esecuzione di un semplice ANOVA modo e tutti i test post-hoc confronto non pianificati:

linear.model <- lm(value~variable, data=data.2) 
anova(linear.model) 

# Analysis of Variance Table 
# Response: value 
#   Df Sum Sq Mean Sq F value Pr(>F) 
# variable 2 10.942 5.4710 5.8628 0.004087 ** 
# Residuals 87 81.185 0.9332  

TukeyHSD(aov(linear.model)) 

# Tukey multiple comparisons of means 
# 95% family-wise confidence level 
# Fit: aov(formula = linear.model) 
# $variable 
      # diff  lwr  upr  p adj 
# y-x 0.8344105 0.2396705 1.42915051 0.0034468 
# z-x 0.2593612 -0.3353788 0.85410126 0.5539050 
# z-y -0.5750493 -1.1697893 0.01969078 0.0602975 

A questo punto, voglio classificare x nel gruppo "a", y nel gruppo "b" e z nel gruppo "a, b". Posso creare un boxplot, ma come lo annotate con le lettere?

boxplot(value~variable, data=data.2) 

risposta

6

Se non ti dispiace utilizzando il pacchetto ggplot2, ecco come farei la figura:

In primo luogo, aggiungere una colonna per la cornice di dati (data.2) con le etichette di testo:

data.2$posthoc[data.2$variable == "x"] <- "a" 
data.2$posthoc[data.2$variable == "y"] <- "b" 
data.2$posthoc[data.2$variable == "z"] <- "a,b" 

Installare e caricare il pacchetto ggplot2:

install.packages("ggplot2", dependencies=T) 
library(ggplot2) 

di capire il codice per la figura, io edificherò nel passaggio S. In primo luogo solo tracciare i mezzi per ciascuno dei tre gruppi:

qplot(data=data.2, 
    x = variable, 
    y = value, 
    stat = "summary", 
    fun.y = "mean", 
    geom = c("point") 
    ) 

Avanti, aggiungere le etichette di testo:

qplot(data=data.2, 
    x = variable, 
    y = value, 
    stat = "summary", 
    fun.y = "mean", 
    label = posthoc, 
    vjust = -12, 
    geom = c("point", "text") 
    ) 

Infine, aggiungere il geom grafico a scatole e ripulirlo un po ':

qplot(data=data.2, 
    x = variable, 
    y = value, 
    stat = "summary", 
    fun.y = "mean", 
    label = posthoc, 
    vjust = -12, 
    ylim = c(-1, 3.5), 
    geom = c("point", "text"), 
    main="ggplot2 ANOVA boxplot" 
    ) + 
    geom_boxplot(aes(fill=posthoc)) + 
    theme_bw() 

R anova boxplot with labels

+0

Questo funziona! C'è un modo per fare la stessa annotazione nella grafica di base R? – user1006644

+1

Sì, ad esempio, 'text (x = 1: 3, y = 3, c (" a "," b "," b, c "))' dopo la funzione 'boxplot'. – rcs

+0

Grazie James & RCS! – user1006644

2

Questo sarebbe più semplice

library(reshape) 

x <- rnorm(30) 
y <- rnorm(30)+1 
z <- rnorm(30)+0.5 

data.1 <- data.frame(x, y, z) 
data.2 <- melt(data.1) 
data.2$newgroup = factor(data.2$variable,labels=c("a","b","ab")) # only line added 
boxplot(value~newgroup, data=data.2) 
+0

Grazie Dieter, questo è utile! – user1006644