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.

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)
# Lấy dữ liệu
data("gapminder")
# Thống kê 10 dòng đầu dữ liệu, 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.
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 ...
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  
## 
table(gapminder$continent)
## 
##   Africa Americas     Asia   Europe  Oceania 
##      624      300      396      360       24
gapminder %>%
  filter(
    continent == "Asia",
    country == "Japan",
    year %in% c(1997, 2002, 2007)
)
## # A tibble: 3 × 6
##   country continent  year lifeExp       pop gdpPercap
##   <fct>   <fct>     <int>   <dbl>     <int>     <dbl>
## 1 Japan   Asia       1997    80.7 125956499    28817.
## 2 Japan   Asia       2002    82   127065841    28605.
## 3 Japan   Asia       2007    82.6 127467972    31656.
gapminder %>%
  filter(
    year == 2007,
    continent == "Asia",
    country == "Japan"
    ) %>%
    summarise(mean(lifeExp))
## # A tibble: 1 × 1
##   `mean(lifeExp)`
##             <dbl>
## 1            82.6
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
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
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.
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.
ggplot(data = gapminder2007, mapping = aes(x = gdpPercap,y = lifeExp, color = continent)) +
  geom_point() +
  scale_x_log10()

ggplot(data = gapminder2007, mapping = aes(x = gdpPercap,y = lifeExp, color = continent, size = pop)) +
  geom_point() +
  scale_x_log10()

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, size = pop)) +
  geom_point(aes(color = continent)) +
  geom_smooth(method = "loess") +
  scale_x_log10() 
## 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.
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, size = pop)) +
  geom_point(aes(color = continent)) +
  geom_smooth(method = "loess", se = FALSE) +  
  scale_x_log10() +  
  labs(x = "Log GDP per Capita", y = "Life Expectancy") +  
  ggtitle("Association between GDP Per Capita and Life Expectancy") +  
  theme(plot.title = element_text(face = "bold", hjust = 0.5)) 
## `geom_smooth()` using formula = 'y ~ x'
## Warning: The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

library(ggthemes)
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
  geom_point(aes(color = continent)) +
  geom_smooth(method = "loess", se = FALSE) + 
  scale_x_log10() +  # Đổi trục x sang log-scale
  labs(x = "Log GDP per Capita", y = "Life Expectancy") + 
  ggtitle("Association between GDP Per Capita and Life Expectancy") + 
  theme(plot.title = element_text(face = "bold", hjust = 0.5)) +  
  theme_economist() 
## `geom_smooth()` using formula = 'y ~ x'

gapminder %>%
  filter(year == 2007) -> gapminder2007
  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.
ggplot(data = gapminder2007, mapping = aes(gdpPercap)) +
  geom_histogram(fill = "#00adef", color = "red", bins = 20) +
  labs(title = "Distribution of GDP per Capita in 2007", y = "Frequency")

library(ggplot2)
library(scales)
ggplot(data = gapminder2007, mapping = aes(x = gdpPercap, y = ..count../sum(..count..))) +
  geom_histogram(fill = "#00adef", color = "red", bins = 20) +
  scale_y_continuous(labels = percent) +
  labs(title = "Distribution of GDP per Capita in 2007", y = "Frequency")
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ggplot(data = gapminder2007, mapping = aes(gdpPercap, fill = continent)) +
  geom_density(alpha = 0.7)

library(ggridges)
ggplot(data = gapminder2007, aes(x = gdpPercap, y = continent, fill = continent)) +
  geom_density_ridges(alpha = 0.7) +
  theme_ridges() +
  labs("RidgePlot for GDPPerCap") +
  theme(legend.position = "none")
## Picking joint bandwidth of 3510

library(dplyr)
library(gapminder)
asia <- gapminder %>%
  filter(continent == "Asia" & year == 2007)

asia %>%
  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 Bahrain          Asia       2007    75.6     708573    29796.
##  3 Bangladesh       Asia       2007    64.1  150448339     1391.
##  4 Cambodia         Asia       2007    59.7   14131858     1714.
##  5 China            Asia       2007    73.0 1318683096     4959.
##  6 Hong Kong, China Asia       2007    82.2    6980412    39725.
##  7 India            Asia       2007    64.7 1110396331     2452.
##  8 Indonesia        Asia       2007    70.6  223547000     3541.
##  9 Iran             Asia       2007    71.0   69453570    11606.
## 10 Iraq             Asia       2007    59.5   27499638     4471.
europe <- gapminder %>%
  filter(continent == "Europe" & year == 2007)
europe %>%
  head(n=10)
## # A tibble: 10 × 6
##    country                continent  year lifeExp      pop gdpPercap
##    <fct>                  <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Albania                Europe     2007    76.4  3600523     5937.
##  2 Austria                Europe     2007    79.8  8199783    36126.
##  3 Belgium                Europe     2007    79.4 10392226    33693.
##  4 Bosnia and Herzegovina Europe     2007    74.9  4552198     7446.
##  5 Bulgaria               Europe     2007    73.0  7322858    10681.
##  6 Croatia                Europe     2007    75.7  4493312    14619.
##  7 Czech Republic         Europe     2007    76.5 10228744    22833.
##  8 Denmark                Europe     2007    78.3  5468120    35278.
##  9 Finland                Europe     2007    79.3  5238460    33207.
## 10 France                 Europe     2007    80.7 61083916    30470.
ggplot(data = asia, mapping = aes(x = country, y = lifeExp, fill = country)) +
  geom_bar(stat = "identity", width = 0.9) +
  coord_flip()

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="Life Expectancy of Asia") -> graph1
  graph1

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="Life Expectancy of Europe") -> graph2
  graph2