Các biểu đồ trực quan dữ liệu đơn biến thông thường miêu tả phân phối của một biến số nào đó. Biến số đó có thể là biến phân loại hoặc biến định lượng.
Biểu đồ trực quan dữ liệu cho một biến phân loại có thể là biểu đồ bar, biểu đồ tròn hoặc biều đồ treemap
library(ggplot2)
data(Marriage, package = "mosaicData")
# trực quan dữ liệu đơn biến(phân loại)
ggplot(Marriage, mapping = aes(x = race))+
geom_bar()
Thay đổi màu sách của các cột dữ liệu và thêm tiêu đề để làm nổi bật dữu liệu hơn:
ggplot(Marriage, mapping = aes(x = race))+
geom_bar(fill = "cornflowerblue", color = "black")+
labs(title = "Participants by race",
x = "Race",
y = "Frequency")
Biểu đồ Bar chart ngoài khả năng hiểu thị tần số của các đối tượng thì có thể được biểu diễn theo tỷ lệ %:
ggplot(Marriage, mapping = aes(x = race, y = ..count../sum(..count..)))+
geom_bar(fill = "cornflowerblue", color = "black")+
labs(title = "Participants by race",
x = "Race",
y = "Frequency")+
scale_y_continuous(labels = scales::percent)
## 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.
Sử dụng hàm reoder() để sắp xếp lại các thứ tự của các giá trị phân loại theo chiều tăng dần hoặc giảm dần
# hàm count để đến số lần xuất hiện các giá trị phân loại trong một biến số
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
plotdata <- Marriage %>%
count(race)
plotdata
## race n
## 1 American Indian 1
## 2 Black 22
## 3 Hispanic 1
## 4 White 74
# sắp xếp tăng dần các giá trị phân loại
ggplot(plotdata, mapping = aes(x = reorder(race, n), y = n))+
geom_bar(stat = "identity", fill = "indianred", color = "black")+
labs(title = "Participants by race",
x = "Race",
y = "Frequency")
# sắp xếp giảm dần các giá trị phân loại của biến số
ggplot(plotdata, mapping = aes(x = reorder(race,-n), y = n))+
geom_bar(stat = "identity", fill = "indianred", color = "black")+
labs(title = "Participants by race",
x = "Race",
y = "Frequency")+
scale_y_continuous(labels = scales::percent)
Gắn nhãn cho các cột với các con số trong tập dữ liệu
plotdata1 <- Marriage %>%
count(race) %>%
mutate(pct = n / sum(n), pctlabel = paste0(round(pct*100), "%"))
ggplot(plotdata1, mapping = aes(x = reorder(race,n), y = pct))+
geom_bar(stat = "identity", fill = "indianred", color = "black")+
geom_text(aes(label = pctlabel), vjust = -0.75)+
scale_y_continuous(labels = scales::percent)+
labs(title = "Participants by race",
x = "Race",
y = "Frequency")
Overlapping là một hiện tượng giá trị các biến xếp chồng lên nhau. thường xảy ra với các giá trị text
ggplot(Marriage, mapping = aes(x = officialTitle))+
geom_bar(fill = "indianred", color = "black")
Trong những trường hợp này có 3 cách xử lý:
Xoay ngang truc Ox và trục Oy
Xoay một góc 45 độ các giá trị của trục Ox
Tạo sự nổi/thụt cho các giá trị (gọi là staggering the labels)
# xoay trục dữ liệu
ggplot(Marriage, mapping = aes(x = officialTitle))+
geom_bar(fill = "indianred", color = "black")+
coord_flip()
# Tạo góc xoay 45 độ
ggplot(Marriage, mapping = aes(x = officialTitle))+
geom_bar(fill = "indianred", color = "black")+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
# staggering
lbls <- paste0(c("","\n"), levels(Marriage$officialTitle))
lbls
## [1] "BISHOP" "\nCATHOLIC PRIEST" "CHIEF CLERK"
## [4] "\nCIRCUIT JUDGE " "ELDER" "\nMARRIAGE OFFICIAL"
## [7] "MINISTER" "\nPASTOR" "REVEREND"
ggplot(Marriage, mapping = aes(x = factor(officialTitle, labels = lbls)))+
geom_bar(fill = "indianred", color = "black")
Treemap là dạng biểu đồ dùng thay thế cho biểu đồ tròng trong trường hợp chúng ta cần trực quan bến phân loại có nhiều giá trị:
library(treemapify)
plotdata <- Marriage %>%
count(officialTitle)
ggplot(plotdata, mapping = aes(fill = officialTitle, area = n, label = officialTitle))+
geom_treemap()+
geom_treemap_text(colour = "white", place = "centre")+
theme(legend.position = "none")