trang<- read.csv("C:/Users/Ngo Trang/Documents/student_spending-1.csv", header = T)
dim(trang)
## [1] 1000 18
Data “student_spending-1.csv” là data dạng csv bao gồm 1000 quan sát và 18 biến miêu tả về các tác động đến sinh viên .Cụ thể các biến là :
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── 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(scales)
##
## Attaching package: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
trang %>% group_by(gender) %>% summarise(n = n()) %>%
ggplot(aes(gender,n)) +
geom_col(fill='green') +
geom_text(aes(label = n),vjust = 2, color = 'red') +
labs(x = 'Giới tính', y = 'Số lượng')
GIẢI THÍCH : Câu lệnh trên cho chúng ta một đồ thị dang Bar-chart có :
trang %>% group_by(major) %>% summarise(n = n()) %>%
ggplot(aes(major,n)) +
geom_col(fill='green') +
geom_text(aes(label = percent(n/length(trang$food))),vjust = 2, color = 'red') +
labs(x = 'Chuyên nghành', y = 'Số lượng')
GIẢI THÍCH : Câu lệnh trên cho chúng ta bốn đồ thị dang Bar-chart có :
trang %>% group_by(gender,year_in_school) %>% summarise(n=n()) %>%
ggplot(aes(x = gender,y = n)) +
geom_col(position = 'dodge') +
facet_wrap(~year_in_school) +
geom_text(aes(label = n),vjust = 2, color = 'green') +
labs(x = 'Giới tính', y = 'Số lượng')
## `summarise()` has grouped output by 'gender'. You can override using the
## `.groups` argument.
GIẢI THÍCH : Câu lệnh trên cho chúng ta bốn đồ thị dang Bar-chart có :
trang %>% group_by(major) %>% summarise(m= median(tuition)) %>%
ggplot(aes(x = major,y = m)) +
geom_col(position = 'dodge') +
geom_text(aes(label = round(m,2)), vjust = 2, color = 'green') +
labs(x = 'Chuyên nghành', y = 'Median(học phí)')
GIẢI THÍCH : Câu lệnh trên cho chúng ta một đồ thị dang Bar-chart có :
trang %>% group_by(age,gender) %>% summarise(m = mean(housing)) %>%
ggplot(aes(x = age,y = m)) +
geom_col(position = 'dodge') +
facet_wrap(~gender) +
geom_text(aes(label = round(m))) +
labs(x = 'Tuổi', y = 'Mean(Tiền nhà)')
## `summarise()` has grouped output by 'age'. You can override using the `.groups`
## argument.
GIẢI THÍCH : Câu lệnh trên cho chúng ta ba đồ thị dang Bar-chart có :
trang <- trang %>% group_by(year_in_school, gender) %>% summarise(n = n())
## `summarise()` has grouped output by 'year_in_school'. You can override using
## the `.groups` argument.
trang %>% ggplot(aes(x = year_in_school, y = n)) +
geom_col(data = trang %>% filter(gender == 'Male'), fill = 'orange') +
geom_col(data = trang %>% filter(gender == 'Non-binary'), fill = 'green')
GIẢI THÍCH : Câu lệnh trên cho chúng ta một đồ thị dạng Bar-chart có :
trang %>% group_by(year_in_school) %>% summarise(n = n()) %>%
ggplot(aes(x = '', y = n,fill = year_in_school)) +
geom_col() +
geom_text(aes(label = n),position = position_stack(vjust = 1))
GIẢI THÍCH : Câu lệnh trên cho chúng ta một đồ thị dạng Pie-chart được trải phẳng có :
trang %>% group_by(gender) %>% summarise(n = n()) %>%
ggplot(aes(x = '', y = n,fill = gender)) +
geom_col() +
coord_polar('y')
GIẢI THÍCH : Câu lệnh trên cho chúng ta một đồ thị dạng Pie-chart có :