2013-02-01 15 views
19

मैं इस कोड के साथ बनाया एक डेटा फ्रेम:ggplot का उपयोग कर एक पहलू रेखा-ग्राफ कैसे बनाएं?

require(reshape2) 
foo <- data.frame(abs(cbind(rnorm(3),rnorm(3, mean=.8),rnorm(3, mean=.9),rnorm(3, mean=1)))) 
qux <- data.frame(abs(cbind(rnorm(3),rnorm(3, mean=.3),rnorm(3, mean=.4),rnorm(1, mean=2)))) 
bar <- data.frame(abs(cbind(rnorm(3,mean=.4),rnorm(3, mean=.3),rnorm(3, mean=.9),rnorm(3, mean=1)))) 

colnames(foo) <- c("w","x","y","z") 
colnames(qux) <- c("w","x","y","z") 
colnames(bar) <- c("w","x","y","z") 

rownames(foo) <- c("n","q","r") 
rownames(qux) <- c("n","q","r") 
rownames(bar) <- c("n","q","r") 

foo <- cbind(ID=rownames(foo),foo) 
bar <- cbind(ID=rownames(bar),qux) 
qux <- cbind(ID=rownames(bar),qux) 

foo$fn <- "foo" 
qux$fn <- "qux" 
bar$fn <- "bar" 

alldf<-rbind(foo,qux,bar) 
alldf.m <- melt(alldf) 

मुझे क्या करना चाहते हैं पहलू प्रारूप में एक ggplot लाइन वक्र बनाने के लिए है, इसलिए वह इस तरह का ग्राफ बनाता है:

enter image description here

वास्तविक ग्राफ में ऊपर की रेखाएं नहीं हैं - यह केवल एक स्केच है ताकि लाइन अलगाव स्पष्ट हो।

मेरे वर्तमान कोड काम नहीं करता:

library(ggplot2) 
    p <- ggplot(data=alldf.m, aes(x=variable)) + 
      geom_line(aes(colour=ID),alpha=0.4) 
    p <- p + facet_wrap(~ fn) 
    p 

यह करने के लिए सबसे अच्छा तरीका क्या है?

+8

हाथों से बनाए गए ग्राफिक्स के उपयोग के लिए +1 – SlowLearner

उत्तर

16

इस प्रयास करें:

ggplot(data=alldf.m, aes(x=variable, y = value, colour = ID, group = ID)) + 
    geom_line() + facet_wrap(~fn) 

enter image description here

6

यहां तक ​​कि यह है एक ggplot2 ओपी के लिए आवश्यक है, लेकिन मुझे लगता है कि इस उदाहरण lattice भी लिए उपयुक्त है:

library(lattice) 
xyplot(data=alldf.m, value~variable|fn, type ='b', groups = ID, auto.key = T) 

enter image description here

औरका उपयोग करनाहम ggplot2 समाधान के लिए colse कुछ प्राप्त कर सकते हैं:

p <- xyplot(data=alldf.m, value~variable|fn, type ='b', groups = ID, auto.key = T) 
update(p , par.settings = ggplot2like()) 

enter image description here

संबंधित मुद्दे