Skip to content

Instantly share code, notes, and snippets.

@rich-iannone
Last active February 28, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rich-iannone/83e0575046c4fe38725a to your computer and use it in GitHub Desktop.
Save rich-iannone/83e0575046c4fe38725a to your computer and use it in GitHub Desktop.
Time series graph of S&P 500 data going back to 1950. Uses DiagrammeR for R.
devtools::install_github("rich-iannone/DiagrammeR")
library(DiagrammeR)
library(DiagrammeRsvg)
library(magrittr)
# Read in the data
sp500 <- read.csv(system.file("examples/sp500.csv",
package = "DiagrammeR"),
stringsAsFactors = FALSE)
# Create a series of (x, y) data
# points as an NDF
high <-
create_xy_pts(
series_label = "high",
x = sp500$Date[c(rep(FALSE,2), TRUE)],
y = sp500$High[c(rep(FALSE,2), TRUE)],
line_width = 2.0,
width = 0.02,
height = 0.02,
shape = "dot",
fill_color = "white",
line_color = "DeepSkyBlue")
# Create an (x, y) plot and save it as
# a PDF in the working directory
xy_graph <-
create_xy_graph(
high,
x_name = "Year",
y_name = "US Dollars",
heading = c("#S&P 500 data", "#####Published February 24, 2016"),
footer = c("###Source: Yahoo Finance (http://beta.finance.yahoo.com)",
"###Market closing values"),
xy_value_labels = c("date", "USD"),
aspect_ratio = c(1, 1),
include_legend = FALSE,
bg_color = "transparent")
# View the graph in the RStudio Viewer
xy_graph %>% render_graph
# Make a PDF
xy_graph %>% export_graph("sp500.pdf")
@rich-iannone
Copy link
Author

The chart as a PNG file.

sp500

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