Note that there are some explanatory texts on larger screens.

plurals
  1. POggplot2: overlay control group line on graph panel set
    primarykey
    data
    text
    <p>I have a stacked areaplot made with ggplot2:</p> <pre><code>dists.med.areaplot&lt;-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 </code></pre> <p>It looks like this: <img src="https://i.stack.imgur.com/sNFRS.png" alt="stacked area graph"></p> <p>I want to add a an overlay of the profile of the control graph (bottom right) to all the graphs in the output (groupname==rowH is the control). </p> <p>So far my best efforts have yielded this:</p> <pre><code>cline&lt;-geom_line(aes(x=starttime,y=value), data=subset(dists.med,groupname=='rowH'),colour='red') dists.med.areaplot + cline </code></pre> <p><img src="https://i.stack.imgur.com/IDDyb.png" alt="problem graph"></p> <p>I need the 3 red lines to be 1 red line that skims the top of the dark blue section. And I need that identical line (the rowH line) to overlay each of the panels.</p> <p>The dataframe looks like this:</p> <pre><code>&gt; 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 ... </code></pre> <p>The red line should be calculated as the sum of the <code>value</code> at each starttime, where groupname='rowH'. I have tried creating <code>cline</code> the following ways. Each results in an error or incorrect output:</p> <pre><code>#sums the entire y for all points and makes horizontal line cline&lt;-geom_line(aes(x=starttime,y=sum(value)),data=subset(dists.med,groupname=='rowH'),colour='red') #using related dataset with pre-summed y's &gt; cline&lt;-geom_line(aes(x=starttime,y=tot_dist),data=subset(t.med,groupname=='rowH')) &gt; dists.med.areaplot + cline Error in eval(expr, envir, enclos) : object 'dists' not found </code></pre> <p>Thoughts?</p> <h1>ETA:</h1> <p>It appears that the issue I was having with <code>'dists' not found</code> has to do with the fact that the initial plot, <code>dists.med.areaplot</code> was created via qplot. To avoid this issue, I can't build on a qplot. This is the code for the working plot:</p> <pre><code>cline.data &lt;- subset( ddply(MDist.median, .(starttime, groupname), summarize, value = sum(value)), groupname == "rowH") cline&lt;-geom_line(data=transform(cline.data,groupname=NULL), colour='red') dists.med.areaplot&lt;-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 </code></pre> <p>resulting in this graphset: <img src="https://i.stack.imgur.com/Uq8CG.png" alt="alt text"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload