options(scipen = 999)

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("WMT", "TGT", "AMZN"), get  = "stock.prices", from = "2020-01-01")

# Calculate daily returns
stock_returns <-
  stock_prices  %>%
    group_by(symbol) %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
stock_returns
## # A tibble: 114 x 9
## # Groups:   symbol [3]
##    symbol date        open  high   low close  volume adjusted daily.returns
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>         <dbl>
##  1 WMT    2020-01-02  119.  120.  119.  119. 6764900     119.       0      
##  2 WMT    2020-01-03  118.  119.  118.  118. 5399200     118.      -0.00883
##  3 WMT    2020-01-06  117.  118.  117.  118. 6445500     118.      -0.00204
##  4 WMT    2020-01-07  117.  118.  116.  117. 6846900     117.      -0.00926
##  5 WMT    2020-01-08  116.  117.  116.  116. 5875800     116.      -0.00343
##  6 WMT    2020-01-09  116.  117.  116.  117. 5563700     117.       0.0103 
##  7 WMT    2020-01-10  117.  117.  116.  116. 6054800     116.      -0.00835
##  8 WMT    2020-01-13  116.  117.  115.  116. 6112600     116.      -0.00430
##  9 WMT    2020-01-14  115.  116.  115.  116. 6585800     116.       0.00259
## 10 WMT    2020-01-15  115.  116.  115.  115. 7454200     115.      -0.00775
## # … with 104 more rows
Walmart <- tq_get("WMT", get = "stock.prices", from = "2020-01-01")
Target <- tq_get("TGT", get = "stock.prices", from = "2020-01-01")
Amazon <- tq_get("AMZN", get = "stock.prices", from = "2020-01-01")
Walmart2 <-
  Walmart  %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
stock_returns
## # A tibble: 114 x 9
## # Groups:   symbol [3]
##    symbol date        open  high   low close  volume adjusted daily.returns
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>         <dbl>
##  1 WMT    2020-01-02  119.  120.  119.  119. 6764900     119.       0      
##  2 WMT    2020-01-03  118.  119.  118.  118. 5399200     118.      -0.00883
##  3 WMT    2020-01-06  117.  118.  117.  118. 6445500     118.      -0.00204
##  4 WMT    2020-01-07  117.  118.  116.  117. 6846900     117.      -0.00926
##  5 WMT    2020-01-08  116.  117.  116.  116. 5875800     116.      -0.00343
##  6 WMT    2020-01-09  116.  117.  116.  117. 5563700     117.       0.0103 
##  7 WMT    2020-01-10  117.  117.  116.  116. 6054800     116.      -0.00835
##  8 WMT    2020-01-13  116.  117.  115.  116. 6112600     116.      -0.00430
##  9 WMT    2020-01-14  115.  116.  115.  116. 6585800     116.       0.00259
## 10 WMT    2020-01-15  115.  116.  115.  115. 7454200     115.      -0.00775
## # … with 104 more rows
Target2 <-
  Target  %>%
  tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
stock_returns
## # A tibble: 114 x 9
## # Groups:   symbol [3]
##    symbol date        open  high   low close  volume adjusted daily.returns
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>         <dbl>
##  1 WMT    2020-01-02  119.  120.  119.  119. 6764900     119.       0      
##  2 WMT    2020-01-03  118.  119.  118.  118. 5399200     118.      -0.00883
##  3 WMT    2020-01-06  117.  118.  117.  118. 6445500     118.      -0.00204
##  4 WMT    2020-01-07  117.  118.  116.  117. 6846900     117.      -0.00926
##  5 WMT    2020-01-08  116.  117.  116.  116. 5875800     116.      -0.00343
##  6 WMT    2020-01-09  116.  117.  116.  117. 5563700     117.       0.0103 
##  7 WMT    2020-01-10  117.  117.  116.  116. 6054800     116.      -0.00835
##  8 WMT    2020-01-13  116.  117.  115.  116. 6112600     116.      -0.00430
##  9 WMT    2020-01-14  115.  116.  115.  116. 6585800     116.       0.00259
## 10 WMT    2020-01-15  115.  116.  115.  115. 7454200     115.      -0.00775
## # … with 104 more rows
Amazon2 <-
  Amazon  %>%
    tq_mutate(select = adjusted, mutate_fun = periodReturn, period = "daily") 
stock_returns
## # A tibble: 114 x 9
## # Groups:   symbol [3]
##    symbol date        open  high   low close  volume adjusted daily.returns
##    <chr>  <date>     <dbl> <dbl> <dbl> <dbl>   <dbl>    <dbl>         <dbl>
##  1 WMT    2020-01-02  119.  120.  119.  119. 6764900     119.       0      
##  2 WMT    2020-01-03  118.  119.  118.  118. 5399200     118.      -0.00883
##  3 WMT    2020-01-06  117.  118.  117.  118. 6445500     118.      -0.00204
##  4 WMT    2020-01-07  117.  118.  116.  117. 6846900     117.      -0.00926
##  5 WMT    2020-01-08  116.  117.  116.  116. 5875800     116.      -0.00343
##  6 WMT    2020-01-09  116.  117.  116.  117. 5563700     117.       0.0103 
##  7 WMT    2020-01-10  117.  117.  116.  116. 6054800     116.      -0.00835
##  8 WMT    2020-01-13  116.  117.  115.  116. 6112600     116.      -0.00430
##  9 WMT    2020-01-14  115.  116.  115.  116. 6585800     116.       0.00259
## 10 WMT    2020-01-15  115.  116.  115.  115. 7454200     115.      -0.00775
## # … with 104 more rows

Q1 filter Select stock returns of January 31, 2020.

Hint: See the code in 1.2.2 Selecting observations.

plotdata <- filter(stock_returns,
                   date == "2020-01-31")

Q2 Which of the three stocks performed best on January 31, 2020?

Hint: Answer the question by comparing daily returns of each stock on January 31, 2020.

Of the three stocks Amazon performed the best on January 31st because their daily return is the only one that is not negative. Not to mention all of the other stocks prices are significantly below Amazon’s.

Q3 Plot the distribution of daily returns by stock using boxplots.

Hint: See the code in 4.3.3 Box plots. Add an appropriate title and labels for both axes.

ggplot(stock_returns, 
       aes(x = symbol, 
           y = daily.returns)) +
  geom_boxplot() +
  labs(title = "Daily Returns by Stock",
       x = "Ticker Symbol",
       y = "Daily Return")

Q4 Based on the boxplot above, which of the three stocks performed best this year?

Hint: Answer the question by comparing median and outliers of each stock.

According to the boxplot Amazon performed the best this year because their tail is the highest and does not go as low as Target’s. They also have an incredibly positive outlier and no negative outliers.

Q5 Calculate mean daily returns for each stock.

Hint: See the code in 4.3.1 Bar chart (on summary statistics).

library(dplyr)
meandata <- stock_returns %>%
  group_by(symbol) %>%
  summarize(mean_daily.returns = mean(daily.returns))

Q6 Plot mean daily returns using bar charts.

Hint: See the code in 4.3.1 Bar chart (on summary statistics). Add an appropriate title and labels for both axes.

ggplot(meandata, 
       aes(x = symbol, 
           y = mean_daily.returns)) +
  geom_bar(stat = "identity",
           fill = "cornflowerblue") +
  labs(title = "Mean Daily Returns by Stock",
       x = "Ticker Symbol",
       y = "Mean Daily Return") +
  geom_text(aes(label = (mean_daily.returns)), 
            vjust = -0.25)

Q7 Create the line plot of stock prices for all three stocks in one graph.

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

ggplot() + 
  geom_line(data = Walmart2, aes(x = date, y = adjusted), color = "blue") +
  geom_line(data = Target2, aes(x = date, y = adjusted), color = "red") +
  geom_line(data = Amazon2, aes(x= date, y = adjusted), color = "yellow")

Q7.a Remove Amazon from the line plot you created in Q7.

Note: This is an extra credit question worth 10 points. However, the total number of points you could earn for this quiz is capped at 100 points. In other words, the extra credit can only offset any one question you missed in the first seven questions.

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.

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

Q10 Use the correct slug.