2013-01-30 10 views
14

मैं नीचे दिए गए के रूप में एक डाटासेट (परीक्षण) है:समूहों के लिए बॉक्सप्लॉट?

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 

मैं बॉक्स भूखंडों पर लिंक का एक बहुत कुछ जाँच की है, लेकिन मैं अभी भी बॉक्स साजिश के प्रकार मैं चाहता हूँ के लिए सफल नहीं है। मैं अपने एक्स-अक्ष के साथ सभी मेट्स (मेट 1, मेट 2, मेट 3, मेट 4) के लिए टाइप ए (पीला, नारंगी) वाला बॉक्सप्लॉट रखना चाहता हूं। संक्षेप में, मैं निम्नलिखित की तरह कुछ (here से लिया गया) हैं: जैसे

enter image description here

मैं कोशिश कर रहा हूँ somethings,

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

लेकिन मैं इसे यह से किसी के साथ बाहर काम करने में सक्षम नहीं हूँ । क्या कोई मुझे यह जानने में मदद कर सकता है कि मैं अपने परीक्षण डेटा पर सही तरीके से ऐसे बॉक्स प्लॉट कैसे बना सकता हूं?

+2

आपको कोड को ध्यान से पढ़ने के लिए सीखना होगा, उदाहरण के लिए, 'subset == "TypeA" स्पष्ट रूप से यह नहीं है कि वे आपके द्वारा लिंक किए गए उदाहरण में क्या दिखाते हैं। – Roland

उत्तर

19

ggplot2 के साथ एक समाधान।

पहले, लंबे प्रारूप melt का उपयोग कर के लिए अपने डेटा फ्रेम test परिणत: डेटा

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

प्लॉट:

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

इस तरह,

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

आप the geom_boxplot manual page पर एक नज़र डालें।

4

जैसा कि अन्य ने कहा है, सबसे पहले आपको अपना डेटा पिघलना होगा।

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

आप Met1, .., Met4 स्तंभों से सभी डेटा के साथ एक लंबे स्तंभ बनाने के लिए reshape फ़ंक्शन का उपयोग कर सकते हैं। यह एक कॉलम (temp$time) भी बनाता है यह पता लगाने के लिए कि डेटा किस कॉलम से आया था, जिसका उपयोग आप अपने बॉक्स प्लॉट (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")) 
संबंधित मुद्दे