Đề bài

Làm việc với tập dữ liệu economic và trực quan biến tỷ lệ thu nhập cá nhâp psavert của người mỹ.chúng ta cần trực quan dữ liệu theo chuỗi thời gian để xem xét tỉ lệ thu nhập của ngừi dân mỹ tăng hay giảm từ năm 1965 đến 2015

Tiền xử lý dữ liệu

Tải thư viện

library(ggplot2)
library(scales)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
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
## 
## ######################### 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: 'xts'
## The following objects are masked from 'package:dplyr':
## 
##     first, last
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo

Tập dữ liệu

data("economics")
head(economics)
## # A tibble: 6 × 6
##   date         pce    pop psavert uempmed unemploy
##   <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
## 1 1967-07-01  507. 198712    12.6     4.5     2944
## 2 1967-08-01  510. 198911    12.6     4.7     2945
## 3 1967-09-01  516. 199113    11.9     4.6     2958
## 4 1967-10-01  512. 199311    12.9     4.9     3143
## 5 1967-11-01  517. 199498    12.8     4.7     3066
## 6 1967-12-01  525. 199657    11.8     4.8     3018
tail(economics)
## # A tibble: 6 × 6
##   date          pce     pop psavert uempmed unemploy
##   <date>      <dbl>   <dbl>   <dbl>   <dbl>    <dbl>
## 1 2014-11-01 12051. 319564.     7.3    13       9090
## 2 2014-12-01 12062  319746.     7.6    12.9     8717
## 3 2015-01-01 12046  319929.     7.7    13.2     8903
## 4 2015-02-01 12082. 320075.     7.9    12.9     8610
## 5 2015-03-01 12158. 320231.     7.4    12       8504
## 6 2015-04-01 12194. 320402.     7.6    11.5     8526

Trực quan dữ liệu

chúng ta sử dụng dữ liệu graph để thực hiện việc trực quan dữ liệu

ggplot(data= economics, mapping= aes(x= date,y= psavert)) +
  geom_line( color = "#6b19bd", size = 0.36) +
  geom_smooth( ) +
  scale_x_date(date_breaks = "5 years", labels = date_format("%b-%y"))+
  labs(title= "Personal Savings Rate in USA from 1967 to 2015",
       subtitle= "5 years from 1967 to 2015",
       x= "",
       y= "Personal Savings Rate") +
  theme_minimal() -> p
## 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.
ggplotly(p)
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

thực hành nội dung dữ liệu real

tải thư viện quantmod

tải dữ liệu

# Apple
getSymbols("AAPL", from = "2021-05-05")
## [1] "AAPL"
apple <- AAPL %>%
  data.frame() %>%
  mutate(Date = as.Date(row.names(.))) %>%
  select(Date, AAPL.Close) %>%
  rename(Close = AAPL.Close) %>%
  mutate(Company = "Apple")

# Facebook
getSymbols("META", from = "2021-05-05")
## [1] "META"
facebook <- META %>%
  data.frame() %>%
  mutate(Date = as.Date(row.names(.))) %>%
  select(Date, META.Close) %>%
  rename(Close = META.Close) %>%
  mutate(Company = "Facebook")

# Google
getSymbols("GOOGL", from = "2021-05-05")
## [1] "GOOGL"
google <- GOOGL %>%
  data.frame() %>%
  mutate(Date = as.Date(row.names(.))) %>%
  select(Date, GOOGL.Close) %>%
  rename(Close = GOOGL.Close) %>%
  mutate(Company = "Google")

# VinFast 
getSymbols("VFS", from = "2023-08-15")
## [1] "VFS"
vinfast <- VFS %>%
  data.frame() %>%
  mutate(Date = as.Date(row.names(.))) %>%
  select(Date, VFS.Close) %>%
  rename(Close = VFS.Close) %>%
  mutate(Company = "VinFast")

# Combine data
data_series <- bind_rows(apple, facebook, vinfast, google)

# Visualization
ggplot(data_series, aes(x = Date, y = Close, color = Company)) +
  geom_line(size = 1) +
  scale_x_date(date_breaks = "3 months",
               labels = date_format("%b-%Y")) +
  scale_y_continuous(labels = dollar) +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(
    title = "Comparison of Stock Closing Prices",
    subtitle = "Apple, Facebook, Google and VinFast",
    caption = "Source: Yahoo Finance",
    x = "",
    y = "Closing Price (USD)"
  ) +
  scale_color_brewer(palette = "Set1")