Exercise 1: Create a line chart for the closing prices for all years, and a stock chart for the high/low/close prices for August 2013 in the Excel file S&P 500. (Pg. 117)
df <- read.csv("./data/sp500.csv")
df
line chart for the closing prices for all years
df$Date = as.Date(df$Date, format = "%m/%d/%y")
plot(type = "l",
x = df$Date,
y = df$Close,
col = "red",
xlab = "times",
ylab = "Price",
main = "Closing Price Chart")
stock chart for the high/low/close prices for August 2013
library(tidyquant)
## Loading required package: lubridate
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
## Loading required package: PerformanceAnalytics
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
##
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
##
## legend
## Loading required package: quantmod
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: tidyverse
## ── Attaching packages ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.1.0 ✔ purrr 0.3.0
## ✔ tibble 2.0.1 ✔ dplyr 0.8.0.1
## ✔ tidyr 0.8.2 ✔ stringr 1.3.1
## ✔ readr 1.3.1 ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ lubridate::date() masks base::date()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks xts::first()
## ✖ lubridate::intersect() masks base::intersect()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks xts::last()
## ✖ lubridate::setdiff() masks base::setdiff()
## ✖ lubridate::union() masks base::union()
aug13 <- df[(df$Date >= as.Date("2013-08-01")) & (df$Date <= as.Date("2013-08-31")), ]
aug13
aug13 %>%
ggplot(aes(x = Date, y = Close)) +
geom_candlestick(aes(open = Open, high = High, low = Low, close = Close),
color_up = "darkgreen", color_down = "darkred",
fill_up = "darkgreen", fill_down = "darkred") +
labs(title = "AAPL Candlestick Chart",
subtitle = "Zoomed in, Experimenting with Formatting",
y = "Closing Price", x = "") +
theme_tq()
## Warning: Ignoring unknown parameters: colour_up, colour_down
## Warning: Ignoring unknown parameters: colour_up, colour_down
## Warning: Computation failed in `stat_linerange_bc()`:
## argument "color_up" is missing, with no default