#thu vien

library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)
library(scales)
library(dplyr)
## 
## ######################### Warning from 'xts' package ##########################
## #                                                                             #
## # The dplyr lag() function breaks how base R's lag() function is supposed to  #
## # work, which breaks lag(my_xts). Calls to lag(my_xts) that you type or       #
## # source() into this session won't work correctly.                            #
## #                                                                             #
## # Use stats::lag() to make sure you're not using dplyr::lag(), or you can add #
## # conflictRules('dplyr', exclude = 'lag') to your .Rprofile to stop           #
## # dplyr from breaking base R's lag() function.                                #
## #                                                                             #
## # Code in packages is not affected. It's protected by R's namespace mechanism #
## # Set `options(xts.warn_dplyr_breaks_lag = FALSE)` to suppress this warning.  #
## #                                                                             #
## ###############################################################################
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

lay du lieu + xu li apple

apple <- getSymbols("AAPL", return.class="data.frame", from="2024-12-09")
tail(AAPL,n=5)
##            AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2025-12-31    273.06    273.68   271.75     271.86    27293600        271.86
## 2026-01-02    272.26    277.84   269.00     271.01    37838100        271.01
## 2026-01-05    270.64    271.51   266.14     267.26    45647200        267.26
## 2026-01-06    267.00    267.55   262.12     262.36    52352100        262.36
## 2026-01-07    263.27    263.68   259.82     260.33    48201265        260.33
apple <- AAPL %>%
  mutate(Date=as.Date(row.names(.))) %>%
  select(Date, AAPL.Close) %>%
  rename(Close=AAPL.Close) %>%
  mutate(company="apple") 

lay du lieu + xu li gg pixel

pixel <- getSymbols("GOOGL", return.class="data.frame", from="2024-12-09")
tail(GOOGL,n=5)
##            GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume
## 2025-12-31     312.85     314.58    311.44      313.00     16377700
## 2026-01-02     316.90     322.50    310.33      315.15     32009400
## 2026-01-05     317.66     319.02    314.63      316.54     30195600
## 2026-01-06     316.40     320.94    311.78      314.34     31212100
## 2026-01-07     314.36     326.15    314.19      321.98     34871880
##            GOOGL.Adjusted
## 2025-12-31         313.00
## 2026-01-02         315.15
## 2026-01-05         316.54
## 2026-01-06         314.34
## 2026-01-07         321.98
pixel <- GOOGL %>%
  mutate(Date=as.Date(row.names(.))) %>%
  rename(Close=GOOGL.Close) %>%
  select(Date,Close) %>%
  mutate(company="G Pixel")

###gop bang

gop=rbind(apple,pixel)
#slice(gop, (n()/2 - 2):(n()/2 + 2))

truc quan

ggplot(data=gop, mapping=aes(x=Date,y=Close, color=company)) +
  geom_line(size=0.8) +
  scale_x_date(date_breaks = "1 months", label = date_format("%b-%Y")) +
  theme(axis.text.x = element_text(angle= 45, hjust= 1)) +
  scale_y_continuous(limits = c(100,400), breaks=seq(100,400,50), labels = dollar) +
  labs(title = "oh yeh",
       subtitle = "tu 12-2024 den ~1-2026",
       x = "",
       y = "Gia dong cua")
## 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.