Ví dụ 1:
library(ggplot2)
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
pham_vi <- c("[65,75)", "[75,85)", "[85,95)","[95,105)", "[105,115)", "[115,125)")
so_nguoi <- c(1,7,24,35,25,8)
data <- data.frame(pham_vi, so_nguoi)
data
## pham_vi so_nguoi
## 1 [65,75) 1
## 2 [75,85) 7
## 3 [85,95) 24
## 4 [95,105) 35
## 5 [105,115) 25
## 6 [115,125) 8
ggplot(data, aes(x = factor(pham_vi, levels = pham_vi), y = so_nguoi))+
geom_bar(stat = "identity", fill = "cornflowerblue", color = "black")

data %>% filter(so_nguoi > 1) %>%
ggplot(aes(x = factor(pham_vi, levels = pham_vi), y = so_nguoi))+
geom_bar(stat = "identity", fill = "cornflowerblue", color = "black")+
scale_y_continuous(breaks = seq(0,40,10), limits = c(0, 40))+
theme(
panel.grid.major = element_line(colour = "blue"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(hjust = 0.5),
axis.text.x = element_text(size = 12, color = "black"),
axis.line.y = element_line(colour = "blue")
)+
labs(title = "Glucoza máu người bình thường",
x = "Glucoza máu (mg%)",
y = "Tần số")

Ví dụ 2:
chieu_cao <- c("[152,155)", "[155,158)", "[158,161)", "[161,164)", "[164,167)", "[167,170)", "[170,173)", "[173,176)")
so_nguoi <- c(263, 460, 540, 385, 204, 70, 20, 6)
data1 <- data.frame(chieu_cao, so_nguoi)
ggplot(data1, aes(x = chieu_cao, y = so_nguoi))+
geom_bar(stat = "identity", fill = "indianred", color = "black")+
theme(
panel.grid.major = element_line(colour = "blue"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(colour = "black", hjust = 0.5),
axis.text.x = element_text(colour = "black", size = 10),
axis.line.y = element_line(color = "blue")
)+
labs(title = "Chiều cao thanh niên 17 tuổi",
x = "Chiều cao (cm)",
y = "Tần số")+
scale_y_continuous(breaks = seq(0,600, 100), limits = c(0,600))

Ví dụ 3
tinh_trang <- c("Thiếu cân", "Bình thường", "Thừa cân", "Béo phì")
so_nguoi <- c(107, 857, 238, 15)
ti_le <- c(8.8, 70.4, 19.6, 1.2)
data2 <- data.frame(tinh_trang, so_nguoi, ti_le)
nhan_ti_le <- paste0(ti_le,"%")
nhan_ti_le
## [1] "8.8%" "70.4%" "19.6%" "1.2%"
ggplot(data2, aes(x = factor(tinh_trang, levels = tinh_trang), y = ti_le))+
geom_bar(stat = "identity", fill = "indianred", color = "black")+
geom_text(aes(label = nhan_ti_le), vjust = -0.5, color = "blue")+
theme(
panel.grid.major = element_line(color = "blue"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x = element_text(size = 12, color = "black"),
axis.line.y = element_line(color = "blue"),
plot.title = element_text(hjust = 0.5)
)+
labs(title = "Tỉ lệ (%)",
x = "",
y = "")+
scale_y_continuous(breaks = seq(0, 80, 10), labels = scales::percent(seq(0, 80, 10)/100), limits = c(0,80))

library(ggrepel)
ggplot(data2, aes(x = "", y = ti_le, fill = tinh_trang))+
geom_col(color = "black")+
labs(x = "",
y = "",
title = "Tỷ lệ %")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))+
coord_polar(theta = "y")+
scale_fill_brewer(palette = "Pastel1")+
geom_label_repel(data = data2, aes(y = ti_le, label = nhan_ti_le),
size = 4.5, nudge_x = 1, show.legend = F)

library(ggrepel)
ggplot(data2, aes(x = "", y = ti_le, fill = tinh_trang))+
geom_col(color = "black")+
geom_text(aes(label = nhan_ti_le), position = position_stack(vjust = 0.6), size = 4.5)+
labs(x = "",
y = "",
title = "Tỷ lệ %")+
theme(plot.title = element_text(hjust = 0.5, face = "bold"))+
coord_polar(theta = "y")+
scale_fill_brewer(palette = "Pastel1")

ggplot(data2, aes(x = "", y = ti_le, fill = tinh_trang))+
geom_bar(stat = "identity" ,position = "stack")+
coord_polar(theta = "y")+
scale_fill_brewer(palette = "pastel1")
## Warning in pal_name(palette, type): Unknown palette pastel1

ggplot(data2, aes(x = factor(tinh_trang, levels = tinh_trang), y = ti_le, group = 1))+
geom_line(linewidth = 2, color = "red")+
geom_point(shape = 23, size = 4, fill = "blue")

Ví dụ 4:
tinh_trang <- c("Thiếu cân", "Bình thường", "Thừa cân","Béo phì")
nam <- c(76,622,153,11)
nu <- c(31,235,85,4)
data3 <- data.frame(tinh_trang, nam, nu)
ggplot(data3, aes(x = tinh_trang, y = nam+nu))+
geom_bar(stat = "identity",position = position_dodge())

library(tidyr)
data3 <- data.frame(tinh_trang, nam, nu)
data4 <- gather(data3, value = "values", key = "sex", nam:nu)
pltdata <- data4 %>%
group_by(tinh_trang)
ggplot(data = pltdata, aes(x = tinh_trang,y = values, fill = sex))+
geom_bar(stat = "identity", position = position_dodge(preserve = "single"))+
labs(title = "Phân bố theo giới tính",
x = "Tình trạng",
y = "Số người")
