TIME SERIES VISUALIZATION làm việc với dữ liệu Economies và trực quan tỉ lệ cá nhân psavert của người dân Mỹ, chúng ta cần trực quan dữ liệu theo thời gian để xem sét tỉ lệ thu nhập của người dân Mỹ tăng hay giảm 1965 đến năm 2015
#Tien xu li du lieu
library(ggplot2)
library(scales)
library(dplyr)
library(quantmod)
#Tap du lieu
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, 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
ggplot(economics, aes(x = date, y = psavert)) +
geom_line(color = "steelblue", linewidth = 1) +
labs(
title = "Personal Savings Rate in the U.S.",
x = "Year",
y = "Savings Rate (%)"
) +
theme_minimal()
# https://www.r-bloggers.com/2013/08/date-formats-in-r/
ggplot(economics, aes(date, psavert)) +
geom_line(color = "indianred3", linewidth = 1) +
geom_smooth(method = "loess", se = FALSE) +
scale_x_date(
date_breaks = "5 years",
labels = date_format("%Y")
) +
labs(
title = "Personal Savings Rate in the U.S.",
subtitle = "1967–2015",
x = "",
y = "Savings Rate (%)"
) +
theme_minimal()
# ========================
# Apple
# ========================
getSymbols("AAPL", from = "2021-05-05", auto.assign = TRUE)
## [1] "AAPL"
apple <- AAPL %>%
data.frame() %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, AAPL.Close) %>%
rename(Close = AAPL.Close) %>%
mutate(Company = "Apple")
# ========================
# Facebook -> META
# ========================
getSymbols("META", from = "2021-05-05", auto.assign = TRUE)
## [1] "META"
facebook <- META %>%
data.frame() %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, META.Close) %>%
rename(Close = META.Close) %>%
mutate(Company = "Meta")
# ========================
# Tesla
# ========================
getSymbols("TSLA", from = "2021-05-05", auto.assign = TRUE)
## [1] "TSLA"
tesla <- TSLA %>%
data.frame() %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, TSLA.Close) %>%
rename(Close = TSLA.Close) %>%
mutate(Company = "Tesla")
# ========================
# Combine
# ========================
data_series <- rbind(apple, facebook, tesla)
ggplot(data_series,
aes(x = Date, y = Close, color = Company)) +
geom_line(linewidth = 1) +
scale_x_date(
date_breaks = "3 months",
labels = date_format("%b-%Y")
) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(labels = dollar) +
labs(
title = "NASDAQ Closing Prices",
subtitle = "From May 2021 to Present",
caption = "Source: Yahoo Finance",
x = "",
y = "Closing Price"
) +
scale_color_brewer(palette = "Set1")