2010-08-24 12 views
8

Mam ułożone areaplot wykonany z ggplot2:ggplot2: nakładka linia grupa kontrolna na panelu wykresu ustawić

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 

wygląda to tak: stacked area graph

Chcę dodać nakładkę profilu wykresu kontrolnego (w prawym dolnym rogu) do wszystkich wykresów na wyjściu (nazwa_grupy == wierszH jest formantem).

Dotychczas moje najlepsze wysiłki przyniosły tak:

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

dists.med.areaplot + cline 

problem graph

Muszę 3 czerwone linie być 1 czerwona linia że piana na szczyt ciemnoniebieskim sekcji. I potrzebuję tej samej linii (linii RowH), aby pokryć każdy z paneli.

dataframe wygląda następująco:

> 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 ... 

Czerwona linia powinna być obliczana jako suma value na każdym startTime, gdzie groupname = 'rowH'. Próbowałem tworzyć następujące sposoby. Każdy powoduje błąd lub niepoprawne wyjście:

#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 

Myśli?

ETA:

Wydaje się, że problem miałem z 'dists' not found ma do czynienia z faktem, że początkowa fabuła, dists.med.areaplot został stworzony przez qplot. Aby uniknąć tego problemu, nie mogę budować na qplot. Jest to kod na działce roboczej:

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 

skutkuje tym graphset: alt text

Odpowiedz

3

tego blogu Learning R powinny być pomocne:

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

to może być warto obliczyć podsumowanie poza ggplot z plyr.

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

następnie dodać go do działki z

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

nie sądzę chcesz usunąć zmienną groupname' '. – hadley

+0

Jeśli usuniesz 'nazwa_grupy', czy to nie będzie oznaczać linii we wszystkich aspektach? – JoFrhwld

+0

Hmm, może źle zrozumiałem pytanie. – hadley

Powiązane problemy