MyNixOS website logo
Description

Remove Weekends and Holidays from ggplot2 Axes.

Provides a continuous date scale, omitting weekends and holidays.

bdscale

CRAN_Status_Badge Build Status Coverage Status

Remove Weekends and Holidays From ggplot2 Axes

Find valid dates

Ask Yahoo Finance for S&P prices, use those as past NYSE trading dates. Then create some fake prices, put them into a data.frame alongside the dates:

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

Plot on standard calendar-day axis:

Create a plot:

library(dplyr)
library(ggplot2)

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')

Note the large gap at the beginning of September, because Labor Day was on the 1st:

calendar dates

Plot on a business-day axis:

Plot against scale_x_bd instead:

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

Removes weekends and holidays from the graph:

business dates, month breaks

The major breaks are pretty far apart, on the first trading day of each month.

It's a wide chart, tell it to use more breaks:

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

Given that max, it determines it can put major breaks on the first trading day of the week, and minor breaks every day:

business dates, week breaks

Translating into business-day space

Say I wanted to put vertical lines on option expiration dates.

Calling as.numeric on my dates translates them into the the number of calendar days after the unix epoch, which is what scale_x_date uses (see scales:::from_date):

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')

calendar dates with options

This doesn't work for business-day space because the x-axis now represents the number of business days after the first date in your business.dates vector.

Instead, use the bdscale::bd2t function to translate into business-day space:

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')

business dates with options

Metadata

Version

2.0.0

License

Unknown

Platforms (77)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows