knitr::opts_chunk$set(
echo = TRUE,
message = FALSE,
warning = FALSE
)
library(tidyverse)
library(tidyquant)
symbols <- c("V", "AMZN", "NFLX", "WMT", "KO")
stocks <- tq_get(
x = symbols,
get = "stock.prices",
from = "2016-01-01",
to = "2016-12-31"
)
# Line plot of adjusted closing prices
ggplot(data = stocks, aes(x = date, y = adjusted, color = symbol)) +
geom_line(size = 1) +
labs(title = "Adjusted Closing Prices for Selected Stocks (2016)",
x = "Date",
y = "Adjusted Price",
color = "Ticker") +
theme_minimal()
This chart shows how the adjusted closing prices of V, AMZN, NFLX, WMT, and KO varied throughout 2016. We can see that Amazon and Netflix had more price volatility, while Coca-Cola (KO) showed a steadier pattern. This reflects the difference between growth and defensive stocks during that time period.
filter(stocks, adjusted > 100)
## # A tibble: 102 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NFLX 2016-01-04 109 110 105. 110. 20794800 110.
## 2 NFLX 2016-01-05 110. 111. 106. 108. 17664600 108.
## 3 NFLX 2016-01-06 105. 118. 105. 118. 33045700 118.
## 4 NFLX 2016-01-07 116. 122. 112. 115. 33636700 115.
## 5 NFLX 2016-01-08 116. 118. 111. 111. 18067100 111.
## 6 NFLX 2016-01-11 112. 117. 111. 115. 21920400 115.
## 7 NFLX 2016-01-12 116. 118. 115. 117. 15133500 117.
## 8 NFLX 2016-01-13 114. 114. 105. 107. 24921600 107.
## 9 NFLX 2016-01-14 106. 109. 101. 107. 23664800 107.
## 10 NFLX 2016-01-15 102. 106. 102. 104. 19775100 104.
## # ℹ 92 more rows
arrange(stocks, desc(adjusted))
## # A tibble: 1,260 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 NFLX 2016-12-27 126. 129. 126. 128. 5288200 128.
## 2 NFLX 2016-10-21 122. 128. 122. 128. 18832400 128.
## 3 NFLX 2016-10-24 127. 129. 126. 127. 15980700 127.
## 4 NFLX 2016-10-26 126. 129. 126. 127. 8602400 127.
## 5 NFLX 2016-10-28 126. 129. 126. 127. 7077300 127.
## 6 NFLX 2016-10-25 127. 128. 126. 127. 8253900 127.
## 7 NFLX 2016-12-21 126. 128. 125 126. 6514800 126.
## 8 NFLX 2016-10-27 127. 128. 126. 126. 6914200 126.
## 9 NFLX 2016-12-28 129. 129. 126. 126. 4389200 126.
## 10 NFLX 2016-12-23 126. 126. 125. 126. 2010400 126.
## # ℹ 1,250 more rows
select(stocks, symbol, date, adjusted)
## # A tibble: 1,260 × 3
## symbol date adjusted
## <chr> <date> <dbl>
## 1 V 2016-01-04 70.8
## 2 V 2016-01-05 71.4
## 3 V 2016-01-06 70.4
## 4 V 2016-01-07 69.1
## 5 V 2016-01-08 68.2
## 6 V 2016-01-11 69.2
## 7 V 2016-01-12 70.0
## 8 V 2016-01-13 68.4
## 9 V 2016-01-14 69.1
## 10 V 2016-01-15 67.2
## # ℹ 1,250 more rows
mutate(stocks, range = high - low)
## # A tibble: 1,260 × 9
## symbol date open high low close volume adjusted range
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 V 2016-01-04 76.1 76.4 75.0 75.7 13476800 70.8 1.37
## 2 V 2016-01-05 76.2 76.5 75.5 76.3 8972700 71.4 0.970
## 3 V 2016-01-06 75 75.8 74.8 75.3 16092000 70.4 1
## 4 V 2016-01-07 73.8 75.1 73.3 73.8 12470800 69.1 1.74
## 5 V 2016-01-08 74.1 74.5 72.8 72.9 9977600 68.2 1.76
## 6 V 2016-01-11 73.1 74.1 72.7 73.9 13069500 69.2 1.42
## 7 V 2016-01-12 74.5 75 74.0 74.8 10236900 70.0 0.980
## 8 V 2016-01-13 75.3 75.4 73.0 73.1 12659600 68.4 2.40
## 9 V 2016-01-14 73.2 74.5 72.7 73.8 10426200 69.1 1.86
## 10 V 2016-01-15 71.9 72.9 70.7 71.8 18470500 67.2 2.29
## # ℹ 1,250 more rows
stocks %>%
group_by(symbol) %>%
summarize(avg_price = mean(adjusted, na.rm = TRUE))
## # A tibble: 5 × 2
## symbol avg_price
## <chr> <dbl>
## 1 AMZN 35.0
## 2 KO 32.9
## 3 NFLX 102.
## 4 V 73.2
## 5 WMT 19.7