library(ggplot2)

Mô hình nhiều lớp

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.
p=ggplot(data=gapminder, aes (x=gdpPercap, y=lifeExp))
p

p=p + geom_point()
p

p=p+geom_line()
p

p=p+geom_point() + geom_smooth()
p
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

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

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

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

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

p=p+labs(x="Log GDP per Capita", y="Life Expectancy")
p=p+ggtitle("Association between GDP Per Capota and Life Expectancy")
p
## `geom_smooth()` using formula = 'y ~ x'

library(ggthemes)
p+theme_economist()
## `geom_smooth()` using formula = 'y ~ x'

Biểu đồ phân bố

df=read.csv('/Users/vothainguyetcam/Downloads/CHNS data full.csv')
p=ggplot(data=df, aes(x=income))
p+geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 224 rows containing non-finite outside the scale range
## (`stat_bin()`).

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

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

p = ggplot(data=df, aes(x =
log(income), fill =factor(gender)))
p

p + geom_histogram(col="white") +
labs(x="Income log(scale)",
y="Frequency", title="Phân bố 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()`).

p+geom_density () + labs (x="Income log(scale)", y="Frequency", title = "Phan bo thu nhap")
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_density()`).

p+geom_density (alpha=0.5) + labs (x="Income log(scale)", y="Frequency", title = "Phan bo thu nhap")
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_density()`).

Biểu đồ thanh

df=read.csv('/Users/vothainguyetcam/Downloads/CHNS data full.csv')
head(df)
##   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

Biểu đồ hộp

p = ggplot(data=df,
aes(x=factor(gender), y=log(income), 
col=factor(gender)))
p + geom_boxplot() +
geom_jitter(alpha=0.05)
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
## Warning: Removed 224 rows containing missing values or values outside the scale range
## (`geom_point()`).

Biểu đồ tương quan

df=read.csv('/Users/vothainguyetcam/Downloads/CHNS data full.csv')
head(df)
##   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
p = ggplot(data=df, aes(x=age,
y=log(income), col=factor(gender)))
p + geom_point() +
geom_smooth(method="lm",
formula=y~x+I(x^2)+I(x^3))
## Warning: Removed 243 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 226 rows containing missing values or values outside the scale range
## (`geom_point()`).