Mô phỏng dữ liệu chuỗi thời gian
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
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
ggplot(data= economics, mapping= aes(x= date,y= psavert)) +
geom_line()+
labs(title= "PersonalSavingsRate",
x= "Date",
y= "PersonalSavingsRate")
#Formore attractivegraphs
#https://www.r-bloggers.com/2013/08/date-formats-in-r/
ggplot(data= economics, mapping= aes(x= date,y= psavert)) +
geom_line(color= "indianred3", linewidth= 1) +
geom_smooth()+
scale_x_date(date_breaks = "5 years", date_labels = "%b-%y") +
labs(title= "PersonalSavingsRate",
subtitle= "From1967to2015",
x= "",
y= "PersonalSavingsRate") +
theme_minimal()
## `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(dplyr)
library(ggplot2)
# ======================
# FPT (HOSE)
# ======================
fpt_xts <- getSymbols(
"FPT.VN",
auto.assign = FALSE,
from = "2021-05-05"
)
## Warning: FPT.VN contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
fpt <- data.frame(
Date = as.Date(index(fpt_xts)),
Close = as.numeric(Cl(fpt_xts)),
Company = "FPT"
) %>%
na.omit()
# ======================
# Vingroup (HOSE)
# ======================
vic_xts <- getSymbols(
"VIC.VN",
auto.assign = FALSE,
from = "2021-05-05"
)
## Warning: VIC.VN contains missing values. Some functions will not work if
## objects contain missing values in the middle of the series. Consider using
## na.omit(), na.approx(), na.fill(), etc to remove or replace them.
vingroup <- data.frame(
Date = as.Date(index(vic_xts)),
Close = as.numeric(Cl(vic_xts)),
Company = "Vingroup"
) %>%
na.omit()
# ======================
# Combine
# ======================
data_series <- bind_rows(fpt, vingroup)
# ======================
# Plot
# ======================
ggplot(data_series, aes(x = Date, y = Close, color = Company)) +
geom_line(linewidth = 1) +
theme_minimal() +
labs(
title = "Vietnam Stocks (HOSE)",
subtitle = "FPT vs Vingroup",
x = "Date",
y = "Closing Price (VND)",
caption = "Source: Yahoo Finance"
)
library(ggplot2)
library(dplyr)
library(tidyr)
library(gapminder)
data(gapminder)
# Lọc dữ liệu
plotdata_long <- filter(gapminder, continent == "Asia" & year %in% c(1952, 2007)) %>%
select(country, year, lifeExp)
# Chuyển sang wide format
plotdata_wide <- spread(plotdata_long, year, lifeExp)
names(plotdata_wide) <- c("Country", "year1952", "year2007")
# Dumbbell chart bằng ggplot2 thuần
ggplot(plotdata_wide, aes(y = reorder(Country, year1952))) +
geom_segment(aes(x = year1952, xend = year2007, yend = Country), color = "grey") +
geom_point(aes(x = year1952), color = "blue", size = 3) +
geom_point(aes(x = year2007), color = "red", size = 3) +
theme_minimal() +
labs(
title = "Change in Life Expectancy",
subtitle = "From 1952 to 2007",
x = "Life Expectancy (years)",
y = ""
)