tags:

views:

121

answers:

1

I want to added dark/light phase information to the background of my stacked area graphset to highlight the how light affects the shapes of the curves. My dataframe looks like this:

> 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 123 124 128 132 ...

example data:

> head(MDist.median)
  groupname  fCycle fPhase starttime   dists  value
1      rowA predark   Dark       0.3 inadist 110.00
2      rowA predark   Dark      60.0 inadist 123.25
3      rowA predark   Dark     120.0 inadist 124.10
4      rowA predark   Dark     180.0 inadist 128.35
5      rowA predark   Dark     240.0 inadist 131.80
6      rowA predark   Dark     300.0 inadist 140.30

My area plot stacks the 3 kinds of dists on the y axis by starttime on the x axis.

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='Changes in Fish Activity and Activity Type') + 
 scale_fill_brewer(type='seq')

I want to add vertical rectangles behind each plot that are horizontally positioned by the min and max starttimes of each fCycle where fPhase=="Dark". I edited my graphset in GIMP to show what I mean:

my altered graphset

I've been attempting to use themes but that's not working out so well. Thoughts?

ETA

So, using this example (from Hadley's website):

(unemp <- qplot(date, unemploy, data=economics, geom="line", 
  xlab = "", ylab = "No. unemployed (1000s)"))

presidential <- presidential[-(1:3), ]

yr <- range(economics$unemploy)
unemp + geom_rect(
  aes(NULL, NULL, xmin = start, xmax = end, fill = party), 
  ymin = yr[1], ymax = yr[2], data = presidential) + 
  scale_fill_manual(values = alpha(c("blue", "red"), 0.2)
)

I can do this with my data:

dists.med.areaplot + geom_rect(
      aes(NULL, NULL, xmin = phase_start, xmax = phase_end, fill = fPhase), 
      ymin = -Inf, ymax = Inf, data = phase_starts) + 
      scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2)
    )

which adds Light and Dark categories to the legend and changes ALL the colours to grey.

If I leave out the scale_fill_manual(values = alpha(c("#cccccc", "#000000"), 0.2), I get the rectangles I expect still with the legend change, but they are completely opaque, on top of my original graph, and use the 1st 2 colours in my colour sequence.

How can I make the geom_rect behind the original plots?

+3  A: 

With your updated code, I would take a subset of just the dark regions. Then add

geom_rect(data = dark.subset, aes(xmin = phase_start, xmax = phase_end), 
              ymin = -Inf,
              xmax = Inf,
              fill = alpha("#000000", 0.2))

That'll avoid mapping the light and dark to any scales. As for appearing on top, add your geoms in a different order:

ggplot(...)+
  geom_rect(...)+
  geom_area(...)
JoFrhwld