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:
I've been attempting to use themes but that's not working out so well. Thoughts?
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?
The + sign used to add layers must be placed at the end of each line containing a layer. If, instead, the + sign is added in the line before the other layer, ggplot2 will not add the new layer and will return an error message.
The default theme of a ggplot2 graph has a grey background color. You can easily and quickly change this to a white background color by using the theme functions, such as theme_bw() , theme_classic() , theme_minimal() or theme_light() (See ggplot2 themes gallery).
The term ggplot2 relates to the package's name. We use the function ggplot() to produce the plots when using the package. Therefore, ggplot() is the command, and the whole package is called ggplot2. It is a part of the R tidyverse, an ecosystem of packages designed with common APIs.
Background color of the plot area You can customize all the background color of the entire plot area with the bg argument of the par function. This will override all the background color of your plots unless you set back to the original graphical parameters.
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,
ymax = Inf,
fill = alpha("#000000", 0.2))
That will 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(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With