Learn R Programming

bdscale (version 1.1)

scale_x_bd: Weekend and holiday ignoring position scale for a ggplot.

Description

Weekend and holiday ignoring position scale for a ggplot.

Usage

scale_x_bd(..., business.dates, max.major.breaks = 5,
  max.minor.breaks = max.major.breaks * 5,
  breaks = bd_breaks(business.dates))

Arguments

...
other arguments passed to continuous_scale
business.dates
a vector of Date objects, sorted ascending
max.major.breaks
maximum major breaks bd_breaks will return, default=5
max.minor.breaks
maximum minor breaks bd_breaks will return, default=major*5
breaks
a function max => [date range] => breaks

Examples

Run this code
## Not run:

require(dplyr, quietly=TRUE, warn.conflicts=FALSE)
require(ggplot2, quietly=TRUE)
require(scales)

nyse <- yahoo()

set.seed(12345)
df <- data.frame(date=nyse, price=cumsum(rnorm(length(nyse))) + 100)

plot <- df %>% filter(as.Date('2014-08-01') <= date & date <= as.Date('2014-10-08')) %>% 
  ggplot(aes(x=date, y=price)) + geom_step() + 
  theme(axis.title.x=element_blank(), axis.title.y=element_blank())

plot + ggtitle('calendar dates')

# ggsave(file='man/figures/calendar.PNG', width=6, height=2)

plot + scale_x_bd(business.dates=nyse, labels=date_format("%b '%y")) + 
  ggtitle('business dates, month breaks')

# ggsave(file='man/figures/business.month.PNG', width=6, height=2)

plot + scale_x_bd(business.dates=nyse, max.major.breaks=10, labels=date_format('%b %d')) + 
  ggtitle('business dates, week breaks')

# ggsave(file='man/figures/business.week.PNG', width=6, height=2)

options <- c('2014-08-15', '2014-09-19') %>% as.Date

plot + 
  geom_vline(xintercept=as.numeric(options), size=2, alpha=0.25) + 
  ggtitle('calendar dates, option expiry')

# ggsave(file='man/figures/calendar.options.PNG', width=6, height=2)

plot + 
  geom_vline(xintercept=bd2t(options, business.dates=nyse), size=2, alpha=0.25) + 
  scale_x_bd(business.dates=nyse) +
  ggtitle('business dates, option expiry')

# ggsave(file='man/figures/business.options.PNG', width=6, height=2)

## End(Not run)

Run the code above in your browser using DataLab