Làm việc với tập dữ liệu Econnomics và trực quan biến tỷ lệ thu nhập cá nhân psavert của người dân 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 năm 2015.
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
Hiển thị 4 dữ liệu đầu tiên
data("economics")
head(economics, n = 4)
## # A tibble: 4 × 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
Hiển thị 4 dữ liệu cuối cùng
tail(economics, n = 4)
## # A tibble: 4 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 2015-01-01 12046 319929. 7.7 13.2 8903
## 2 2015-02-01 12082. 320075. 7.9 12.9 8610
## 3 2015-03-01 12158. 320231. 7.4 12 8504
## 4 2015-04-01 12194. 320402. 7.6 11.5 8526
Chúng ta sử dụng dữ liệu line 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 = "#f96a11", size = 0.7) +
geom_smooth() +
scale_x_date(date_breaks = "5 years", labels = date_format("%b-%y"))+
labs(title = "Personal Saving Rate in USA from 1967 to 2015",
subtitle = "5 years from 1967 to 2015",
x = "",
y = "Personal Saving 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'
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
apple <- getSymbols("AAPL", return.class = "data.frame", from = "2024-01-01")
tail(AAPL, n=4)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2025-12-19 272.15 274.60 269.90 273.67 144632000 273.67
## 2025-12-22 272.86 273.88 270.51 270.97 36571800 270.97
## 2025-12-23 270.84 272.50 269.56 272.36 29642000 272.36
## 2025-12-24 272.34 275.43 272.20 273.81 17910600 273.81
apple <- AAPL %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, AAPL.Close) %>%
rename(Close = AAPL.Close) %>%
mutate(Company = "Apple")
vinfast <- getSymbols("VFS", return.class = "data.frame", from = "2024-01-01")
vinfast <- VFS %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, VFS.Close) %>%
rename(Close = VFS.Close) %>%
mutate(Company = "Vinfast")
mydata <- rbind(apple, vinfast)
#Trực quan dữ liệu
ggplot(data = apple, mapping = aes(x = Date,
y = Close)) +
geom_line(size = 1, color = "indianred3") +
scale_x_date(date_breaks = "3 months",
label = date_format("%b-%Y")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(limits = c(30, 450), breaks = seq(30,400,50),
labels = dollar) +
labs(title = "NASDAQ Closing Prices",
subtitle = "From May 2024 to February 2025",
caption = "source: Yahoo Finance",
x = "",
y = "Closing Price") +
scale_color_brewer(palette = "Set1")