Hi:
You need to make two calls to geom_rect(), and since you're using a
time series, you need to set the x limits as the numeric equivalents
of the dates. Here's a reproducible example:
DF <- data.frame(date = seq(as.Date('1950-01-01'), as.Date('2012-06-01'),
by = 'months'),
y = rnorm(750))
# Check the numeric values of certain dates:
range(as.numeric(DF$date))
as.numeric(as.Date('2001-01-01'))
ggplot(DF, aes(date, y)) +
geom_rect(xmin = -7305, xmax = 11322, ymin = -10, ymax = 10,
fill = 'red') +
geom_rect(xmin = 11323, xmax = 15492, ymin = -10, ymax = 10,
fill = 'green') +
geom_line() +
scale_x_date(expand = c(0, 0))
You weren't terribly explicit about the way you wanted the rectangles
to be drawn. From Jan 1950 to Dec 2000, I set background rectangle in
red; from Jan 2001 forward to Jun 2012, the background is in green. If
you want to reset the boundaries between red and green, first find the
numeric equivalent of the boundary date you want (as shown above) and
then substitute appropriately for xmax in the first rectangle and xmin
in the second. The y-limits are set to ensure that the vertical extent
encompasses the data range (which here is about -4 to 4).
The rectangles are drawn first so that they'll stay in the background.
If you plot the series first, the fill color of the rectangles will
overwrite the series.
I tried to set an alpha transparency to mute the rather loud
background, but the several things I tried had no effect.
HTH,
Dennis
On Thu, Jun 14, 2012 at 9:31 PM, tom joad <
tomj...@gmail.com> wrote:
> Could I make a figure without two background theme or something similar?
>
> For example, I have some time series data from year 1950 to 2012.
> I want the background for year 2000 to be of color red while
> the background after year 2000 be of color green.
>
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
>
https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email
ggp...@googlegroups.com
> To unsubscribe: email
ggplot2+u...@googlegroups.com
> More options:
http://groups.google.com/group/ggplot2