Thứ Ba, 1 tháng 10, 2024

Biểu đồ barplot với ggplot2

Barplot.knit

Các thư viện sử dụng

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(datawizard)
library(ggstats)
library(plyr)
## 
## Attaching package: 'plyr'
## The following object is masked from 'package:ggstats':
## 
##     round_any
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble  3.2.1     ✔ dplyr   1.1.4
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.3     ✔ forcats 0.5.1
## ✔ purrr   0.3.4
## Warning: package 'tibble' was built under R version 4.2.3
## Warning: package 'dplyr' was built under R version 4.2.3
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::arrange()   masks plyr::arrange()
## ✖ purrr::compact()   masks plyr::compact()
## ✖ dplyr::count()     masks plyr::count()
## ✖ dplyr::desc()      masks plyr::desc()
## ✖ dplyr::failwith()  masks plyr::failwith()
## ✖ dplyr::filter()    masks stats::filter()
## ✖ dplyr::id()        masks plyr::id()
## ✖ dplyr::lag()       masks stats::lag()
## ✖ dplyr::mutate()    masks plyr::mutate()
## ✖ dplyr::rename()    masks plyr::rename()
## ✖ dplyr::summarise() masks plyr::summarise()
## ✖ dplyr::summarize() masks plyr::summarize()

Nhâp dữ liệu

dt <- read.csv('https://math.ued.udn.vn/wp-content/uploads/2024/10/So_lieu_tv.csv')

# display top 5 row
head(dt)
##   Tuoi Gioi   Vi_tri_u Kich_thuoc_u Giai_doan_benh Nam_phau_thuat
## 1   37   Nu Trên ngoài          2.5            IIB           2021
## 2   54   Nu Trên trong          2.3            IIB           2021
## 3   46   Nu Trên trong          7.0            IIB           2020
## 4   48   Nu Trên ngoài          3.5            IIA           2021
## 5   76   Nu Trên trong          4.0            IIB           2021
## 6   55   Nu Trên ngoài          2.5            IIB           2020
##   Phuong_phap_phau_thuat_tai_vu Hai_long_ve_phuong_phap_phau_thuat
## 1              Cắt vú + tái tạo                           Hài lòng
## 2                       bảo tồn                           Hài lòng
## 3              Cắt vú + tái tạo                           Hài lòng
## 4              Cắt vú triệt căn                       Rất hài lòng
## 5              Cắt vú triệt căn                           Hài lòng
## 6              Cắt vú triệt căn                     Không hài lòng
dt$Hai_long_ve_phuong_phap_phau_thuat=replace(dt$Hai_long_ve_phuong_phap_phau_thuat,dt$Hai_long_ve_phuong_phap_phau_thuat=="","Không đánh giá")
ggplot(dt, aes(x=Hai_long_ve_phuong_phap_phau_thuat ,y = ..count..,fill = Hai_long_ve_phuong_phap_phau_thuat))+ 
  geom_bar()+ 
  geom_text(aes( label = scales::percent((..count..)/sum(..count..))), stat= "count",vjust = -.5)+
  labs(y = "Số lượng", x="Hài lòng về phương pháp phẫu thuật", fill="Hài lòng về phương pháp phẫu thuật")
## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Nếu muốn đếm số lượng của mỗi loại thì ta sử dụng lệnh sau

dt %>% count(Hai_long_ve_phuong_phap_phau_thuat)
##   Hai_long_ve_phuong_phap_phau_thuat   n
## 1                           Hài lòng 691
## 2                     Không hài lòng   5
## 3                     Không đánh giá  10
## 4                       Rất hài lòng 163

Phân lớp tuổi theo nhóm

dt$Nhomtuoi<-recode_values(dt$Tuoi, list(`1`=1:39,`2`=40:60,`3`=61:100))
ggplot(dt, aes(x=  dt$Nhomtuoi,y = ..count..,fill = factor(..x..)))+ 
  geom_bar() +
  geom_text(aes( label = scales::percent((..count..)/sum(..count..))), stat= "count",vjust = -.5)+
  labs(y = "Số lượng", x="Nhóm tuổi: 1 (<40), 2 (40-60), 3 (>60)", fill="Nhóm tuổi") 
## Warning: Use of `dt$Nhomtuoi` is discouraged.
## ℹ Use `Nhomtuoi` instead.
## Use of `dt$Nhomtuoi` is discouraged.
## ℹ Use `Nhomtuoi` instead.

Phân lớp kích thước u theo nhóm

dt$Nhomkichthuocu=as.numeric(dt$Kich_thuoc_u)
dt$Nhomkichthuocu=replace(dt$Nhomkichthuocu,(dt$Nhomkichthuocu>=2)&(dt$Nhomkichthuocu<=5),2)
dt$Nhomkichthuocu=replace(dt$Nhomkichthuocu,dt$Nhomkichthuocu<2,1)

dt$Nhomkichthuocu=replace(dt$Nhomkichthuocu,dt$Nhomkichthuocu>5,3)

ggplot(dt, aes(x=  Nhomkichthuocu,y = ..count..,fill = factor(..x..)))+ 
  geom_bar() +
  geom_text(aes( label = scales::percent((..count..)/sum(..count..))), stat= "count",vjust = -.5)+
  labs(y = "Số lượng", x="Nhóm kích thước u: 1 (<2), 2 (2-5), 3 (>5)", fill="Nhóm kích thước u") 

Barplot theo thời gian và theo nhóm

nam=rep(c(2018,2019,2020,2021,2022),each=3)
pp_phauthuat_taivu=rep(c("Bảo tồn", "Cắt vú + tái tạo","Cắt vú triệt căn"),time=5)
tan_so=c(45,7,71,53,25,115,51,23,140,64,22,105,44,17,87)
dt_new2=data.frame(nam,pp_phauthuat_taivu,tan_so)

ggplot(dt_new2) +
  aes(x = nam, fill = pp_phauthuat_taivu, weight = tan_so, by = nam) +
  geom_bar(position = "fill") +
  geom_text(aes(label = scales::percent(after_stat(prop), accuracy = 2)),stat = "prop", position = position_fill(.5))+
  labs(y = "Tỉ lệ", x="Năm phẫu thuật", fill="Phương pháp phẫu thuật tại vú") +
  scale_y_continuous(labels = scales::percent)
## Warning: The following aesthetics were dropped during statistical transformation: by.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.