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)
## 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
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
## ══ Need to Learn tidyquant? ═════════════════════════════
## Business Science offers a 1-hour course - Learning Lab #9: Performance Analysis & Portfolio Optimization with tidyquant!
## </> Learn more at: https://university.business-science.io/p/learning-labs-pro </>
library(tidyverse)
## ── Attaching packages ──────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.2.1     ✓ purrr   0.3.3
## ✓ tibble  2.1.3     ✓ dplyr   0.8.4
## ✓ tidyr   1.0.2     ✓ stringr 1.4.0
## ✓ readr   1.3.1     ✓ forcats 0.4.0
## ── Conflicts ─────────────────── tidyverse_conflicts() ──
## x lubridate::as.difftime() masks base::as.difftime()
## x lubridate::date()        masks base::date()
## x dplyr::filter()          masks stats::filter()
## x dplyr::first()           masks xts::first()
## x lubridate::intersect()   masks base::intersect()
## x dplyr::lag()             masks stats::lag()
## x dplyr::last()            masks xts::last()
## x lubridate::setdiff()     masks base::setdiff()
## x lubridate::union()       masks base::union()
# Import stock prices
stock_prices <- tq_get(c("AAPL", "MSFT"), get  = "stock.prices", from = "2020-01-01")

Q1 Select Apple stock prices and save it under plotdata.

Hint: See the code in 4.2.2 Line plot.

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

Q2 Create a simple line plot with date on the x-axis and closing price on the y-axis.

Hint: See the code in 4.2.2 Line plot.

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

Q3 Add the color (cornflowerblue) to the line plot.

Hint: See the code in 4.2.2 Line plot.

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

Q4 Make the line thicker in the line plot.

 ggplot(plotdata, 
       aes(x = date, 
           y = close)) +
  geom_line(size = 1.5, color = "cornflowerblue") 

Q5 Label the y-axis as “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")

Q6 Remove the label of the x-axis.

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 = "")

Q7 Create the line plot for both Apple and Microsoft.

Hint: Google search something like “ggplot2 two lines”.

 ggplot(stock_prices, 
       aes(x = date, 
           y = close, 
        color = symbol)) +
  geom_line() 

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

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

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

Q10 Use the correct slug.