Task 1: Biểu đồ phân bố với hist

t = "file:///E:/Dropbox/Phuong Nam's research/Research documents/GS Nguyen Van Tuan/GS Tuan - Khoa hoc phan tich 2020-01-05/Textbook/TDTU Datasets for 2020 Workshop/PISA Data Vietnam 2015.csv"
pisa = read.csv(t, header = T)
#Thử nghiệm với hist
hist(pisa$Math, col = "blue")

hist(pisa$Math, col = "blue", border = "white")

hist(pisa$Math, col = "blue", border = "white", main = "Distribution of Math Scores", xlab = "Math Score")

hist(pisa$Math, col = "blue", border = "white", main = "Phân bố điểm môn toán", xlab = "Điểm môn toán", prob = T)
lines(density(pisa$Math), col = "red")

p1 = hist(pisa$Math[pisa$Gender=="Boys"], plot = F)
p2 = hist(pisa$Math[pisa$Gender=="Girls"], plot = F)
plot(p1, col = "blue", border = "white")
#Thêm "add = T" nếu muốn biểu đồ sau chồng lên biểu đồ trước
plot(p2, add = T, col = scales::alpha("red", 0.9), border = "white")

Task 2: Biểu đồ phân bố với lattice()

#Vẽ biểu đồ phân bố cho nhiều nhóm dùng hàm desityplot
library("lattice")
## Warning: package 'lattice' was built under R version 3.6.2
densityplot(~ Math, groups = Gender, data = pisa)

densityplot(~ Math, groups = Gender, data = pisa, auto.key = list(space = "right"))

densityplot(~ Math, groups = Area, data = pisa)

densityplot(~ Math, groups = Area, data = pisa, auto.key = list(space = "top"))

p1 = densityplot(~ Science, groups = Area, data = pisa, auto.key = list(space = "top"), ylim = c(0,0.007))
p2 = densityplot(~ Math, groups = Area, data = pisa, auto.key = list(space = "top"), ylim = c(0,0.007))
p3 = densityplot(~ Read, groups = Area, data = pisa, auto.key = list(space = "top"), ylim = c(0,0.007))
library("gridExtra")
## Warning: package 'gridExtra' was built under R version 3.6.2
grid.arrange(p1, p2, p3, ncol=3)

Task 3: Biểu đồ hộp với boxplot()

# Chia màn hình ra 3 cửa sổ 
par(mfrow=c(1, 3))
boxplot(pisa$Science, col="purple")
boxplot(pisa$Science ~ pisa$Gender, col=c("blue", "red"))
boxplot(pisa$Science ~ pisa$Region, col=c("blue", "red","purple"))

Task 4: Biểu đồ tương quan với plot()

# Chuyển màn hình về 1 cửa sổ
par(mfrow=c(1, 1))
# Tìm hiểu mối liên quan giữa PARED và điểm môn toán 
plot(pisa$Math ~ pisa$PARED, col="blue")
abline(lm(pisa$Math ~ pisa$PARED), col="red")

# Tìm hiểu mối liên quan giữa điểm môn khoa học và điểm môn toán
plot(pisa$Science ~ pisa$Math, col="blue")
abline(lm(pisa$Science ~ pisa$Math), col="red")

Task 5: Biểu đồ tương quan với ggplot2

ob = read.csv("file:///E:/Dropbox/Phuong Nam's research/Research documents/GS Nguyen Van Tuan/GS Tuan - Khoa hoc phan tich 2020-01-05/Textbook/TDTU Datasets for 2020 Workshop/Obesity data.csv")
ob$OB[ob$bmi < 18.5] = "Underweight"
ob$OB[ob$bmi >= 18.5 & ob$bmi <= 24.9] = "Normal"
ob$OB[ob$bmi >= 25.0 & ob$bmi <= 29.9] = "Overweight"
ob$OB[ob$bmi >= 30] = "Obese"
ob$OB = factor(ob$OB, levels=c("Underweight", "Normal","Overweight", "Obese"))
library("ggplot2")
## Warning: package 'ggplot2' was built under R version 3.6.2
p = ggplot(data = ob, aes(OB, fill = OB)) + geom_bar()
p = p + xlab("Obesity group") + ylab("Frequency")
p 

p + theme(legend.position = "none")

# Kiểm tra dữ liệu trống (nếu đồ thị có cột NA)
table(is.na(ob$OB))
## 
## FALSE 
##  1217

Task 6: Vẽ biểu đồ tương quan giữa weight và pcfat dùng ggplot2

# Biểu đồ đơn giản
library("ggplot2"); library("ggthemes"); library("gridExtra")
## Warning: package 'ggthemes' was built under R version 3.6.2
p = ggplot(data = ob, aes(x=weight, y=pcfat)) 
p = p + geom_point() + geom_smooth()
p = p + ggtitle("T\u01B0\u01A1ng quan gi\u1EEFa pcfat v\u00E0 weight") + xlab("Weight") + ylab("PcFat")
p = p + theme(plot.title=element_text(hjust=0.5))
p
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

# Để đánh title bằng tiếng Việt thì dán chữ tiếng Việt vào link sau: https://www.freeformatter.com/javascript-escape.html#ad-output
p = p + theme_economist()
p
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'

# Biểu đồ theo nhóm Nam/Nữ
p = ggplot(data=ob, aes(x=weight, y=pcfat, fill=gender, col=gender))
p = p + geom_point() + geom_smooth()
p = p + xlab("Weight") + ylab("Percent body fat") +
ggtitle("Weight and Percent Body Fat for men and women separately")
p = p + theme(plot.title=element_text(hjust=0.5))
p + theme_economist()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + theme_tufte()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + theme_few()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + theme_wsj()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + theme_clean()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

p + theme_hc()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Biểu đồ tương quan + biểu đồ phân bố dùng package ggExtra  
library(ggExtra)
## Warning: package 'ggExtra' was built under R version 3.6.2
p = ggplot(data=ob, aes(x=bmi, y=pcfat, fill=gender,col=gender))
p = p + geom_point() + geom_smooth()
ggMarginal(p, type="histogram", groupColour=T, groupFill=T)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Thử nghiệm thêm với type "density"
ggMarginal(p, type="density", groupColour=T, groupFill=T)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
# Thử nghiệm thêm với type "violin", "boxplot"
ggMarginal(p, type="violin", groupColour=T, groupFill=T)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## Warning: position_dodge requires non-overlapping x intervals
## Warning: position_dodge requires non-overlapping x intervals
# Thử nghiệm thêm với type "boxplot"
ggMarginal(p, type="boxplot", groupColour=T, groupFill=T)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

# Biểu đồ tương quan đa biến (package GGally)
# Chọn biến quan tâm 
library(GGally)
## Warning: package 'GGally' was built under R version 3.6.2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
dat = ob[, c("gender", "age", "bmi", "weight", "height","pcfat")]
ggpairs(dat) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Thêm màu theo nhóm  
ggpairs(data=dat, mapping = aes(color = gender)) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# So sánh với: 
ggpairs(data=ob, mapping = aes(color = gender), columns = c("age", "weight", "bmi", "pcfat"))

ggpairs(data=ob, mapping = aes(color = gender), columns = c("gender", "weight", "bmi", "pcfat"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggpairs(data=ob, mapping = aes(color = ob$OB), columns = c("OB", "weight", "bmi", "pcfat"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Task 7: Vẽ biểu đồ phân bố (histogram)

# Vẽ biểu đồ phân bố điểm môn khoa học  
library(gridExtra) 
p = ggplot(data=pisa, aes(x=Science))
p1 = p + geom_histogram(color="white", fill="blue")
p = ggplot(data=pisa, aes(x=Science))
p = p + geom_histogram(aes(y=..density..), color="white", fill="blue")
p2 = p + geom_density(col="red")
grid.arrange(p1, p2, ncol=2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Phân bố điểm môn khoa học theo vùng (Area)  
p = ggplot(data=pisa, aes(x=Science, fill=Area))
p1 = p + geom_histogram(position="dodge") 
p2 = ggplot(data=pisa, aes(x=Science, fill=Area, color=Area)) +
geom_density(alpha = 0.1) 
grid.arrange(p1, p2, nrow=2) 
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Histogram và xác suất tích lũy  
p = ggplot(data=pisa, aes(HISCED)) 
p = p + stat_ecdf(color="red", lwd=1) 
p = p + geom_bar(aes(y = (..count..)/sum(..count..)),
fill="blue", colour='yellow')
p
## Warning: Removed 14 rows containing non-finite values (stat_ecdf).
## Warning: Removed 14 rows containing non-finite values (stat_count).

Task 8: Vẽ biểu đồ hộp (box plot)

# Biểu đồ hộp theo vùng (Bắc, Trung, Nam)  
p = ggplot(data=pisa, aes(x=Area, y=Science, col=Area, fill=Area))
p1 = p + geom_boxplot(col="black")
p2 = p + geom_boxplot(col="black") + geom_jitter(alpha=0.05)
grid.arrange(p1, p2, ncol=2)

# Biểu đồ hộp theo kinh tế của Cha Mẹ   
p = ggplot(data=pisa, aes(x=PARED, y=Science, fill=PARED))
p1 = p + geom_boxplot(col="black") + geom_jitter(alpha=0.02)
p = ggplot(data=na.omit(pisa), aes(x=factor(PARED), y=Science, fill=factor(PARED), color=factor(PARED)))
p2 = p + geom_boxplot(col="black") + geom_jitter(alpha=0.05)
grid.arrange(p1, p2, ncol=2)
## Warning: Continuous x aesthetic -- did you forget aes(group=...)?
## Warning: Removed 14 rows containing missing values (stat_boxplot).
## Warning: Removed 14 rows containing missing values (geom_point).