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.
library(tidyverse)
library(tidyquant)
plotdata <- tq_get("AAPL", get = "stock.prices", from = "2020-01-01")
Hint: See the code in 4.2.2 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(size = 1.5,
color = "cornflowerblue")
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line(size = 1.5,
color = "cornflowerblue") +
labs(y = "Closing Price")
Hint: See the code in 4.2.2 Line plot.
ggplot(plotdata,
aes(x = date,
y = close)) +
geom_line(size = 1.5,
color = "cornflowerblue") +
labs(y = "Closing Price",
x = "")
Hint: Google search something like “ggplot2 two lines”.
plotdata2 <- tq_get("MSFT", get = "stock.prices", from = "2020-01-01")
ggplot(stock_prices) +
geom_line(data = plotdata, aes(x = date, y = close), color = "green") +
geom_line(data = plotdata2, aes(x = date, y = close), color = "blue") +
xlab('Date') +
ylab('Closing Price')
Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.