options(repos = c(CRAN = "https://cloud.r-project.org"))
Note: Choices of stocks reflect popular stocks among the Swedish population.
The choosen stocks are: Atlas Copco (Industrial), Investor (Financials), SINCH (Information Technology), Orrön (Energy), SSAB (Material), SAAB (Defense), Axfood (Consumer Staples), Volvo Car (Consumer Discretionary), AstraZeneca (Healtcare), Nordea (Bank), Ericsson (Communication Services), Castellum (Real Estate)
The Stock price is collected from March 1st, a month ahead of liberation day to May 10th.
stocks <- tq_get(c("ATCO-A.ST", "SINCH.ST", "INVE-B.st", "SSAB-B.ST", "SAAB-B.ST", "AXFO.ST", "VOLCAR-B.ST", "NDA-SE.ST", "ERIC-B.ST", "CAST.ST", "ORRON.ST", "AZN.ST"),
get = "stock.prices",
from = "2025-03-01",
to = "2025-05-10")
stocks
## # A tibble: 564 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 5851286 179.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 5849265 174.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 6462212 183.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 5369088 181.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 6084395 182.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 3915631 177.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 4600879 173.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 6365358 175.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3017065 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 3440299 175.
## # ℹ 554 more rows
stocks %>%
ggplot(aes(x = date, y = adjusted, color = symbol)) +
geom_line()
Improved Graph
# Load data
tickers <- c("ATCO-A.ST", "SINCH.ST", "INVE-B.st", "SSAB-B.ST", "SAAB-B.ST", "AXFO.ST", "VOLCAR-B.ST", "NDA-SE.ST", "ERIC-B.ST", "CAST.ST", "ORRON.ST", "AZN.ST")
df_stocks <- yf_get(tickers = tickers,
first_date = as.Date("2025-03-01"),
last_date = as.Date("2025-05-10"))
##
## ── Running yfR for 12 stocks | 2025-03-01 --> 2025-05-10 (70 days) ──
##
## ℹ Downloading data for benchmark ticker ^GSPC
## ℹ (1/12) Fetching data for ATCO-A.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Feliz que nem lambari de sanga!
## ℹ (2/12) Fetching data for AXFO.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Youre doing good!
## ℹ (3/12) Fetching data for AZN.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Time for some tea?
## ℹ (4/12) Fetching data for CAST.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- All OK!
## ℹ (5/12) Fetching data for ERIC-B.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Looking good!
## ℹ (6/12) Fetching data for INVE-B.st
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- All OK!
## ℹ (7/12) Fetching data for NDA-SE.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- All OK!
## ℹ (8/12) Fetching data for ORRON.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Nice!
## ℹ (9/12) Fetching data for SAAB-B.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- You got it vincentsmac!
## ℹ (10/12) Fetching data for SINCH.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Boa!
## ℹ (11/12) Fetching data for SSAB-B.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Time for some tea?
## ℹ (12/12) Fetching data for VOLCAR-B.ST
## ! - not cached
## ✔ - cache saved successfully
## ✔ - got 47 valid rows (2025-03-03 --> 2025-05-09)
## ✔ - got 96% of valid prices -- Nice!
## ℹ Binding price data
##
## ── Diagnostics ─────────────────────────────────────────────────────────────────
## ✔ Returned dataframe with 564 rows -- Time for some tea?
## ℹ Using 78.4 kB at /var/folders/xv/5h23j4p10m94jp1pvq6296x00000gn/T//Rtmp9P70JR/yf_cache for 13 cache files
## ℹ Out of 12 requested tickers, you got 12 (100%)
# Calculate % change from first available price
df_pct_change <- df_stocks %>%
group_by(ticker) %>%
arrange(ref_date) %>%
mutate(pct_change = 100 * (price_adjusted / first(price_adjusted) - 1))
# Plot % change
ggplot(df_pct_change, aes(x = ref_date, y = pct_change, color = ticker)) +
geom_line(lindewidth = 1) +
labs(title = "Stock Performance (% Change from Start)",
x = "Date", y = "% Change", color = "Stock") +
scale_y_continuous(labels = scales::percent_format(scale = 1)) +
theme_minimal()
## Warning in geom_line(lindewidth = 1): Ignoring unknown parameters: `lindewidth`
ggplot(df_pct_change, aes(x = ref_date, y = pct_change, color = ticker)) +
geom_line(size = 1.2) +
scale_color_tableau(name = "Stock") + # Color-blind friendly and distinct
labs(title = "Stock Performance (% Change from Start)",
x = "Date", y = "% Change") +
theme_economist() + # or try theme_fivethirtyeight()
scale_y_continuous(labels = scales::percent_format(scale = 1))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning in check_pal_n(n, max_n): This palette can handle a maximum of 10
## values.You have supplied 12.
## Warning: Removed 94 rows containing missing values or values outside the scale range
## (`geom_line()`).
Filter Rows
filter(stocks, high < 200, low > 100)
## # A tibble: 139 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 5851286 179.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 5849265 174.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 6462212 183.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 5369088 181.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 6084395 182.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 3915631 177.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 4600879 173.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 6365358 175.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3017065 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 3440299 175.
## # ℹ 129 more rows
filter(stocks, high < 200 | low > 100)
## # A tibble: 564 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 5851286 179.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 5849265 174.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 6462212 183.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 5369088 181.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 6084395 182.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 3915631 177.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 4600879 173.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 6365358 175.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3017065 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 3440299 175.
## # ℹ 554 more rows
filter(stocks, high < 200 & low > 100)
## # A tibble: 139 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 5851286 179.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 5849265 174.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 6462212 183.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 5369088 181.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 6084395 182.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 3915631 177.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 4600879 173.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 6365358 175.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3017065 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 3440299 175.
## # ℹ 129 more rows
Arrange Rows
arrange(stocks, symbol, date, open, close)
## # A tibble: 564 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 5851286 179.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 5849265 174.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 6462212 183.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 5369088 181.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 6084395 182.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 3915631 177.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 4600879 173.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 6365358 175.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3017065 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 3440299 175.
## # ℹ 554 more rows
arrange(stocks, desc(open))
## # A tibble: 564 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AZN.ST 2025-03-03 1622 1628. 1606. 1607 375268 1607
## 2 AZN.ST 2025-03-05 1613 1614. 1584. 1596 410343 1596
## 3 AZN.ST 2025-03-04 1607 1639 1604. 1625 457533 1625
## 4 AZN.ST 2025-03-06 1586 1588. 1564 1582. 339353 1582.
## 5 AZN.ST 2025-03-17 1586. 1586. 1550. 1563 223416 1563
## 6 AZN.ST 2025-03-07 1574. 1575 1562. 1572 235706 1572
## 7 AZN.ST 2025-03-14 1568 1576. 1555 1574. 214782 1574.
## 8 AZN.ST 2025-03-18 1563 1568. 1546. 1552. 236841 1552.
## 9 AZN.ST 2025-03-10 1560 1560. 1530. 1554. 284282 1554.
## 10 AZN.ST 2025-03-21 1552 1552 1523 1525 447013 1525
## # ℹ 554 more rows
arrange(stocks, desc(close))
## # A tibble: 564 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 AZN.ST 2025-03-04 1607 1639 1604. 1625 457533 1625
## 2 AZN.ST 2025-03-03 1622 1628. 1606. 1607 375268 1607
## 3 AZN.ST 2025-03-05 1613 1614. 1584. 1596 410343 1596
## 4 AZN.ST 2025-03-06 1586 1588. 1564 1582. 339353 1582.
## 5 AZN.ST 2025-03-14 1568 1576. 1555 1574. 214782 1574.
## 6 AZN.ST 2025-03-07 1574. 1575 1562. 1572 235706 1572
## 7 AZN.ST 2025-03-13 1534 1570. 1531 1569 404628 1569
## 8 AZN.ST 2025-03-17 1586. 1586. 1550. 1563 223416 1563
## 9 AZN.ST 2025-03-20 1542. 1569 1542. 1555 243844 1555
## 10 AZN.ST 2025-03-10 1560 1560. 1530. 1554. 284282 1554.
## # ℹ 554 more rows
Select Columns
select(stocks, symbol:close)
## # A tibble: 564 × 6
## symbol date open high low close
## <chr> <date> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181.
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175.
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184.
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183.
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183.
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179.
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175.
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177.
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177.
## # ℹ 554 more rows
select(stocks, symbol, date, open, close)
## # A tibble: 564 × 4
## symbol date open close
## <chr> <date> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 181.
## 2 ATCO-A.ST 2025-03-04 178. 175.
## 3 ATCO-A.ST 2025-03-05 180. 184.
## 4 ATCO-A.ST 2025-03-06 186. 183.
## 5 ATCO-A.ST 2025-03-07 181. 183.
## 6 ATCO-A.ST 2025-03-10 184. 179.
## 7 ATCO-A.ST 2025-03-11 182. 175.
## 8 ATCO-A.ST 2025-03-12 178. 177.
## 9 ATCO-A.ST 2025-03-13 175. 176.
## 10 ATCO-A.ST 2025-03-14 175. 177.
## # ℹ 554 more rows
select(stocks, symbol, date, open, close, contains("adjusted"))
## # A tibble: 564 × 5
## symbol date open close adjusted
## <chr> <date> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 181. 179.
## 2 ATCO-A.ST 2025-03-04 178. 175. 174.
## 3 ATCO-A.ST 2025-03-05 180. 184. 183.
## 4 ATCO-A.ST 2025-03-06 186. 183. 181.
## 5 ATCO-A.ST 2025-03-07 181. 183. 182.
## 6 ATCO-A.ST 2025-03-10 184. 179. 177.
## 7 ATCO-A.ST 2025-03-11 182. 175. 173.
## 8 ATCO-A.ST 2025-03-12 178. 177. 175.
## 9 ATCO-A.ST 2025-03-13 175. 176. 174.
## 10 ATCO-A.ST 2025-03-14 175. 177. 175.
## # ℹ 554 more rows
Add Columns
mutate(stocks,
range = high - low) %>%
select(symbol:close, range)
## # A tibble: 564 × 7
## symbol date open high low close range
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 ATCO-A.ST 2025-03-03 183. 184. 181. 181. 3.10
## 2 ATCO-A.ST 2025-03-04 178. 179. 174. 175. 4.90
## 3 ATCO-A.ST 2025-03-05 180. 185. 178. 184. 7.5
## 4 ATCO-A.ST 2025-03-06 186. 186. 179. 183. 7.55
## 5 ATCO-A.ST 2025-03-07 181. 184. 179. 183. 4.95
## 6 ATCO-A.ST 2025-03-10 184. 185. 179. 179. 6.05
## 7 ATCO-A.ST 2025-03-11 182. 182. 175. 175. 6.95
## 8 ATCO-A.ST 2025-03-12 178. 179. 176. 177. 3.55
## 9 ATCO-A.ST 2025-03-13 175. 178. 174. 176. 3.80
## 10 ATCO-A.ST 2025-03-14 175. 177. 175. 177. 2.55
## # ℹ 554 more rows
mutate(stocks,
range = high - low) %>%
select(range)
## # A tibble: 564 × 1
## range
## <dbl>
## 1 3.10
## 2 4.90
## 3 7.5
## 4 7.55
## 5 4.95
## 6 6.05
## 7 6.95
## 8 3.55
## 9 3.80
## 10 2.55
## # ℹ 554 more rows
Summarise with groups
summarise(stocks, open = mean(open, na.rm = TRUE))
## # A tibble: 1 × 1
## open
## <dbl>
## 1 248.
stocks %>%
group_by(symbol) %>%
summarise(open = mean(open, na.rm = TRUE)) %>%
arrange(open)
## # A tibble: 12 × 2
## symbol open
## <chr> <dbl>
## 1 ORRON.ST 4.64
## 2 VOLCAR-B.ST 19.6
## 3 SINCH.ST 21.9
## 4 SSAB-B.ST 62.6
## 5 ERIC-B.ST 80.2
## 6 CAST.ST 111.
## 7 NDA-SE.ST 132.
## 8 ATCO-A.ST 162.
## 9 AXFO.ST 240.
## 10 INVE-B.st 294.
## 11 SAAB-B.ST 407.
## 12 AZN.ST 1437.