Nhập data

setwd("E:/CONG VIEC/Ky nang ngoai/Xu ly so lieu Bang R/Regression-Book-master_VanLang2023")
t2=file.choose()
sa=read.csv(t2,header=T)
head(sa)
##   ID      Rank Discipline Yrs.since.phd Yrs.service  Sex Salary
## 1  1      Prof          B            19          18 Male 139750
## 2  2      Prof          B            20          16 Male 173200
## 3  3  AsstProf          B             4           3 Male  79750
## 4  4      Prof          B            45          39 Male 115000
## 5  5      Prof          B            40          41 Male 141500
## 6  6 AssocProf          B             6           6 Male  97000

Vẽ biểu đồ phân bố (histogram) tiền lương với hàm ggplot trong package ggplot2 và nhận xét kết quả.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
his=ggplot(data=sa,aes(x=Salary))+geom_histogram(fill="blue",color="white")+ labs(x="tiền lương ", y=" tần suất"                                 
                                                                                  )
his
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Vẽ biểu đồ hộp BoxPlot

box=ggplot(data=sa,aes(x=Sex, y=Salary,col=Sex))+geom_boxplot()+geom_jitter(alpha=0.2)
box

So sánh thời gian làm việc giữa nam và nữ

boxyear=ggplot(data=sa,aes(x=Sex, y=Yrs.service,col=Sex))+geom_boxplot()+geom_jitter(alpha=0.2)
boxyear

Vẽ biểu đồ tán xạ giữa tiền lương và số năm sau tiến sĩ bằng geom_smooth (method=“loess”)

plot=ggplot(data=sa,aes(x=Yrs.since.phd, y=Salary,col=Sex))+geom_point()+geom_smooth(method="loess")
plot
## `geom_smooth()` using formula = 'y ~ x'

Vẽ biểu đồ tán xạ giữa tiền lương và số năm sau tiến sĩ bằng geom_smooth (method=“lm”)

plotlm=ggplot(data=sa,aes(x=Yrs.since.phd, y=Salary,col=Sex))+geom_point()+geom_smooth(method="lm")
plotlm
## `geom_smooth()` using formula = 'y ~ x'