geom_cycle: A geom that draws vertical bars based on given dates. The...

Description Usage Arguments See Also Examples

Description

A geom that draws vertical bars based on given dates. The default dates are recession_dates corresponding to dates for NBER recessions. The data also can use bear_dates

Usage

1
2
3
geom_cycle(dates = ggts::recession_dates, fill = "#003F87", alpha = 0.2,
  geom = "rect", position = "identity", show.legend = FALSE,
  inherit.aes = TRUE, ...)

Arguments

dates

(dataframe) a dataframe with the dates that will be used to create the vertical bars. The first column is the start of each cycle; the second column is the end of each cycle. Default is recession_dates

fill

(string) a string with the color used to fill the cycle bars (corresponds to ggplot2 fill)

alpha

(numeric) a number between 0 and 1 that specifies the transparency of the regression bars (corresponds to ggplot2 alpha)

geom

the ggplot2 geom to be plotted. Defaults to "rect"

position

the ggplot2 position. Defaults to "identity"

show.legend

Determines ggplot2 show legend behavior. Defaults to FALSE

inherit.aes

Speficies if the aes should be inherited from previous plot. Defaults to TRUE

...

Other parameters to be passed to the layer

See Also

ggts

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
data(AirPassengers)
##The defualt shading for geom_cycle is recession dates
ggts(AirPassengers) + geom_cycle()

##Apple stock prices originally in \code{xts} format
library(xts)
data(AAPL.data)  ##xts object
##Apple stock price with recessions shaded
ggts(AAPL.data, -dplyr::matches("Volume")) + geom_cycle()


##Apple stock price with recessions and bear markets shaded
##First, load the bear market dates and recession dates
data(bear_dates); data(recession_dates)
ggts(AAPL.data, -dplyr::matches("Volume")) + geom_cycle(dates = bear_dates)
ggts(AAPL.data, -dplyr::matches("Volume")) + geom_cycle(dates = recession_dates) +
   geom_cycle(dates = bear_dates, fill="gray50")




##Economics data
data(economics)
ggts(economics, psavert, uempmed) + geom_cycle()

##Using ggts_facet
ggts_facet(economics) + geom_cycle()
library(AER)
data(USMacroSW)
data(bear_dates)
ggts_facet(USMacroSW) + geom_cycle()
ggts_facet(USMacroSW) + geom_cycle(dates=bear_dates)
ggts_facet(USMacroSW) + geom_cycle() + geom_cycle(dates=bear_dates, fill="grey50")

##Can also be used with a ggplot not created by ggts
library(dplyr); library(tidyr)
econ.out <- economics %>%
   select(date, psavert, uempmed) %>%
   gather(variable, value, -date)
ggplot(econ.out, aes(x=date, y=value, group=variable)) +
   geom_line(aes(linetype=variable)) +
   geom_cycle()

ChandlerLutz/ggts documentation built on May 6, 2019, 9:56 a.m.