library(tidyquant)
library(tidyverse)
library(gt)
library(scales)Demo: How Markdown Document Works
Abstract
This code gets stock prices direclty from Yahoo Finance.
Keywords
Stock price, Yahoo Finance
Getting Data from Yahoo for Stock Prices:
This is a demo to show how you can access data directly from Yahoo Finance and import it into R. This demo will show what libraries needed and basic data cleaning.
Loading Libraries
Getting the Data Using {tidyquant} Package
#get Google (GOOG stock prices for a specific range
GOOG_stock_price <- tq_get("GOOG",
from = "2022-01-01",
to= "2026-01-30",
get = "stock.prices" )Displaying first 10 Observations & Cleaning the Table
head(GOOG_stock_price,n = 10) |> gt() |>
tab_header(
title = "Google Stock Price Dataset",
subtitle = "Direclty Using {tidyquant}") |>
fmt_number(decimal = 1)| Google Stock Price Dataset | |||||||
| Direclty Using {tidyquant} | |||||||
| symbol | date | open | high | low | close | volume | adjusted |
|---|---|---|---|---|---|---|---|
| GOOG | 2022-01-03 | 144.5 | 145.6 | 143.5 | 145.1 | 25,214,000.0 | 143.7 |
| GOOG | 2022-01-04 | 145.6 | 146.6 | 143.8 | 144.4 | 22,928,000.0 | 143.1 |
| GOOG | 2022-01-05 | 144.2 | 144.3 | 137.5 | 137.7 | 49,642,000.0 | 136.4 |
| GOOG | 2022-01-06 | 137.5 | 139.7 | 136.8 | 137.6 | 29,050,000.0 | 136.3 |
| GOOG | 2022-01-07 | 137.9 | 138.3 | 135.8 | 137.0 | 19,408,000.0 | 135.7 |
| GOOG | 2022-01-10 | 135.1 | 138.6 | 133.1 | 138.6 | 34,096,000.0 | 137.3 |
| GOOG | 2022-01-11 | 138.2 | 140.3 | 136.8 | 140.0 | 23,502,000.0 | 138.7 |
| GOOG | 2022-01-12 | 141.6 | 142.8 | 141.1 | 141.6 | 23,642,000.0 | 140.3 |
| GOOG | 2022-01-13 | 141.8 | 143.2 | 138.9 | 139.1 | 26,566,000.0 | 137.8 |
| GOOG | 2022-01-14 | 137.5 | 141.2 | 137.5 | 139.8 | 23,826,000.0 | 138.5 |
Plotting The Stock High-Low Price
ggplot (data = GOOG_stock_price, mapping = aes( x = date, y = low ))+
geom_line(color = "darkgrey", size =1)+
labs(title = " Google Stock Price During January 2026")+
theme_minimal()+
theme(plot.title = element_text(hjust = 0.5))Reshaping the data to plot low-high price
GOOG_stock_price_long <- pivot_longer(
GOOG_stock_price,
cols = c("high","low"),
names_to = "variable",
values_to = "value"
)
GOOG_stock_price_long# A tibble: 2,044 × 8
symbol date open close volume adjusted variable value
<chr> <date> <dbl> <dbl> <dbl> <dbl> <chr> <dbl>
1 GOOG 2022-01-03 144. 145. 25214000 144. high 146.
2 GOOG 2022-01-03 144. 145. 25214000 144. low 144.
3 GOOG 2022-01-04 146. 144. 22928000 143. high 147.
4 GOOG 2022-01-04 146. 144. 22928000 143. low 144.
5 GOOG 2022-01-05 144. 138. 49642000 136. high 144.
6 GOOG 2022-01-05 144. 138. 49642000 136. low 138.
7 GOOG 2022-01-06 137. 138. 29050000 136. high 140.
8 GOOG 2022-01-06 137. 138. 29050000 136. low 137.
9 GOOG 2022-01-07 138. 137. 19408000 136. high 138.
10 GOOG 2022-01-07 138. 137. 19408000 136. low 136.
# ℹ 2,034 more rows
Plotting the data
ggplot(data = GOOG_stock_price_long, aes(date, value, color = variable))+
geom_line(size=.5)+
scale_y_continuous(labels = dollar)+
theme(legend.position ="inside",
legend.position.inside = c(0.1,0.95))#Changing the order of columns in the data