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 ## 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
data("economics")
economics %>% head()
## # 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
chúng ta sử dụng dl line graph để trực quan dl
ggplot(data = economics, mapping = aes(x=date, y=psavert)) +
geom_line(color= "#f96a11", size =0.8) +
geom_smooth() +
scale_x_date(date_breaks = "5 years", labels = date_format("%b-%y")) +
labs(title = "Personal Savings Rate",
subtitle = "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'
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
library(ggplot2)
library(scales)
library(dplyr)
apple_raw <- getSymbols(
"AAPL",
from = "2024-03-01",
auto.assign = FALSE
)
apple <- apple_raw %>%
data.frame() %>%
mutate(Date = as.Date(index(apple_raw))) %>%
select(Date, AAPL.Close) %>%
rename(Close = AAPL.Close) %>%
mutate(Company = "Apple")
vin <- getSymbols("VFS",
from = "2024-03-01",
auto.assign = FALSE)
vin <- vin %>%
data.frame() %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, VFS.Close) %>%
rename(Close = VFS.Close) %>%
mutate(Company = "VinFast")
data_series <- bind_rows(apple, vin)
ggplot(data = data_series,
aes(x = Date, y = Close, color = Company)) +
geom_line(linewidth = 1) +
scale_x_date(date_breaks = "3 month",
labels = date_format("%b-%Y")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
scale_y_continuous(labels = dollar) +
labs(
title = "Stock Closing Prices",
subtitle = "Apple vs VinFast",
caption = "Source: Yahoo Finance",
x = "",
y = "Closing Price (USD)"
) +
scale_color_brewer(palette = "Set1")