In this exercise you will learn to plot data using the ggplot2 package. To this end, you will make your own note of 4.2 Quantitative vs. Quantitative from Data Visualization with R.

library(tidyverse)
library(tidyquant)
library(lubridate) # for year()

# Pick stocks
stocks <- c("AAPL", "MSFT")

# Import stock prices
stock_prices <- stocks %>%
  tq_get(get  = "stock.prices",
           from = "2019-01-01") 

stock_prices

Q1 filter Select Apple and save it under plotdata.

Hint: See the code in 4.2.2 Line plot.

library(dplyr)
plotdata <- filter(stock_prices, 
            symbol == "AAPL")

plotdata

Q2 Line plot Plot the relationship between time (date) and Apple stock prices (close).

Hint: See the code in 4.2.2 Line plot.

ggplot(plotdata, 
       aes(x = date, 
           y = close)) +
  geom_line() 

Q3 In what month did the Apple stock peak this year?.

Hint: See the line plot you created in the previous question.

Q4 Scatterplot Plot the relationship between closing price and volume for Apple.

Hint: See the code in 4.2.1 Scatterplot

ggplot(plotdata, 
       aes(x = date, 
           y = close)) +
  geom_line() 

Q5 Scatterplot Add the best fit line.

Hint: See the code in 4.2.1 Scatterplot

ggplot(plotdata,
       aes(x =close, 
           y = volume)) +
  geom_point(color= "steelblue") +
  geom_smooth(method = "lm")

Q6 Describe the relationship between closing price and volume for Apple.

Hint: See the scatterplot you created in the previous question.

Q7 Scatterplot Plot the relationship between closing price and volume for both Apple and Microsoft.

Hint: Use facet_wrap().

ggplot(stock_prices,
       aes(x =close, 
           y = volume)) +
  geom_point(color= "steelblue") +
  geom_smooth(method = "lm")+
  facet_wrap(~symbol)

Q8 Hide the messages, the code and its results on the webpage.

Hint: Use message, echo and results in the chunk options. Refer to the RMarkdown Reference Guide.

Q9 Display the title and your name correctly at the top of the webpage.

Q10 Use the correct slug.