#De Bai lam viec voi tap du lieu economic va truc quan bieu dien ti le thu nhap ca nhan cua nguoi dan My. Chung ta can truc quan de lieu theo chuoi thoi gian de xem xet ti le thu thap cua nguoi dan My tang hay giam tu 1
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
{r} ggplot(data = economics, mapping = aes(x = date, y = psavert)) + geom_line(color=#00FFFF) + labs(tilte = “bang tu nam 1967 den 2015”, x=“time”, y= “ti le tiet kiem” ) Nguyễn
library(ggplot2)
library(scales)
library(dplyr)
library(plotly)
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
ggplot(data = economics, mapping = aes(x = date, y = psavert)) +
geom_line(color="#880000", size = 1) +
geom_smooth() +
scale_x_date(date_breaks = "5 years", labels = date_format("%b-%y")) +
labs(title = "bang tu nam 1967 den 2015",
subtitle = " 5 nam tu 1967 den 2015",
x="time",
y= "ti le tiet kiem"
)+
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)
library(plotly)
apple <- getSymbols("AAPL", return.class="data.frame", from = "2025-1-7")
tail(AAPL, n=4)
## AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
## 2025-12-31 273.06 273.68 271.75 271.86 27293600 271.86
## 2026-01-02 272.26 277.84 269.00 271.01 37838100 271.01
## 2026-01-05 270.64 271.51 266.14 267.26 45647200 267.26
## 2026-01-06 267.00 267.55 262.12 262.36 52282100 262.36
nvidia <- getSymbols("NVDA", return.class="data.frame", from = "2025-1-7")
tail(NVDA, n=4)
## NVDA.Open NVDA.High NVDA.Low NVDA.Close NVDA.Volume NVDA.Adjusted
## 2025-12-31 189.57 190.56 186.49 186.50 120100500 186.50
## 2026-01-02 189.84 192.93 188.26 188.85 148240500 188.85
## 2026-01-05 191.76 193.63 186.15 188.12 183529700 188.12
## 2026-01-06 190.52 192.17 186.82 187.24 176174700 187.24
apple <- AAPL %>%
mutate(Date = as.Date(row.names(.))) %>%
select(Date, AAPL.Close) %>%
rename(Close = AAPL.Close) %>%
mutate(Company = "Apple")
nvidia <- NVDA %>%
mutate(Date = as.Date(row.names(.),format = "%Y-%m-%d")) %>%
select(Date, NVDA.Close) %>%
rename(Close = NVDA.Close) %>%
mutate(Company = "nvidia")
gop <- rbind(apple, nvidia)
ggplot(data=gop, mapping = aes(x=Date, y=Close, color=Company)) +
geom_line(size=1) +
geom_smooth() +
scale_x_date(date_breaks = "1 month", labels = date_format("%b-%y")) +
theme(axis.text.x = element_text(angle = 36, hjust = 1)) +
labs(title="Gia dong cua co phieu",
subtitle = "12-2024 den nay",
x="",
y="Closing price")
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'