library(knitr)
knit("1. ggplot2.Rmd")
##
##
## processing file: 1. ggplot2.Rmd
##
|
| | 0%
|
|... | 4%
|
|....... | 8% (unnamed-chunk-17)
|
|.......... | 12%
|
|............. | 16% (unnamed-chunk-18)
|
|................ | 20%
|
|.................... | 24% (unnamed-chunk-19)
|
|....................... | 28%
|
|.......................... | 32% (unnamed-chunk-20)
|
|.............................. | 36%
|
|................................. | 40% (unnamed-chunk-21)
|
|.................................... | 44%
|
|....................................... | 48% (unnamed-chunk-22)
|
|........................................... | 52%
|
|.............................................. | 56% (unnamed-chunk-23)
|
|................................................. | 60% (unnamed-chunk-24)
|
|.................................................... | 64%
|
|........................................................ | 68% (unnamed-chunk-25)
|
|........................................................... | 72%
|
|.............................................................. | 76% (unnamed-chunk-26)
|
|.................................................................. | 80%
|
|..................................................................... | 84% (unnamed-chunk-27)
|
|........................................................................ | 88%
|
|........................................................................... | 92% (unnamed-chunk-28)
|
|............................................................................... | 96%
|
|..................................................................................| 100% (unnamed-chunk-29)
## output file: 1. ggplot2.md
## [1] "1. ggplot2.md"
# Tạo dữ liệu ví dụ
category <- c("Áo", "Quần", "Áo khoác", "Váy", "Phụ kiện")
quantity <- c(300, 250, 120, 180, 80)
# Tạo data frame từ dữ liệu
fashion_data <- data.frame(category, quantity)
# Tạo biểu đồ cột sử dụng ggplot2
ggplot(data = fashion_data, aes(x = category, y = quantity, fill = category)) +
geom_col() +
labs(title = "Số lượng sản phẩm thời trang theo chủng loại",
x = "Chủng loại sản phẩm", y = "Số lượng") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot of chunk unnamed-chunk-5
library(tidyverse)
# Tạo dữ liệu ví dụ
product <- c("Sản phẩm A", "Sản phẩm B", "Sản phẩm C", "Sản phẩm D")
revenue <- c(1500, 2000, 1800, 1200)
cost <- c(1000, 1500, 1200, 900)
# Tạo data frame từ dữ liệu
financial_data <- data.frame(product, revenue, cost)
# Tạo biểu đồ cột sử dụng ggplot2
ggplot(data = financial_data, aes(x = product)) +
geom_col(aes(y = revenue, fill = "Doanh thu"), position = "dodge") +
geom_col(aes(y = cost, fill = "Chi phí"), position = "dodge") +
scale_fill_manual(values = c("Doanh thu" = "skyblue", "Chi phí" = "orange")) +
labs(title = "Mối quan hệ giữa doanh thu và chi phí theo sản phẩm",
x = "Sản phẩm", y = "Số tiền") +
theme(legend.title = element_blank())
plot of chunk unnamed-chunk-6
#1. Biểu đồ geom_point()
#Ví dụ 1:
# Dữ liệu ví dụ
data <- data.frame(
x = c(1, 2, 3, 4, 5),
y = c(10, 15, 7, 25, 18))
# Tạo biểu đồ điểm sử dụng ggplot2 và geom_point()
ggplot(data, aes(x = x, y = y)) +
geom_point()
plot of chunk unnamed-chunk-7
#Ví dụ 2:
# Dữ liệu ví dụ
data <- data.frame(
student = c("Alice", "Bob", "Carol", "David", "Eve"),
score = c(85, 92, 78, 65, 88))
# Tạo biểu đồ điểm với tên học sinh và điểm số
ggplot(data, aes(x = student, y = score)) +
geom_point(color = "blue", size = 3) +
labs(title = "Biểu đồ điểm theo điểm số của học sinh",
x = "Học sinh", y = "Điểm số") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
plot of chunk unnamed-chunk-7
#2. Biểu đồ đường geom_line()
#Ví dụ 1:
# Dữ liệu ví dụ
data <- data.frame(x = c(1, 2, 3, 4, 5), y = c(10, 15, 7, 25, 18))
# Tạo biểu đồ đường sử dụng ggplot2 và geom_line()
ggplot(data, aes(x = x, y = y)) +
geom_line(color = "red", size = 2) +
labs(title = "Biểu đồ đường thể hiện sự thay đổi của dữ liệu",
x = "X-axis", y = "Y-axis")
plot of chunk unnamed-chunk-8
#Ví dụ 2
# Dữ liệu ví dụ
data <- data.frame(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun"),
temperature = c(10, 12, 15, 18, 20, 22))
# Tạo biểu đồ đường với nhiệt độ theo tháng
ggplot(data, aes(x = month, y = temperature)) +
geom_line(color = "green", size = 1.5) +
geom_point(color = "blue", size = 3) +
labs(title = "Biểu đồ đường theo nhiệt độ theo tháng",
x = "Tháng", y = "Nhiệt độ (°C)") +
theme_minimal()
## `geom_line()`: Each group consists of only one observation.
## ℹ Do you need to adjust the group aesthetic?
plot of chunk unnamed-chunk-8
#3. Biểu đồ cột geom_bar()
#Ví dụ 1
# Dữ liệu ví dụ
data <- data.frame(category = c("A", "B", "C", "D"),
value = c(25, 40, 15, 30))
# Tạo biểu đồ cột sử dụng ggplot2 và geom_bar()
ggplot(data, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
labs(title = "Biểu đồ cột thể hiện giá trị theo danh mục",
x = "Danh mục", y = "Giá trị") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-9
#Ví dụ 2:
# Dữ liệu ví dụ
data <- data.frame(month = c("Jan", "Feb", "Mar", "Apr", "May", "Jun"),
sales = c(120, 150, 180, 140, 200, 170))
# Tạo biểu đồ cột với doanh số bán hàng theo tháng
ggplot(data, aes(x = month, y = sales, fill = month)) +
geom_bar(stat = "identity", color = "black") +
labs(title = "Biểu đồ cột thể hiện doanh số bán hàng theo tháng",
x = "Tháng", y = "Doanh số bán hàng") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-9
#4. Biều đồ hộp boxplot()
# Dữ liệu ví dụ
data <- data.frame(category = rep(c("A", "B", "C"), each = 100),
value = c(rnorm(100), rnorm(100, mean = 2), rnorm(100, mean = 3)))
# Tạo biểu đồ hộp thể hiện phân phối giá trị theo từng danh mục
ggplot(data, aes(x = category, y = value, fill = category)) +
geom_boxplot() +
labs(title = "Biểu đồ hộp thể hiện phân phối giá trị theo danh mục",
x = "Danh mục", y = "Giá trị") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-10
#5. Hàm mật độ geom_density()
#Ví dụ 1:
# Dữ liệu ví dụ
set.seed(123)
group1 <- rnorm(200, mean = 5, sd = 2)
group2 <- rnorm(150, mean = 8, sd = 1.5)
# Tạo biểu đồ mật độ so sánh phân phối của hai nhóm
ggplot() +
geom_density(data = data.frame(value = group1, group = "Nhóm 1"),
aes(x = value, fill = group), alpha = 0.5) +
geom_density(data = data.frame(value = group2, group = "Nhóm 2"),
aes(x = value, fill = group), alpha = 0.5) +
labs(title = "Biểu đồ mật độ so sánh phân phối của hai nhóm",
x = "Giá trị", y = "Mật độ") +
theme_minimal() +
scale_fill_manual(values = c("Nhóm 1" = "blue", "Nhóm 2" = "red"))
plot of chunk unnamed-chunk-11
#Ví dụ 2:
# Hàm để tạo biểu đồ mật độ cho một biến theo từng nhóm
plot_density <- function(data, x_var, group_var, title, x_label, y_label) {
ggplot(data, aes_string(x = x_var, fill = group_var)) +
geom_density(alpha = 0.5) +
labs(title = title, x = x_label, y = y_label) +
theme_minimal()
}
# Gọi hàm để tạo biểu đồ mật độ chiều dài cánh hoa theo từng loài hoa
plot_density(iris, "Petal.Length", "Species",
"Biểu đồ mật độ chiều dài cánh hoa theo từng loài hoa",
"Chiều dài cánh hoa", "Mật độ")
plot of chunk unnamed-chunk-11
#6. Biểu đồ violin (geom_violin())
#Ví dụ 1:
# Dữ liệu ví dụ
data <- data.frame(group = rep(c("A", "B", "C"), each = 100),
value = c(rnorm(100), rnorm(100, mean = 2), rnorm(100, mean = 3)))
# Tạo biểu đồ violin thể hiện phân phối giá trị theo từng nhóm
ggplot(data, aes(x = group, y = value, fill = group)) +
geom_violin() +
labs(title = "Biểu đồ violin thể hiện phân phối giá trị theo nhóm",
x = "Nhóm", y = "Giá trị") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-12
#ví dụ 2:
# Tạo biểu đồ violin thể hiện phân phối độ dài cánh hoa theo loài hoa
ggplot(iris, aes(x = Species, y = Petal.Length, fill = Species)) +
geom_violin() +
labs(title = "Biểu đồ violin phân phối độ dài cánh hoa theo loài hoa",
x = "Loài hoa", y = "Độ dài cánh hoa") +
theme_minimal()
plot of chunk unnamed-chunk-12
#Ví dụ 3:
# Tạo biểu đồ violin thể hiện phân phối giá trị giá của kim cương theo loại cut
ggplot(diamonds, aes(x = cut, y = price, fill = cut)) +
geom_violin() +
labs(title = "Biểu đồ violin phân phối giá của kim cương theo loại cut",
x = "Loại cut", y = "Giá") +
theme_minimal()
plot of chunk unnamed-chunk-12
#7. Biểu đồ thể hiện dạng đặc điểm (geom_jitter())
#Ví dụ 1:
# Dữ liệu ví dụ
data <- data.frame(category = rep(c("A", "B", "C"), each = 50),
value = c(rnorm(50), rnorm(50, mean = 2), rnorm(50, mean = 3)))
# Tạo biểu đồ thể hiện đặc điểm sử dụng ggplot2 và geom_jitter()
ggplot(data, aes(x = category, y = value, color = category)) +
geom_jitter(width = 0.2, height = 0) +
labs(title = "Biểu đồ thể hiện đặc điểm của dữ liệu",
x = "Danh mục", y = "Giá trị") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-13
#Ví dụ 2
# Tạo biểu đồ thể hiện đặc điểm của dữ liệu iris
ggplot(iris, aes(x = Species, y = Sepal.Length, color = Species)) +
geom_jitter(width = 0.2, height = 0) +
labs(title = "Biểu đồ thể hiện đặc điểm của dữ liệu iris",
x = "Loài", y = "Chiều dài đài hoa") +
theme_minimal() +
theme(legend.position = "none")
plot of chunk unnamed-chunk-13
#8. Biều đồ đa phân chia (facet_wrap() và facet_grid())
# Tạo biểu đồ đa phân chia sử dụng facet_wrap()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
labs(title = "Biểu đồ đa phân chia của dữ liệu iris",
x = "Chiều dài đài hoa", y = "Chiều rộng đài hoa") +
theme_minimal() +
theme(legend.position = "bottom") +
facet_wrap(~ Species, ncol = 2)
plot of chunk unnamed-chunk-14
# Tạo biểu đồ đa phân chia sử dụng facet_grid()
ggplot(diamonds, aes(x = carat, y = price)) +
geom_point() +
facet_grid(clarity ~ cut) +
labs(title = "Biểu đồ đa phân chia sử dụng facet_grid()",
x = "Carat", y = "Giá") +
theme_minimal()
plot of chunk unnamed-chunk-14
# Tạo biểu đồ với dữ liệu diamond
ggplot(diamonds, aes(x = carat, y = price, color = cut)) +
geom_point(alpha = 0.5) +
facet_wrap(~ clarity, nrow = 2) +
labs(title = "Biểu đồ Scatter cho dữ liệu diamonds",
x = "Carat", y = "Price") +
theme_minimal()
plot of chunk unnamed-chunk-15