In this exercise you will learn to plot data using the ggplot2 package. To answer the questions below, use 4.1 Categorical vs. Categorical from Data Visualization with R.
# Load packages
library(tidyquant)
library(tidyverse)
library(lubridate) #for year()
# Pick stocks
stocks <- c("AAPL", "MSFT", "NFLX")
# Import stock prices
stock_prices <- stocks %>%
tq_get(get = "stock.prices",
from = "2015-01-01",
to = "2019-05-31") %>%
group_by(symbol)
stock_prices
## # A tibble: 3,327 x 8
## # Groups: symbol [3]
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AAPL 2015-01-02 111. 111. 107. 109. 53204600 100.
## 2 AAPL 2015-01-05 108. 109. 105. 106. 64285500 97.6
## 3 AAPL 2015-01-06 107. 107. 105. 106. 65797100 97.6
## 4 AAPL 2015-01-07 107. 108. 107. 108. 40105900 99.0
## 5 AAPL 2015-01-08 109. 112. 109. 112. 59364500 103.
## 6 AAPL 2015-01-09 113. 113. 110. 112. 53699500 103.
## 7 AAPL 2015-01-12 113. 113. 109. 109. 49650800 100.
## 8 AAPL 2015-01-13 111. 113. 109. 110. 67091900 101.
## 9 AAPL 2015-01-14 109. 110. 108. 110. 48956600 101.
## 10 AAPL 2015-01-15 110 110. 107. 107. 60014000 98.1
## # … with 3,317 more rows
# Process stock_prices and save it under stock_returns
stock_returns <-
stock_prices %>%
# Calculate monthly returns
tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "monthly") %>%
# create a new variable, year
mutate(year = year(date)) %>%
# drop date
select(-date)
stock_returns
## # A tibble: 159 x 3
## # Groups: symbol [3]
## symbol monthly.returns year
## <chr> <dbl> <dbl>
## 1 AAPL 0.0716 2015
## 2 AAPL 0.101 2015
## 3 AAPL -0.0314 2015
## 4 AAPL 0.00579 2015
## 5 AAPL 0.0453 2015
## 6 AAPL -0.0372 2015
## 7 AAPL -0.0329 2015
## 8 AAPL -0.0662 2015
## 9 AAPL -0.0218 2015
## 10 AAPL 0.0834 2015
## # … with 149 more rows
Hint: See the code in 4.3.1 Bar chart (on summary statistics).
library(dplyr)
plotdata <- stock_returns %>%
group_by(symbol) %>%
summarize(mean_returns = mean(monthly.returns))
plotdata
## # A tibble: 3 x 2
## symbol mean_returns
## <chr> <dbl>
## 1 AAPL 0.0136
## 2 MSFT 0.0228
## 3 NFLX 0.0448
Hint: See the code in 4.3.1 Bar chart (on summary statistics).
ggplot(plotdata,
aes(x = symbol,
y = mean_returns)) +
geom_bar(stat = "identity")
Hint: See the code in 4.3.1 Bar chart (on summary statistics).
library(scales)
ggplot(plotdata,
aes(x = factor(symbol,
labels = c("Apple",
"Microsoft",
"International Business Machines Corporation")),
y = mean_returns)) +
geom_bar(stat = "identity") + geom_bar(stat = "identity",
fill = "cornflowerblue") +
geom_text(aes(label = percent(mean_returns)),
vjust = -0.25)
Hint: See the code in 4.3.2 Grouped kernel density plots.
ggplot(stock_returns,
aes(x = monthly.returns,
fill = symbol)) +
geom_density(alpha = 0.4) +
labs(title = "Stock distribution")
Hint: Google how to interpret density plots.
The long tail on the right indicates that it’s possible to win very big with Netflix. I think Netflix out of the three stocks has the highest chance of winning big when things are good.
Hint: See the code in 4.3.3 Box plots.
ggplot(stock_returns,
aes(x = symbol,
y = monthly.returns)) +
geom_boxplot() +
labs(title = "Distribution of Daily Returns")
A risk-loving investor would choose Netflix. The long tail on the right indicates that it’s possible to win very big with Apple. The fatter tail of Netflix on the far left indicates that the odds of losing big with Netflix is likelier than the other two stocks.
Hint: Use message, echo and results in the global chunk options. Refer to the RMarkdown Reference Guide.