2010-08-24 21 views
8

Tôi có một areaplot xếp chồng lên nhau được thực hiện với ggplot2:ggplot2: overlay dòng nhóm đối chứng trên bảng đồ thị thiết lập

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 

Nó trông như thế này: stacked area graph

Tôi muốn thêm một lớp phủ của hồ sơ cá nhân của đồ thị điều khiển (dưới cùng bên phải) cho tất cả các đồ thị trong đầu ra (groupname == rowH là điều khiển).

Cho đến nay những nỗ lực tốt nhất của tôi đã mang lại điều này:

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

dists.med.areaplot + cline 

problem graph

tôi cần 3 đường màu đỏ là 1 đường đỏ mà lượm lặt trên cùng của phần màu xanh đậm. Và tôi cần dòng giống hệt nhau (dòng rowH) để phủ lên mỗi tấm.

Các dataframe trông như thế này:

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

Đường màu đỏ nên được tính bằng cách lấy tổng các value tại mỗi StartTime, nơi groupname = 'rowH'. Tôi đã thử tạo cline các cách sau. Mỗi kết quả có lỗi hoặc đầu ra không chính xác:

#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 

Suy nghĩ?

ETA:

Dường như vấn đề tôi gặp phải với 'dists' not found đã làm với thực tế là cốt truyện ban đầu, dists.med.areaplot đã được tạo ra thông qua qplot. Để tránh vấn đề này, tôi không thể xây dựng trên một qplot. Đây là mã cho những âm mưu làm việc:

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 

dẫn đến graphset này: alt text

Trả lời

3

bài đăng blog Learning R này cần được giúp đỡ một số:

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

Nó có thể là đáng để tính toán bản tóm tắt bên ngoài của ggplot với plyr.

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

Sau đó thêm nó vào cốt truyện với

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

Tôi không nghĩ rằng bạn muốn loại bỏ các biến 'groupname'. – hadley

+0

Nếu bạn loại bỏ 'groupname', sẽ không phải là sau đó vẽ đường trên tất cả các khía cạnh? – JoFrhwld

+0

Hmm, có lẽ tôi đã hiểu nhầm câu hỏi. – hadley

Các vấn đề liên quan