# Cài đặt các thư viện cần thiết
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(ggplot2)
library(gapminder)
library(ggthemes)
library(gridExtra)
## 
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
## 
##     combine
library(ggridges)
library(scales)

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

# Thống kê 10 dòng đầu và 10 dòng cuối dữ liệu
head(gapminder, n = 10)
## # A tibble: 10 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
tail(gapminder, n = 10)
## # A tibble: 10 × 6
##    country  continent  year lifeExp      pop gdpPercap
##    <fct>    <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Zimbabwe Africa     1962    52.4  4277736      527.
##  2 Zimbabwe Africa     1967    54.0  4995432      570.
##  3 Zimbabwe Africa     1972    55.6  5861135      799.
##  4 Zimbabwe Africa     1977    57.7  6642107      686.
##  5 Zimbabwe Africa     1982    60.4  7636524      789.
##  6 Zimbabwe Africa     1987    62.4  9216418      706.
##  7 Zimbabwe Africa     1992    60.4 10704340      693.
##  8 Zimbabwe Africa     1997    46.8 11404948      792.
##  9 Zimbabwe Africa     2002    40.0 11926563      672.
## 10 Zimbabwe Africa     2007    43.5 12311143      470.
# Xem chiều dữ liệu và cấu trúc của bộ dữ liệu
dim(gapminder)
## [1] 1704    6
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
##  $ country  : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
##  $ year     : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
##  $ lifeExp  : num [1:1704] 28.8 30.3 32 34 36.1 ...
##  $ pop      : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
##  $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
# Thống kê tổng quan
summary(gapminder)
##         country        continent        year         lifeExp     
##  Afghanistan:  12   Africa  :624   Min.   :1952   Min.   :23.60  
##  Albania    :  12   Americas:300   1st Qu.:1966   1st Qu.:48.20  
##  Algeria    :  12   Asia    :396   Median :1980   Median :60.71  
##  Angola     :  12   Europe  :360   Mean   :1980   Mean   :59.47  
##  Argentina  :  12   Oceania : 24   3rd Qu.:1993   3rd Qu.:70.85  
##  Australia  :  12                  Max.   :2007   Max.   :82.60  
##  (Other)    :1632                                                
##       pop              gdpPercap       
##  Min.   :6.001e+04   Min.   :   241.2  
##  1st Qu.:2.794e+06   1st Qu.:  1202.1  
##  Median :7.024e+06   Median :  3531.8  
##  Mean   :2.960e+07   Mean   :  7215.3  
##  3rd Qu.:1.959e+07   3rd Qu.:  9325.5  
##  Max.   :1.319e+09   Max.   :113523.1  
## 
# Thống kê số lượng các quốc gia theo từng châu lục
table(gapminder$continent)
## 
##   Africa Americas     Asia   Europe  Oceania 
##      624      300      396      360       24
# Lọc dữ liệu cho Việt Nam trong các năm 1997, 2002 và 2007
gapminder %>% filter(continent == "Asia", country == "Vietnam", year %in% c(1997, 2002, 2007))
## # A tibble: 3 × 6
##   country continent  year lifeExp      pop gdpPercap
##   <fct>   <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Vietnam Asia       1997    70.7 76048996     1386.
## 2 Vietnam Asia       2002    73.0 80908147     1764.
## 3 Vietnam Asia       2007    74.2 85262356     2442.
# Tính tuổi thọ trung bình của các quốc gia theo từng châu lục trong năm 2007
gapminder %>%
  filter(year == 2007) %>%
  group_by(continent) %>%
  summarise(average_lifeExp = mean(lifeExp))
## # A tibble: 5 × 2
##   continent average_lifeExp
##   <fct>               <dbl>
## 1 Africa               54.8
## 2 Americas             73.6
## 3 Asia                 70.7
## 4 Europe               77.6
## 5 Oceania              80.7
# Thống kê tổng dân số theo từng châu lục trong năm 2007
gapminder %>%
  filter(year == 2007) %>%
  group_by(continent) %>%
  summarise(total_pop = sum(pop)) %>%
  arrange(desc(total_pop))
## # A tibble: 5 × 2
##   continent  total_pop
##   <fct>          <dbl>
## 1 Asia      3811953827
## 2 Africa     929539692
## 3 Americas   898871184
## 4 Europe     586098529
## 5 Oceania     24549947
# Thêm cột GDP trong năm 2007 và hiển thị 10 dòng đầu tiên
gapminder %>%
  filter(year == 2007) %>%
  mutate(totalGDP = gdpPercap * pop) %>%
  head(n = 10)
## # A tibble: 10 × 7
##    country     continent  year lifeExp       pop gdpPercap      totalGDP
##    <fct>       <fct>     <int>   <dbl>     <int>     <dbl>         <dbl>
##  1 Afghanistan Asia       2007    43.8  31889923      975.  31079291949.
##  2 Albania     Europe     2007    76.4   3600523     5937.  21376411360.
##  3 Algeria     Africa     2007    72.3  33333216     6223. 207444851958.
##  4 Angola      Africa     2007    42.7  12420476     4797.  59583895818.
##  5 Argentina   Americas   2007    75.3  40301927    12779. 515033625357.
##  6 Australia   Oceania    2007    81.2  20434176    34435. 703658358894.
##  7 Austria     Europe     2007    79.8   8199783    36126. 296229400691.
##  8 Bahrain     Asia       2007    75.6    708573    29796.  21112675360.
##  9 Bangladesh  Asia       2007    64.1 150448339     1391. 209311822134.
## 10 Belgium     Europe     2007    79.4  10392226    33693. 350141166520.
# Lọc dữ liệu cho năm 2007
gapminder2007 <- gapminder %>% filter(year == 2007)
gapminder2007 %>% head(n=10)
## # A tibble: 10 × 6
##    country     continent  year lifeExp       pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>     <int>     <dbl>
##  1 Afghanistan Asia       2007    43.8  31889923      975.
##  2 Albania     Europe     2007    76.4   3600523     5937.
##  3 Algeria     Africa     2007    72.3  33333216     6223.
##  4 Angola      Africa     2007    42.7  12420476     4797.
##  5 Argentina   Americas   2007    75.3  40301927    12779.
##  6 Australia   Oceania    2007    81.2  20434176    34435.
##  7 Austria     Europe     2007    79.8   8199783    36126.
##  8 Bahrain     Asia       2007    75.6    708573    29796.
##  9 Bangladesh  Asia       2007    64.1 150448339     1391.
## 10 Belgium     Europe     2007    79.4  10392226    33693.
# Biểu đồ điểm thể hiện mối quan hệ giữa GDP và tuổi thọ
ggplot(data = gapminder2007, mapping = aes(x = gdpPercap, y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10() +
  labs(x = "Log GDP per Capita", y = "Life Expectancy", title = "Mối quan hệ giữa GDP và Tuổi thọ")

# Biểu đồ Bubble với kích thước theo dân số
ggplot(data = gapminder2007, mapping = aes(x = gdpPercap, y = lifeExp, color = continent, size = pop)) +
  geom_point() +
  scale_x_log10() +
  labs(x = "Log GDP per Capita", y = "Life Expectancy", title = "Mối quan hệ giữa GDP, Tuổi thọ và Dân số")

# Biểu đồ mật độ xác suất
ggplot(data = gapminder2007, mapping = aes(gdpPercap, fill = continent)) +
  geom_density(alpha = 0.7) +
  scale_x_log10() +
  labs(title = "Biểu đồ mật độ phân phối GDP per Capita")

# Biểu đồ Ridgeplot cho GDP per Capita theo các châu lục
ggplot(data = gapminder2007, aes(x = gdpPercap, y = continent, fill = continent)) +
  geom_density_ridges(alpha = 0.7) +
  theme_ridges() +
  labs(title = "RidgePlot cho GDP per Capita theo các châu lục") +
  theme(legend.position = "none")
## Picking joint bandwidth of 3510

# Lọc dữ liệu cho các quốc gia ở Châu Á và Châu Âu
asia <- gapminder %>% filter(continent == "Asia" & year == 2007)
europe <- gapminder %>% filter(continent == "Europe" & year == 2007)

# Biểu đồ so sánh tuổi thọ giữa các quốc gia ở châu Á
graph1 <- ggplot(data = asia, mapping = aes(x = reorder(country, lifeExp), y = lifeExp, fill = country)) +
  geom_bar(stat = "identity", width = 0.9) +
  coord_flip() +
  theme(legend.position = "none") +
  labs(x = "", y = "Tuổi thọ của Châu Á")

# Biểu đồ so sánh tuổi thọ giữa các quốc gia ở châu Âu
graph2 <- ggplot(data = europe, mapping = aes(x = reorder(country, lifeExp), y = lifeExp, fill = country)) +
  geom_bar(stat = "identity", width = 0.9) +
  coord_flip() +
  theme(legend.position = "none") +
  labs(x = "", y = "Tuổi thọ của Châu Âu")

# Hiển thị 2 biểu đồ Châu Á và Châu Âu cùng một lúc
grid.arrange(graph1, graph2, ncol = 2)

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.