t= ("/Users/locnguyen/Documents/R Console/Datasets for practice/obesity data.csv")
ob=read.csv(t)
head(ob)
##   id gender height weight  bmi age  bmc  bmd   fat  lean pcfat
## 1  1      F    150     49 21.8  53 1312 0.88 17802 28600  37.3
## 2  2      M    165     52 19.1  65 1309 0.84  8381 40229  16.8
## 3  3      F    157     57 23.1  64 1230 0.84 19221 36057  34.0
## 4  4      F    156     53 21.8  56 1171 0.80 17472 33094  33.8
## 5  5      M    160     51 19.9  54 1681 0.98  7336 40621  14.8
## 6  6      F    153     47 20.1  52 1358 0.91 14904 30068  32.2

#ve bieu do so sanh Pcfat voi gender

library(ggplot2)
## Registered S3 methods overwritten by 'ggplot2':
##   method         from 
##   [.quosures     rlang
##   c.quosures     rlang
##   print.quosures rlang
p= ggplot(data=ob, aes(x=pcfat, fill=gender, col=gender))
ob$obesity [ob$bmi<18.5]= "underweight"
ob$obesity [ob$bmi>=18.5 & ob$bmi<25]= "normal"
ob$obesity [ob$bmi>=25 & ob$bmi<30]= "overweight"
ob$obesity [ob$bmi>300]= "obese"
head(ob, 3)
##   id gender height weight  bmi age  bmc  bmd   fat  lean pcfat obesity
## 1  1      F    150     49 21.8  53 1312 0.88 17802 28600  37.3  normal
## 2  2      M    165     52 19.1  65 1309 0.84  8381 40229  16.8  normal
## 3  3      F    157     57 23.1  64 1230 0.84 19221 36057  34.0  normal
ggplot(data=ob, aes(x=pcfat, fill=obesity, col= obesity)) + geom_density(alpha=0.1)

# ve bieu do hop

p= ggplot(data=ob, aes(x=gender, y=pcfat, color=gender))
p + geom_boxplot()

p1=p+geom_boxplot()+ geom_jitter(alpha=0.05)

#histogram

library(ggplot2)
p= ggplot(data=ob, aes( x = pcfat, fill = obesity, col=obesity))
p2= p+ geom_density(alpha=0.1)
library(gridExtra)
grid.arrange(p1, p2, ncol=2)

#ve bieu do tuong quan - scatter plot

library(ggplot2)
p= ggplot(data=ob, aes(x=bmi, y=pcfat))
p + geom_point()

#ve bieu do theo nhom gender

p= ggplot(data=ob, aes(x=bmi, y=pcfat, fill=gender, col=gender))
p + geom_point()

# ve bieu do theo nhom gender va liner model

p= ggplot(data=ob, aes(x=bmi, y=pcfat, fill=gender, col=gender))
p + geom_point() + geom_smooth(method="lm", formula = y~x+I(x^2)) + xlab("body mass index")+ ylab("percent body fat")

library(ggthemes)

p3= p + geom_point() + geom_smooth(method="lm", formula = y~x+I(x^2)) + xlab("body mass index")+ ylab("percent body fat")
p3+ theme_economist()+ggtitle("this is tittle")