【问题标题】:ggplot2: overlay control group line on graph panel setggplot2:图形面板集上的覆盖控制组线
【发布时间】:2010-08-24 18:15:45
【问题描述】:

我有一个用 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

看起来像这样:

我想在输出中的所有图表中添加控制图(右下角)轮廓的叠加层(groupname==rowH 是控件)。

到目前为止,我的最大努力已经取得了这样的成果:

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

dists.med.areaplot + cline

我需要 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 ...

红线应计算为每个开始时间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

想法?

预计到达时间:

看来我遇到的'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

导致这个图集:

【问题讨论】:

    标签: r graph ggplot2


    【解决方案1】:

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

    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")   
    

    然后将其添加到情节中

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

    【讨论】:

    • 我认为您不想删除 groupname 变量。
    • 如果你删除groupname,那不是在所有方面都画线吗?
    • 嗯,也许我误解了这个问题。
    • @hadley:我确实想把 rowH 线放在每个方面。这样做的目的是让用户轻松了解治疗与对照的不同之处。
    • @JoFrhwld:我喜欢你制作 cline.data.subset 的方式。它很干净。不幸的是,当我按照您建议的方式绘制它时,我得到了错误:Error in eval(expr, envir, enclos) : object 'dists' not found. dists 是主数据集中包含变量名称并设置填充的列。出于某种原因geom_line() 正在关注它,但我不知道为什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多