tis (version 1.37.1)

nberShade.ggplot: Plotting NBER Recesssions in the ggplot graphics system

Description

nberShade.ggplot is a method for shading recession areas in a plot of class ggplot. It calls nberDates to get x coordinates for the areas to be shaded and then passes those coordinates along with its own arguments to geom_rect to do the shading. It may also draw a vertical line at the appropriate location if openShade is false and its xrange is of an appropiate size.

Usage

# S3 method for ggplot
nberShade(gg = ggplot2::last_plot(), fill = "grey80", color = NA, size = 0.5, 
                        alpha = 0.5, xrange = NULL, openShade = TRUE, ...)

Arguments

gg

A ggplot object that contains a mapping of y to its data. The default is to use the last ggplot object that was plotted.

fill

color to shade recessionary periods, passed to geom_rect.

color

The default (NA) omits borders on the shaded regions. Alternatively, specify a border color. Passed to geom_rect

size

A number specifying how thick should the border lines be. Passed to geom_rect

alpha

A number specifying how transparent the shaded regions should be. Passed to geom_rect

xrange

Horizontal range over which recession shading should be drawn. The default value NULL uses the entire range of the recession dates. xlim and scale_x_date are convenient ggplot2 functions to afterwards restrict the plot to a more sensible range.

openShade

governs how nberShade handles the case where the last row of the matrix returned by nberDates has an NA in the "End" column, indicating that the end date of the most recent recession is not yet known. If TRUE and xrange has been specified and extends far enough into the future it will replace the NA with xrange[2]. Else if openShade is TRUE and xrange is NULL then it will replace the NA with Sys.Date(). If openShade is FALSE then nberShade will draw a vertical line at the start of the most recent recession.

not used, present only to match generic

Value

Returns a ggplot object.

References

National Bureau of Economic Research. http://www.nber.org.

See Also

geom_rect, nberDates, nberShade

Examples

Run this code
# NOT RUN {
    
# }
# NOT RUN {
        require("ggplot2")
        ggp <- ggplot(aes(x=date, y=100*unemploy/pop), data=economics)

        nberShade( ggp ) + xlim(as.Date("1967-01-01"), Sys.Date()) + 
        opts(legend.position="none") + geom_line() + theme_bw()

        nberShade( ggp, fill = "yellow", color = "pink",                      
                xrange = c("1969-01-01", "2008-02-01"), openShade = FALSE) + 
        opts(legend.position="none") + geom_line(size=1) + theme_bw()
    
# }

Run the code above in your browser using DataLab