2010-08-24 11 views
8

मैं एक खड़ी ggplot2 के साथ बनाया areaplot है:ggplot2: ग्राफ पैनल पर ओवरले नियंत्रण समूह लाइन सेट

dists.med.areaplot<-qplot(starttime,value,fill=dists,facets=~groupname, 
    geom='area',data=MDist.median, stat='identity') + 
    labs(y='median distances', x='time(s)', fill='Distance Types')+ 
    opts(title=subt) + 
    scale_fill_brewer(type='seq') + 
    facet_wrap(~groupname, ncol=2) + grect #grect adds the grey/white vertical bars 

यह इस तरह दिखता है: stacked area graph

मैं प्रोफ़ाइल का एक एक उपरिशायी जोड़ना चाहते हैं आउटपुट में सभी ग्राफों के लिए नियंत्रण ग्राफ (नीचे दाएं) का (समूह नाम == पंक्ति एच नियंत्रण है)।

अब तक अपना सर्वश्रेष्ठ प्रयास इस निकले हैं:

cline<-geom_line(aes(x=starttime,y=value), 
    data=subset(dists.med,groupname=='rowH'),colour='red') 

dists.med.areaplot + cline 

problem graph

मैं 3 लाल लाइनों की जरूरत है 1 लाल रेखा है कि गहरे नीले रंग अनुभाग के शीर्ष को स्किम किया जाना है। और मुझे प्रत्येक पैनल को ओवरले करने के लिए उस समान रेखा (पंक्ति रेखा) की आवश्यकता है।

dataframe इस तरह दिखता है:

> str(MDist.median) 
'data.frame': 2880 obs. of 6 variables: 
$ groupname: Factor w/ 8 levels "rowA","rowB",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ fCycle : Factor w/ 6 levels "predark","Cycle 1",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ fPhase : Factor w/ 2 levels "Light","Dark": 2 2 2 2 2 2 2 2 2 2 ... 
$ starttime: num 0.3 60 120 180 240 300 360 420 480 540 ... 
$ dists : Factor w/ 3 levels "inadist","smldist",..: 1 1 1 1 1 1 1 1 1 1 ... 
$ value : num 110 117 115 113 114 ... 

लाल रेखा प्रत्येक StartTime पर value का योग है, जहां समूहनाम = 'rowH' के रूप में गणना की जानी चाहिए। मैंने निम्नलिखित तरीकों से cline बनाने का प्रयास किया है। प्रत्येक त्रुटि या गलत आउटपुट में परिणाम:

#sums the entire y for all points and makes horizontal line 
cline<-geom_line(aes(x=starttime,y=sum(value)),data=subset(dists.med,groupname=='rowH'),colour='red') 

#using related dataset with pre-summed y's 
> cline<-geom_line(aes(x=starttime,y=tot_dist),data=subset(t.med,groupname=='rowH')) 
> dists.med.areaplot + cline 
Error in eval(expr, envir, enclos) : object 'dists' not found 

विचार?

ईटीए:

ऐसा प्रतीत होता है इस मुद्दे को मैं 'dists' not found साथ हो रही थी तथ्य यह है कि प्रारंभिक साजिश, dists.med.areaplot qplot के माध्यम से बनाया गया था के साथ क्या करना है। इस मुद्दे से बचने के लिए, मैं qplot पर नहीं बना सकता।

cline.data <- subset(
     ddply(MDist.median, .(starttime, groupname), summarize, value = sum(value)), 
     groupname == "rowH") 
cline<-geom_line(data=transform(cline.data,groupname=NULL), colour='red') 

dists.med.areaplot<-ggplot(MDist.median, aes(starttime, value)) + 
    grect + nogrid + 
    geom_area(aes(fill=dists),stat='identity') + 
    facet_grid(~groupname)+ scale_fill_brewer(type='seq') + 
    facet_wrap(~groupname, ncol=2) + 
    cline 

इस graphset में जिसके परिणामस्वरूप: alt text

उत्तर

3

लर्निंग आर ब्लॉग पोस्ट कुछ मदद की होनी चाहिए:

http://learnr.wordpress.com/2009/12/03/ggplot2-overplotting-in-a-faceted-scatterplot/

यह हो सकता है यह काम कर साजिश के लिए कोड है plyr के साथ ggplot के बाहर सारांश की गणना करने के लायक।

cline.data <- ddply(MDist.median, .(starttime, groupname), summarize, value = sum(value)) 
cline.data.subset <- subset(cline.data, groupname == "rowH") 

तो मुझे नहीं लगता कि आप `groupname` चर निकालना चाहते हैं

last_plot() + geom_line(data = transform(cline.data.subset, groupname = NULL), color = "red") 
+0

साथ साजिश में जोड़ें। – hadley

+0

यदि आप 'groupname' को हटाते हैं, तो क्या वह सभी पहलुओं पर रेखा को प्लॉट नहीं करेगा? – JoFrhwld

+0

हम्म, शायद मैंने सवाल को गलत समझा। – hadley

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