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)
#install.packages("gapminder")
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.
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
dim(gapminder)
## [1] 1704 6
#Xem cấu trúc biến
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 từng châu lục bằng table
table(gapminder$continent)
##
## Africa Americas Asia Europe Oceania
## 624 300 396 360 24
gapminder %>%
filter(
year == 2007,
continent == "Asia",
country == "China"
) %>%
summarise(mean(lifeExp))
## # A tibble: 1 × 1
## `mean(lifeExp)`
## <dbl>
## 1 73.0
#Lọc dữ liệu
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ê với summarize
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.
#Lọc dữ liệu cho năm2007
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.
#Mô phỏng trực quan
ggplot(data= gapminder2007, mapping= aes(x= gdpPercap,y= lifeExp,color= continent)) +
geom_point()+
scale_x_log10()

#Thể hiện bubblechart theo quy mô dân số
ggplot(data= gapminder2007, mapping= aes(x= gdpPercap,y= lifeExp,color= continent,size= pop))+
geom_point()+
scale_x_log10()

# Vẽ đường hồi quy với geom_smooth.
# Tham số method = "loess" để chỉ ra sử dụng phương pháp bình phương tối thiểu LMSE
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?

# Gán nhãn cho biểu đồ
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp, size = pop)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
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?

# Phong cách tạp chí The Economist
#install.packages("ggthemes")
library(ggthemes)
ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) +
geom_point(aes(color = continent)) +
geom_smooth(method = "loess") +
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)) +
theme_economist()
## `geom_smooth()` using formula = 'y ~ x'

# Lọc dữ liệu năm 2007
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.
#Dùng histogram
ggplot(data= gapminder2007, mapping= aes(gdpPercap)) +
geom_histogram(fill= "#00adef", color= "red",bins= 20) +
labs(title= "DistributionofGDP perCapitain 2007", y= "Frequency")

#Dùng histogram
#install.packages("scales")
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 Capitain 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.

#Biểu đồ mật độ xác suất thông thường
ggplot(data= gapminder2007, mapping= aes(gdpPercap, fill= continent))+
geom_density(alpha= 0.7)

# Biểu đồ Ridgeplot
#install.packages("ggridges")
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

#Lọc ra dữ liệu các quốc gia ở châu Á
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.
#Lọc ra dữ liệu cho các quốc gia ở châu Âu
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.
#Trực quan dữ liệu châu Á để so sánh
ggplot(data= asia, mapping= aes(x= country,y= lifeExp,fill= country)) +
geom_bar(stat= "identity", width= 0.9) +
coord_flip()

#Sắp xếp lại trật tự cho châu Á và gán cho đối tượng là 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="Life Expectancy of Asia")->graph1
graph1

#Tương tự với châu Âu gán cho đối tượng là 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="Life Expectancy of Europe")->graph2
graph2

#Hiển thị nhiều biểu đồ cùng lúc
#install.packages("gridExtra")
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
grid.arrange(graph1, graph2, ncol = 2)
