Làm việc với tập dữ liệu Economies và trực quan hóa biến tỷ lệ thu nhập ca nhân của người dân theo tháng tại Mỹ từ tháng 1 năm 1967 tới tháng 1 năm 2015

#Tải thư viện
library(ggplot2)
library(scales)
library(dplyr)


###Tập dữ liệu
#hiện thị 6 dữ liệu đâu tiên tiên

data(economics, package = "ggplot2")
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
#hiện thị  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
###Trực quan dữ liệu tại đây
ggplot(data = economics, mapping = aes(x = date, y = psavert)) +
  geom_line(color = "indianred3", linewidth = 1) + 
  geom_smooth(method = "loess", se = FALSE) +
  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()

SO SÁNH SỐ LIỆU CHỨNG KHOÁNG

library(quantmod)
library(dplyr)
library(ggplot2)
library(scales)

# Lấy dữ liệu Apple
apple <- getSymbols("AAPL", src = "yahoo", auto.assign = FALSE,
                    from = "2020-01-01") %>%
  data.frame(Date = index(.), coredata(.)) %>%
  select(Date, AAPL.Close) %>%
  rename(Close = AAPL.Close) %>%
  mutate(Company = "Apple")

# Lấy dữ liệu Microsoft
microsoft <- getSymbols("MSFT", src = "yahoo", auto.assign = FALSE,
                        from = "2020-01-01") %>%
  data.frame(Date = index(.), coredata(.)) %>%
  select(Date, MSFT.Close) %>%
  rename(Close = MSFT.Close) %>%
  mutate(Company = "Microsoft")

# Lấy dữ liệu Nvidia
nvidia <- getSymbols("NVDA", src = "yahoo", auto.assign = FALSE,
                     from = "2020-01-01") %>%
  data.frame(Date = index(.), coredata(.)) %>%
  select(Date, NVDA.Close) %>%
  rename(Close = NVDA.Close) %>%
  mutate(Company = "Nvidia")

# Kết hợp dữ liệu
data_series <- rbind(apple, microsoft, nvidia)

# Vẽ biểu đồ
ggplot(data = data_series, mapping = aes(x = Date, y = Close, color = Company)) +
  geom_line(size = 0.8) +
  scale_x_date(
    date_breaks = "4 month",
    labels = date_format("%b-%Y")
  ) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_y_continuous(
    labels = dollar
  ) +
  labs(
    title = "- Securities",
    subtitle = "Apple, Microsoft, Nvidia",
    caption = "Source: Yahoo Finance",
    x = "",
    y = "Closing Price"
  ) +
  scale_color_brewer(palette = "Set1")

DumbbellChart Trực quan sự thay đổi về tuổi thọ trung bình của các quốc gia tại châu Á từ năm 1952 đến năm 20

# Nạp các thư viện cần thiết
library(ggplot2)
library(tidyr)
library(gapminder)
library(dplyr)

# Lấy dữ liệu gapminder
data(gapminder)

# Lọc dữ liệu: châu Á, năm 1952 và 2007
plotdata_long <- gapminder %>%
  filter(continent == "Asia", year %in% c(1952, 2007)) %>%
  select(country, year, lifeExp)

# Chuyển từ long sang wide
plotdata_wide <- plotdata_long %>%
  pivot_wider(names_from = year, values_from = lifeExp)

# Đổi tên cột
names(plotdata_wide) <- c("Country", "year1952", "year2007")

# Vẽ biểu đồ dumbbell bằng ggplot2 thuần
ggplot(plotdata_wide,
       aes(y = reorder(Country, year1952))) +

  # Đường nối
  geom_segment(aes(x = year1952, xend = year2007,
                   yend = reorder(Country, year1952)),
               color = "grey", linewidth = 1) +

  # Điểm năm 1952
  geom_point(aes(x = year1952),
             color = "orange", size = 3) +

  # Điểm năm 2007
  geom_point(aes(x = year2007),
             color = "green", size = 3) +

  theme_minimal() +
  labs(
    title = "Thay đổi tuổi thọ trung bình tại châu Á",
    subtitle = "So sánh năm 1952 và 2007",
    x = "Tuổi thọ trung bình (năm)",
    y = ""
  )