2013-01-30 4 views
14

Ho un set di dati (di prova), come indicato di seguito:Boxplot per gruppi?

Type Met1 Met2 Met3 Met4 
TypeA 65 43 97 77 
TypeA 46 25 76 77 
TypeA 44 23 55 46 
TypeA 46 44 55 77 
TypeA 33 22 55 54 
TypeB 66 8 66 47 
TypeB 55 76 66 65 
TypeB 55 77 88 46 
TypeB 36 67 55 44 
TypeB 67 55 76 65 

Ho controllato un sacco di link sulla scatola trame, ma ancora non sono riusciti per il tipo di diagramma a riquadri che voglio. Vorrei avere un boxplot con il mio asse X di tipo A (giallo, arancione) per tutti i Mets (Met1, Met2, Met3, Met4). In sostanza, voglio qualcosa di simile alla seguente (tratto da here): quarantina

enter image description here

sto cercando come,

boxplot(formula = len ~ Type , data = test, subset == "TypeA") 
boxplot(formula = len ~ Type , data = test, subset == "TypeA", add=TRUE) 
Legend(legend = c("typeA", "typeB"), fill = c("yellow", "orange")) 

Ma io non sono in grado di lavorare fuori con qualsiasi di esso . Qualcuno può aiutarmi a sapere come faccio a creare questi grafici sui miei dati di prova nel modo corretto?

+2

È necessario imparare a leggere attentamente il codice, ad esempio, 'sottoinsieme ==" TipoA "' chiaramente non è ciò che viene mostrato nell'esempio a cui è stato collegato. – Roland

risposta

19

Una soluzione con ggplot2.

In primo luogo, trasforma la tua cornice di dati test alla lunga formato utilizzando melt:

library(reshape2) 
test.m <- melt(test) 

tracciare i dati:

library(ggplot2) 
ggplot(test.m, aes(x = variable, y = value, fill = Type)) + 
    geom_boxplot() + 
    scale_fill_manual(values = c("yellow", "orange")) 

enter image description here

3

come questo,

test <- structure(list(Type = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
    2L, 2L, 2L), .Label = c("TypeA", "TypeB"), class = "factor"), 
    Met1 = c(65L, 46L, 44L, 46L, 33L, 66L, 55L, 55L, 36L, 67L 
    ), Met2 = c(43L, 25L, 23L, 44L, 22L, 8L, 76L, 77L, 67L, 55L 
    ), Met3 = c(97L, 76L, 55L, 55L, 55L, 66L, 66L, 88L, 55L, 
    76L), Met4 = c(77L, 77L, 46L, 77L, 54L, 47L, 65L, 46L, 44L, 
    65L)), .Names = c("Type", "Met1", "Met2", "Met3", "Met4"), 
    class = "data.frame", row.names = c(NA, -10L)) 


# install.packages("ggplot2", dependencies = TRUE) 
require(ggplot2) 
require(reshape2) 
df <- melt(test) 
p <- ggplot(df, aes(factor(variable), value)) + geom_boxplot(aes(fill = Type)) 
p 

enter image description here

si dà un'occhiata al the geom_boxplot manual page.

4

Come altri hanno già detto, è necessario prima fondere i dati.

df <- read.table(text="Type Met1 Met2 Met3 Met4 
TypeA 65 43 97 77 
TypeA 46 25 76 77 
TypeA 44 23 55 46 
TypeA 46 44 55 77 
TypeA 33 22 55 54 
TypeB 66 8 66 47 
TypeB 55 76 66 65 
TypeB 55 77 88 46 
TypeB 36 67 55 44 
TypeB 67 55 76 65",header=TRUE) 

library(reshape2) 
df2 <- melt(df) 

boxplot(
    formula = value ~ variable, 
    data = df2, 
    boxwex = 0.25, 
    at  = 1:4 - 0.2, 
    subset = Type == "TypeA", 
    col  = "yellow", 
    main = "blah", 
    xlab = "x", 
    ylab = "y", 
    ylim = c(0, ceiling(max(df2$value)) + 1), 
    yaxs = "i") 


boxplot(
    formula = value ~ variable, 
    data = df2, 
    boxwex = 0.25, 
    at  = 1:4 + 0.2, 
    subset = Type == "TypeB", 
    col  = "orange", 
    add  = TRUE) 
1

È possibile utilizzare la funzione reshape per fare una lunga colonna con tutti i dati da met1, .., colonne Met4. Crea inoltre una colonna (temp$time) per identificare da quale colonna derivano i dati che è possibile utilizzare per stratificare il diagramma a riquadri (temp$Type*temp$time).

df <- read.table(text= 
"Type Met1 Met2 Met3 Met4 
TypeA 65 43 97 77 
TypeA 46 25 76 77 
TypeA 44 23 55 46 
TypeA 46 44 55 77 
TypeA 33 22 55 54 
TypeB 66 8 66 47 
TypeB 55 76 66 65 
TypeB 55 77 88 46 
TypeB 36 67 55 44 
TypeB 67 55 76 65",header=TRUE) 

temp <- reshape(df, direction='long', varying = 2:5, sep='') 

boxplot(temp$Met ~ temp$Type*temp$time, col=c("yellow", "orange"))