In this exercise you will learn to plot data using the ggplot2 package. To answer the questions below, use Chapter 4.3 Categorical vs. Quantitative Data Visualization with R.
# Load packages
library(tidyquant)
library(tidyverse)
# Import stock prices
stock_prices <- tq_get(c("AAPL", "MSFT"), get = "stock.prices", from = "2020-01-01")
Hint: See the code in 4.2.2 Line plot.
# Select US cases
library(dplyr)
plotdata <- filter(stock_prices,
symbol == "AAPL")
Hint: See the code in 4.2.2 Line plot.
# simple line plot
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line()
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line(color = "cornflowerblue")
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line(line = 3)
## Warning: Ignoring unknown parameters: line
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line() +
labs(y = "Closing Price")
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line() +
labs(x = "")
Hint: Google search something like “ggplot2 two lines”.
plotdata2 <- filter(stock_prices,
symbol == "MSFT")
ggplot() +
geom_line(data = plotdata, aes(x = date, y = close), color = "darkred") +
geom_line(data = plotdata2, aes(x = date,y = close), color="steelblue")
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.
knitr::opts_chunk$set(echo = TRUE, message = FALSE, results = "markup")