vẽ histogram
t="C:\\Users\\pc\\Downloads\\CAC NGHIEN CUU\\BAI GIANG CAC MON\\GS TUAN\\BG 12.6.19\\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
#analysis: mô tả phân bố của pcfat cho nam và nữ
# vẽ histogram với cột màu cam và viền màu trắng
library(ggplot2)
p=ggplot(data=ob,aes(x=pcfat))
p=p+geom_histogram(fill="orange", col="white")
p
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#vẽ histogram theo tỉ trọng density
p=p+geom_histogram(aes(y=..density..),fill="orange", col="white") + geom_density(col="red")
p
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# vẽ histogram tỉ trọng theo giới tính
p=ggplot(data=ob,aes(x=pcfat, fill=gender,col=gender))
p=p+geom_histogram(aes(y=..density..),fill="orange", col="white") + geom_density(col="red")
p
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# vẽ histogram không có bar nữa
p=ggplot(data=ob,aes(x=pcfat, fill=gender,col=gender))
p=ggplot(data=ob,aes(x=pcfat, fill=gender,col=gender))+geom_density(alpha=0.1)
p
# đặt thêm biến mới từ biến cũ
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>=30]="obese"
head(ob)
## 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
## 4 4 F 156 53 21.8 56 1171 0.80 17472 33094 33.8 Normal
## 5 5 M 160 51 19.9 54 1681 0.98 7336 40621 14.8 Normal
## 6 6 F 153 47 20.1 52 1358 0.91 14904 30068 32.2 Normal
# vẽ histogram cho biến mới, alpha là độ đậm của màu.
p=ggplot(data=ob, aes(x=pcfat, fill=obesity, col=obesity))+geom_density(alpha=0.1)
p
# vẽ biểu đồ hộp
p=ggplot(data=ob,aes(x=gender, y=pcfat, color=gender))
p+geom_boxplot()
p+geom_boxplot()+geom_jitter(alpha=0.3)
# để cả hai biểu đồ boxplot và histogram để cùng nhau:
#boxplot
p=ggplot(data=ob, aes(x=gender, y=pcfat, color=gender))
p1=p+ geom_boxplot()+ geom_jitter(alpha =0.05)
#histogram
p=ggplot(data=ob, aes(x=pcfat, fill=gender, col=gender))
p2=p+geom_density(alpha =0.1)
library(gridExtra)
grid.arrange(p1,p2,ncol=2)
#biểu đồ tương quan-scatterplot
p=ggplot(data=ob, aes(x=bmi, y=pcfat))
p+geom_point()
p
# vẽ biểu đồ theo gender
p=ggplot(data=ob, aes(x=bmi, y=pcfat, fill=gender, col=gender))
p+geom_point()
p
# vẽ biểu đồ gender và linear bằng thêm geom_smooth(method="lm")
p=ggplot(data=ob, aes(x=bmi, y=pcfat, fill=gender, col=gender))
p+geom_point()+geom_smooth(method="lm")
# thêm phương trình cho biểu đồ
p=ggplot(data=ob, aes(x=bmi,y=pcfat, fill=gender, col=gender))
p=p+geom_point()+geom_smooth(method="lm", formula = y~x+I(x^2))+ xlab("Body mass index")+ ylab ("Percent body fat")
library(ggthemes)
## Warning: package 'ggthemes' was built under R version 3.5.3
p+theme_economist()
p+theme_economist()+ ggtitle("This is the title")