#Cài đặt các packages cần thiết: library(ggplot2); library(tidyverse); library(gridExtra) library(readxl) #Việc 1: Đọc dữ liệu vào R
bw = read.csv("/Users/marcelkhoaphuoc/Documents/Data R/Dataset for TDTU workshop 4-2022/obesity data.csv")
#Việc 2: Biên tập dữ liệu bằng tidyverse (2.1) Mã hoá biến gender (F/M) thành sex với giá trị 1/0
bw$sex[bw$gender == "F"] = 1
bw$sex[bw$gender == "M"] = 0
head(bw,9)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex
## 1 1 F 150 49 21.8 53 1312 0.88 17802 28600 37.3 1
## 2 2 M 165 52 19.1 65 1309 0.84 8381 40229 16.8 0
## 3 3 F 157 57 23.1 64 1230 0.84 19221 36057 34.0 1
## 4 4 F 156 53 21.8 56 1171 0.80 17472 33094 33.8 1
## 5 5 M 160 51 19.9 54 1681 0.98 7336 40621 14.8 0
## 6 6 F 153 47 20.1 52 1358 0.91 14904 30068 32.2 1
## 7 7 F 155 58 24.1 66 1546 0.96 20233 35599 35.3 1
## 8 8 M 167 65 23.3 50 2276 1.11 17749 43301 28.0 0
## 9 9 M 165 54 19.8 61 1778 0.96 10795 38613 21.1 0
(2.2) Mã hoá biến bmi thành 4 nhóm như sau:
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
bw = bw %>% mutate(obese = cut(bmi,breaks=c(-Inf, 18.5, 24.9, 29.9, Inf),
labels=c("Underweight","Normal","Overweight", "Obese")))
head(bw)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex obese
## 1 1 F 150 49 21.8 53 1312 0.88 17802 28600 37.3 1 Normal
## 2 2 M 165 52 19.1 65 1309 0.84 8381 40229 16.8 0 Normal
## 3 3 F 157 57 23.1 64 1230 0.84 19221 36057 34.0 1 Normal
## 4 4 F 156 53 21.8 56 1171 0.80 17472 33094 33.8 1 Normal
## 5 5 M 160 51 19.9 54 1681 0.98 7336 40621 14.8 0 Normal
## 6 6 F 153 47 20.1 52 1358 0.91 14904 30068 32.2 1 Normal
tail(bw)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex
## 1212 1222 F 153 50 21.4 59 1309 0.87 18328 29147 37.6 1
## 1213 1223 F 150 44 19.6 44 1474 0.95 12906 28534 30.1 1
## 1214 1224 F 148 51 23.3 58 1522 0.97 14938 33931 29.6 1
## 1215 1225 F 149 50 22.5 57 1409 0.93 16777 30598 34.4 1
## 1216 1226 F 144 49 23.6 67 1266 0.90 20094 27272 41.3 1
## 1217 1227 F 141 45 22.6 58 1228 0.91 14567 28111 33.2 1
## obese
## 1212 Normal
## 1213 Normal
## 1214 Normal
## 1215 Normal
## 1216 Normal
## 1217 Normal
(2.3) Dùng hàm mutate tính toán lượng cơ (lean) và mỡ (fat) bằng đơn vị kg:
bw = bw %>% mutate(lean = lean/1000,fat = fat/1000)
head(bw)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex obese
## 1 1 F 150 49 21.8 53 1312 0.88 17.802 28.600 37.3 1 Normal
## 2 2 M 165 52 19.1 65 1309 0.84 8.381 40.229 16.8 0 Normal
## 3 3 F 157 57 23.1 64 1230 0.84 19.221 36.057 34.0 1 Normal
## 4 4 F 156 53 21.8 56 1171 0.80 17.472 33.094 33.8 1 Normal
## 5 5 M 160 51 19.9 54 1681 0.98 7.336 40.621 14.8 0 Normal
## 6 6 F 153 47 20.1 52 1358 0.91 14.904 30.068 32.2 1 Normal
tail(bw)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex
## 1212 1222 F 153 50 21.4 59 1309 0.87 18.328 29.147 37.6 1
## 1213 1223 F 150 44 19.6 44 1474 0.95 12.906 28.534 30.1 1
## 1214 1224 F 148 51 23.3 58 1522 0.97 14.938 33.931 29.6 1
## 1215 1225 F 149 50 22.5 57 1409 0.93 16.777 30.598 34.4 1
## 1216 1226 F 144 49 23.6 67 1266 0.90 20.094 27.272 41.3 1
## 1217 1227 F 141 45 22.6 58 1228 0.91 14.567 28.111 33.2 1
## obese
## 1212 Normal
## 1213 Normal
## 1214 Normal
## 1215 Normal
## 1216 Normal
## 1217 Normal
#Việc 3: Soạn biểu đồ phân bố biến số dùng ggplot (3.1) Thể hiện phân bố tỉ trọng mỡ (pcfat) bằng biểu đồ histogram.
library(ggplot2)
p = ggplot(data=bw, aes(x=pcfat))
p1 = p + geom_histogram()
p1
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Biểu đồ 2:
library(ggplot2)
p = ggplot(data=bw, aes(x=pcfat))
p2 = p + geom_histogram(color="white", fill="blue")
p2
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Biểu đồ 3:
library(ggplot2)
p = ggplot(data=bw, aes(x=pcfat))
p3 = p + geom_histogram(aes(y = ..density..), col = "white", fill = "blue") + geom_density(col = "red")
p3
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Biểu đồ 4
p4 = p3 +labs(x="Number of people", y="Percent body fat")+ggtitle("Distribution of percent body fat")
p4
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
(3.2) Hãy vẽ biểu đồ phân bố tỉ trọng mỡ (pcfat) theo giới tính: Biểu đồ
1
p = ggplot(data=bw, aes(x=pcfat, fill=gender))
p5 = p + geom_histogram(position="dodge")
p5
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Biểu đồ 2:
p6 = ggplot(data=bw, aes(x=pcfat,fill=gender, color=gender)) + geom_density(alpha = 0.1)
p6
Kết hợp 2 biểu đồ:
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
grid.arrange(p5, p6, nrow=2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Việc 4: Soạn biểu đồ thanh Trong nghiên cứu ob, có 4 nhóm cá nhân được
phân chia theo tình trạng béo phỉ (obese). (4.1) Hãy thể hiện phân bố
của tình trạng béo phì bằng biểu đồ thanh. Biểu đồ 1:
p = ggplot(data=bw, aes(x=obese))
p7 = p + geom_bar()
p7
Biểu đồ 2:
px =p+ geom_bar(data=bw, aes(x=obese, fill=obese, col=obese ))
px
(4.2) Hãy thể hiện phân bố của tình trạng béo phì (obese) theo giới tính
(gender):
library(ggplot2)
p = ggplot(data=bw, aes(x=obese, fill=gender,col=gender), y="percent", coord_flip())
p8 = p + geom_bar(position="fill")
p8
bw %>% count(obese, gender) %>% group_by(obese) %>% mutate(percent = n / sum(n) * 100) %>% ggplot(aes(x=obese, y=percent, fill=gender)) + geom_bar(stat="identity")
bw %>% count(obese, gender) %>% group_by(obese) %>% mutate(percent = n / sum(n) * 100) %>% ggplot(aes(x=obese, y=percent, fill=gender)) + geom_bar(stat="identity") + geom_text(aes(label=paste0(sprintf("%1.1f", percent),"%")), position=position_stack(vjust=0.5))
(4.3) Làm thể nào để thêm số phần trăm của phân bố của tình trạng béo
phì theo giới tính? Vẽ biểu đồ hiện tỉ lệ %
“geom_text(aes(label=paste0(sprintf(”%1.1f”, percent),“%”)),
position=position_stack(vjust=0.5))”
p = ggplot(data=bw, aes(x=obese, fill=gender,col=gender), stat="percent", coord_flip())
p8 = p + geom_bar(position="fill")
p8
bw %>% count(obese, gender) %>% group_by(obese) %>% mutate(percent = n / sum(n) * 100) %>% ggplot(aes(x=obese, y=percent, fill=gender)) + geom_bar(stat="identity") + geom_text(aes(label=paste0(sprintf("%1.1f", percent),"%")), position=position_stack(vjust=0.5))
(4.4) Hãy vẽ biểu đồ thanh dựa vào số trung bình: (i) qua hai bước: tính
trung bình pcfat và vẽ theo nhóm obesity
p4.4 = ggplot(bw, aes(x = obese, y = bmi, fill = obese)) + stat_summary(tmp=mean_sdl, geom="bar") + labs(x = "Obesity", y = "Mean BMI")
## Warning: Ignoring unknown parameters: tmp
#Việc 5: Soạn biểu đồ hộp Biểu đồ hộp thường hay được sử dụng để so sánh biến tục giữa hai hay hơn 2 nhóm. (5.1) Các bạn hãy dùng biểu đồ so sánh phân bố của tỉ trọng mỡ (pcfat) giữa các nhóm béo phì (obese) cho nữ giới. (i) Tạo 1 tập dữ liệu chỉ gồm nữ giới
temp = bw %>% filter(sex == 1)
head(temp)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex obese
## 1 1 F 150 49 21.8 53 1312 0.88 17.802 28.600 37.3 1 Normal
## 2 3 F 157 57 23.1 64 1230 0.84 19.221 36.057 34.0 1 Normal
## 3 4 F 156 53 21.8 56 1171 0.80 17.472 33.094 33.8 1 Normal
## 4 6 F 153 47 20.1 52 1358 0.91 14.904 30.068 32.2 1 Normal
## 5 7 F 155 58 24.1 66 1546 0.96 20.233 35.599 35.3 1 Normal
## 6 10 F 158 60 24.0 58 1404 0.86 21.365 35.534 36.6 1 Normal
p = ggplot(data=temp, aes(x=obese, y=pcfat, fill=obese))
p9 = p + geom_boxplot()
p9
Biểu đồ 3
library(ggplot2)
p = ggplot(data=temp, aes(x=obese, y=pcfat, fill=obese, col=obese))
p10 = p + geom_boxplot(col="black") + geom_jitter(alpha=0.15)
p10
Biểu đồ 4
p = ggplot(data=temp, aes(x=obese, y=pcfat, fill=obese, col=obese))
p10 = p + geom_boxplot(col="black") + geom_jitter(alpha=0.15) + labs(x="Number of people", y="Obesity Group")
p10
(5.2) Các bạn hãy lặp lại biểu đồ trên cho nam giới:
temp2 = bw %>% filter(sex == 0)
head(temp2)
## id gender height weight bmi age WBBMC wbbmd fat lean pcfat sex obese
## 1 2 M 165 52 19.1 65 1309 0.84 8.381 40.229 16.8 0 Normal
## 2 5 M 160 51 19.9 54 1681 0.98 7.336 40.621 14.8 0 Normal
## 3 8 M 167 65 23.3 50 2276 1.11 17.749 43.301 28.0 0 Normal
## 4 9 M 165 54 19.8 61 1778 0.96 10.795 38.613 21.1 0 Normal
## 5 12 M 165 65 23.9 50 1878 0.97 18.100 43.391 28.6 0 Normal
## 6 14 M 158 57 22.8 84 1772 1.02 13.398 40.313 24.1 0 Normal
px = ggplot(data=temp2, aes(x=obese, y=pcfat, fill=obese, col=obese))
p11 = px + geom_boxplot(col="black") + geom_jitter(alpha=0.15)
p11
p12 = px + geom_boxplot(col="black") + geom_jitter(alpha=0.15) + labs(x="Number of people", y="Obesity Group")
p12
#(5.3) Làm thế nào để trình bày trong 1 trang bằng cách dùng
gridExtra?
library(gridExtra)
p13 = p10 + theme(legend.position="none") + labs(x="Obeseity group", y="Percent body fat (%)")
p12 = p12 + theme(legend.position="none") + labs(x="Obeseity group", y="Percent body fat (%)")
grid.arrange(p12,p13,ncol=2)
Việc 6: Soạn biểu đồ tương quan Đây là loại biểu đồ dùng để mô tả mối
liên quan giữa 2 biến liên tục (continuous variables). Tuy nhiên, mối
liên quan có thể phân chia theo nhóm qua một biến phân loại (categorial
variables). (6.1) Các bạn hãy thể hiện mối liên quan giữa chỉ số khối cơ
thể (bmi) và tỉ trọng mỡ (pcfat) bằng hàm ggplot trong package
ggplot2.
p14 = ggplot(data=bw, aes(x=bmi, y=pcfat))
p14 = p14 + geom_point()
p14
biểu đồ 2:
p15 = p14 + geom_point()+geom_smooth()
p15
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
biểu đồ 3:
p15 = p14 + geom_point()+geom_smooth(lwd=2, se=F, method="lm")
p15
## `geom_smooth()` using formula 'y ~ x'