Skip to content

Instantly share code, notes, and snippets.

@fearofcode
Created March 19, 2013 20:27
Show Gist options
  • Save fearofcode/5199796 to your computer and use it in GitHub Desktop.
Save fearofcode/5199796 to your computer and use it in GitHub Desktop.
Sample usage of ggplot2 to highlight timeseries by color. http://t.co/DDfe8eWZkY for sample image of what it looks like when used with plausible CSV data
library(ggplot2)
eurusd <- read.table("/home/warren/Downloads/EURUSD_Candlestick_1_D_BID_01.03.2007-16.03.2013.csv", sep=",", header=T)
eurusd$Time <- as.Date(eurusd$Time, "%d.%m.%Y %H:00:00.000")
long_trades = read.table(textConnection(
"Open, Close
2009-02-02, 2009-02-10
2009-04-05, 2009-06-07
2013-01-01, 2013-01-25"), sep=',',
colClasses=c('Date', 'Date'), header=TRUE)
short_trades = read.table(textConnection(
"Open, Close
2009-03-05, 2009-03-18
2009-07-08, 2009-09-10
2012-02-03, 2012-04-05"), sep=',',
colClasses=c('Date', 'Date'), header=TRUE)
g <- ggplot(eurusd) + geom_line(aes(x=Time, y=Open)) + theme_bw()
g <- g + geom_rect(data=long_trades, aes(xmin=Open, xmax=Close, ymin=-Inf, ymax=+Inf), fill='green', alpha=0.2)
g <- g + geom_rect(data=short_trades, aes(xmin=Open, xmax=Close, ymin=-Inf, ymax=+Inf), fill='red', alpha=0.2)
@fearofcode
Copy link
Author

This idea was directly borrowed from http://jeffreybreen.wordpress.com/2011/08/15/recession-bars/ .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment