ggplot2:图形面板集上的叠加控制组线

dna*_*irl 8 r graph ggplot2

我有一个用ggplot2制作的堆叠areaplot:

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
Run Code Online (Sandbox Code Playgroud)

它看起来像这样: 堆积面积图

我想在控制图的配置文件(右下角)中添加一个叠加到输出中的所有图形(groupname == rowH是控件).

到目前为止,我的最大努力已经产生了这个:

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

dists.med.areaplot + cline
Run Code Online (Sandbox Code Playgroud)

问题图

我需要3条红线作为1条红线,掠过深蓝色部分的顶部.我需要相同的线(rowH线)来覆盖每个面板.

数据框如下所示:

> 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 ...
Run Code Online (Sandbox Code Playgroud)

红线应计算为value每个开始时间的总和,其中groupname ='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
Run Code Online (Sandbox Code Playgroud)

思考?

ETA:

看来,我遇到的问题与'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
Run Code Online (Sandbox Code Playgroud)

导致此图集: 替代文字

JoF*_*wld 3

这篇学习 R 博客文章应该会有所帮助:

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

ggplot在with之外计算摘要可能是值得的plyr

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

然后将其添加到绘图中

last_plot() + geom_line(data = transform(cline.data.subset, groupname = NULL), color = "red")
Run Code Online (Sandbox Code Playgroud)