Khoá học thực hành Phân tích dữ liệu bằng R

Ngày 01 (10/05/2025)

Ngày 02 (11/05/2025)

Hiển thị dữ liệu:

##Giới thiệu về GGPlot: ##Vẽ biểu đồ (ggplot2) ##1. lấy data từ gapminder package ‘gapminder’

library(gapminder)
data("gapminder")
head(gapminder)
## # A tibble: 6 × 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.

##2.Vẽ biểu đồ tương quan

library(ggplot2)
p= ggplot(data=gapminder,aes(x=gdpPercap, y=lifeExp))
p = p + geom_point ()
p

## Vẽ biểu đồ tương quan (thêm màu):

p= ggplot(data=gapminder,aes(x=gdpPercap, y=lifeExp,color=continent))
p = p + geom_point ()
p

##vẽ biểu đồ line, smooth thể hiện đường cong tương quan.

p = ggplot(data = gapminder,aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth (method = "loess") + scale_x_log10()
p
## `geom_smooth()` using formula = 'y ~ x'

##vẽ biểu đồ line, smooth thể hiện đường cong tương quan

p = ggplot(data = gapminder,aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth (method = "loess") + scale_x_log10()
p = p + labs ( x= "Log GPD per Capita", y = "Life Expectancy")
p
## `geom_smooth()` using formula = 'y ~ x'

## Vẽ thêm theme

p = ggplot(data = gapminder,aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth (method = "loess") + scale_x_log10()
p = p + labs ( x= "Log GPD per Capita", y = "Life Expectancy")
library(ggthemes)
p + theme_economist()
## `geom_smooth()` using formula = 'y ~ x'

p
## `geom_smooth()` using formula = 'y ~ x'

## CÁC BIỂU ĐỒ PHỔ BIẾN

Vẽ biểu đồ Histogram

Đọc dữ liệu:

tq=read.csv("/Users/baochau/Library/CloudStorage/OneDrive-PVCFC/Cong viec/Don vi ben ngoai/Phuong Nam Institute/Phuong phap phan tich du lieu voi R/Data thuc hanh/CHNS data full.csv")
head(tq)
##   id whours wgroup dead fu.time gender age edu marital residence income occu
## 1  1     35      1    0       4      2  52   3       3         1 116000    1
## 2  2     48      3    0       4      1  36   3       2         1  25200    1
## 3  3     40      1    0       4      2  31   3       2         1  27000    1
## 4  4     48      3    0       4      2  51   2       2         1  27600    2
## 5  5     32      2    0       4      1  58   2       2         1  34800    2
## 6  6     40      1    0       4      1  42   3       2         1  77000    1
##   smoking drinking height weight   bmi sys1 sys2 sys3 dias1 dias2 dias3 tsf1
## 1       0        0    168   83.5 29.58  120  126  120    80    82    76   28
## 2       1        0    173   85.0 28.40  120  120  120    90    80    80   25
## 3       0        1    167   50.0 17.93  110  108  110    70    70    70   18
## 4       0        0    164   80.0 29.74  120  110  120    80    82    80   27
## 5       0        0    175   65.0 21.22  120  120  120    80    82    80   23
## 6       0        1    179   75.0 23.41  110  112  110    72    76    70   24
##   tsf2 tsf3 uac  hc  wc
## 1   27   28  36 111 103
## 2   44   25  35 102  95
## 3   17   18  25  96  72
## 4   26   27  32 104  97
## 5   22   22  35 102  90
## 6   23   24  28  96  90

Vẽ biểu đồ phân bổ:

p = ggplot (data = tq, aes(x = log(income)))
p + geom_histogram(fill="blue", col="white")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).

### Vẽ biểu đồ phân bổ thu nhập theo giới tính

p = ggplot(data=tq, aes (x =log(income), fill =factor(gender) ) )
p + geom_histogram(col="white") +
     labs (x="Income log(scale)",y="Frequency", title="Phan bo thu nhập")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).

### Vẽ theo mật độ hay tỷ lệ %, frequency, làm mờ alpha =0.5 để thấy đường biểu diễn:

 p + geom_density(alpha=0.5) +labs (x="Income log(scale)", y="Frequency", title = "Phân bố thu nhập")
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_density()`).