date: “17:20:06, 02 - 03 - 2024”
output: html_document: toc: true toc_float: true number_sections: true
Bộ dữ liệu “diamonds” là một tập dữ liệu phổ biến thường được sử dụng trong phân tích dữ liệu. Bộ dữ liệu này chứa thông tin về các mẫu kim cương, bao gồm các thuộc tính như trọng lượng (carat), giá (price), độ trong suốt (clarity), màu sắc (color), loại cắt (cut), độ sâu (depth), chiều rộng (table), và các kích thước khác (x, y, z).
Dưới đây là một phân tích chi tiết về bộ dữ liệu diamonds:
Giới thiệu về bộ dữ liệu
Bộ dữ liệu chứa thông tin về 53,940 mẫu kim cương. Mỗi mẫu được mô tả bởi 10 thuộc tính. Đây là một bộ dữ liệu thực tế được sử dụng rộng rãi trong cộng đồng dữ liệu và phân tích dữ liệu.
Cấu trúc dữ liệu
Bộ dữ liệu diamonds có 10 cột và 53,940 hàng.
Các biến bao gồm:
carat: Trọng lượng của kim cương.
cut: Loại cắt của kim cương (Fair, Good, Very Good, Premium, Ideal).
color: Màu sắc của kim cương, được mã hóa từ D (tốt nhất) đến J (kém nhất).
clarity: Độ trong suốt của kim cương (IF, VVS1, VVS2, VS1, VS2, SI1, SI2, I1).
depth: Độ sâu của kim cương, được đo từ mặt bên dưới đến mặt trên, chia cho chiều dài.
table: Chiều rộng của mặt bên trên của kim cương so với điểm cắt, chia cho chiều rộng lớn nhất.
price: Giá của kim cương (đơn vị: USD).
x: Chiều dài của kim cương (đơn vị: mm).
y: Chiều rộng của kim cương (đơn vị: mm).
z: Chiều sâu của kim cương (đơn vị: mm). ***
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.5.0 ✔ 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
install.packages("ggplot2")
## Warning: package 'ggplot2' is in use and will not be installed
library(ggplot2)
ggplot(diamonds, aes(x = carat)) +
geom_bar(fill = "pink", color = "black") +
labs(title = "Distribution of Carat",
x = "Carat",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = cut)) +
geom_bar(fill = "#336666", color = "black") +
labs(title = "Distribution of Cut Quality",
x = "Cut Quality",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = color)) +
geom_bar(fill = "darkred", color = "black") +
labs(title = "Distribution of Diamond Color",
x = "Color",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = clarity)) +
geom_bar(fill = "beige", color = "black") +
labs(title = "Distribution of Diamond Clarity",
x = "Clarity",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = depth)) +
geom_bar(fill = "lightpink", color = "black") +
labs(title = "Distribution of Depth",
x = "Depth",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = table)) +
geom_bar(fill = "lightyellow", color = "black") +
labs(title = "Distribution of Table",
x = "Table",
y = "Frequency") +
theme_minimal()
ggplot(diamonds, aes(x = price)) +
geom_bar(fill = "lightgray", color = "black") +
labs(title = "Distribution of Price",
x = "Price",
y = "Frequency") +
theme_minimal()
Biểu đồ này cho thấy phân phối của giá của các viên kim cương trong bộ dữ liệu. Giá của các viên kim cương phân bố rộng rãi, từ các giá trị thấp đến các giá trị cao, nhưng có một số lượng lớn các viên kim cương có giá trị tập trung ở mức thấp.
ggplot(diamonds, aes(x = x)) +
geom_bar(fill = "lightsalmon", color = "black") +
labs(title = "Distribution of Length (x)",
x = "Length (x)",
y = "Frequency") +
theme_minimal()
Biểu đồ này cho thấy phân phối của chiều dài của các viên kim cương trong bộ dữ liệu. Chiều dài của các viên kim cương phân bố rộng rãi, từ các giá trị thấp đến các giá trị cao.
ggplot(diamonds, aes(x = y)) +
geom_bar(fill = "#336666", color = "black") +
labs(title = "Distribution of Width (y)",
x = "Width (y)",
y = "Frequency") +
theme_minimal()
Biểu đồ này cho thấy phân phối của chiều rộng của các viên kim cương trong bộ dữ liệu. Chiều rộng của các viên kim cương cũng phân bố rộng rãi, với một số lượng lớn các viên kim cương có chiều rộng tập trung ở mức thấp.
ggplot(diamonds, aes(x = z)) +
geom_bar(fill = "lightgreen", color = "black") +
labs(title = "Distribution of Depth (z)",
x = "Depth (z)",
y = "Frequency") +
theme_minimal()
diamonds %>%
group_by(cut, color) %>%
summarise(n = n()) %>%
ggplot(aes(x = cut, y = n, fill = cut)) + # Thay đổi fill thành biến cut
geom_col(position = 'dodge') +
facet_wrap(~color) +
scale_fill_manual(values = c("Fair" = "beige", "Good" = "navyblue", "Very Good" = "beige", "Premium" = "navyblue", "Ideal" = "beige")) + # Thay đổi màu sắc cho từng giá trị của biến cut
labs(x = 'Loại', y = 'Số lượng')
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
Biểu đồ giúp hiểu được phân phối của các loại kim cương (cut) trong từng mức độ màu sắc khác nhau. Mỗi cột trong biểu đồ thể hiện số lượng các loại kim cương (cut) cho mỗi mức độ màu sắc. Đối với mỗi mức độ màu sắc, chúng ta có thể quan sát được sự phân bố của các loại kim cương (cut) là như thế nào. Sự sắp xếp theo màu sắc giúp ta so sánh phân phối của các loại kim cương trong từng mức độ màu sắc, từ đó đánh giá được mức độ ảnh hưởng của màu sắc đối với chất lượng cắt của viên kim cương
Ví dụ: Trong một số màu sắc nhất định, có thể thấy rằng các loại kim cương Ideal thường chiếm tỷ lệ cao, cho thấy rằng trong một số trường hợp, các viên kim cương có chất lượng cắt Ideal thường được ưa chuộng hơn. Một số màu sắc có sự phân bố đồng đều giữa các loại kim cương, trong khi ở các màu sắc khác, có sự ưu tiên cho một số loại cắt cụ thể hơn.
diamonds %>%
group_by(cut, color) %>%
summarise(n = n()) %>%
ggplot(aes(x = cut, y = n, fill = cut)) +
geom_col(position = 'dodge') +
facet_wrap(~color) +
geom_text(aes(label = n), vjust = 2, color = 'black') +
scale_fill_manual(values = c("#FF99CC", "#cc99cc", "#9999cc", "#6699cc", "#3399cc")) + # Thay đổi màu sắc cho biểu đồ
labs(x = 'Loại', y = 'Số lượng')
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
diamonds %>%
group_by(cut) %>%
summarise(m = mean(carat)) %>%
ggplot(aes(x = cut, y = m, fill = cut)) +
geom_col(position = 'dodge', fill = c("#ffffff", "#ccffff", "#99ffff", "#ccccff", "#99ccff")) +
geom_text(aes(label = round(m, 2)), vjust = 2, color = 'black') +
labs(x = 'Màu', y = 'Mean')
tn <- diamonds
tn <- tn %>% group_by(cut, color) %>% summarise(n = n())
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
tn %>% ggplot(aes(x = cut, y = n)) +
geom_col(data = tn %>% filter(color == 'D'), fill = '#00676B') +
geom_col(data = tn %>% filter(color == 'J'), fill = '#99D1D3')
tn1 <- diamonds
tn1 <- tn1 %>% mutate(caratC = cut(carat,5, label = c('rất nhỏ', 'nhỏ','vừa','lớn','rất lớn')))
tn1 %>% ggplot(aes(x = caratC)) +
geom_bar(fill = '#98D0B9')
diamonds %>% group_by(clarity,color) %>% summarise(n=n()) %>%
ggplot(aes(x = clarity,y = n)) +
geom_col(position = 'dodge') +
facet_wrap(~color) +
geom_text(aes(label = n),vjust = 2, color = 'white') +
labs(x = 'Loại', y = 'Số lượng')
## `summarise()` has grouped output by 'clarity'. You can override using the
## `.groups` argument.
diamonds %>%
group_by(clarity, cut) %>%
summarise(n = n()) %>%
ggplot(aes(x = clarity, y = n, fill = cut)) +
geom_col(position = 'dodge') +
facet_wrap(~cut) +
geom_text(aes(label = n), vjust = 2, color = 'white') +
labs(x = 'Loại', y = 'Số lượng') +
scale_fill_manual(values = c("#191970", "#6495ED", "#0000CD", "#B0E0E6", "#4682B4"))
## `summarise()` has grouped output by 'clarity'. You can override using the
## `.groups` argument.
diamonds %>% group_by(clarity,cut) %>% summarise(n=n()) %>%
ggplot(aes(x = clarity,y = n)) +
geom_col(position = 'dodge') +
facet_wrap(~cut) +
geom_text(aes(label = n),vjust = 2, color = 'white') +
labs(x = 'Loại', y = 'Số lượng')
## `summarise()` has grouped output by 'clarity'. You can override using the
## `.groups` argument.
diamonds %>%
group_by(clarity, cut) %>%
summarise(n = n()) %>%
ggplot(aes(x = clarity, y = n, fill = cut)) +
geom_col(position = 'dodge') +
facet_wrap(~cut) +
geom_text(aes(label = n), vjust = 2, color = 'white') +
labs(x = 'Loại', y = 'Số lượng') +
scale_fill_manual(values = c("#7FFFD4", "#66CDAA", "#458B74", "#698B69", "#556B2F"))
## `summarise()` has grouped output by 'clarity'. You can override using the
## `.groups` argument.
diamonds %>%
group_by(cut) %>%
summarise(mean_carat = mean(carat)) %>%
ggplot(aes(x = cut, y = mean_carat, fill = cut)) +
geom_col() +
labs(title = "Trung bình carat theo loại", x = "Loại", y = "Trung bình carat") +
scale_fill_brewer(palette = "Accent")
diamonds %>%
group_by(cut) %>%
summarise(mean_price = mean(price)) %>%
ggplot(aes(x = cut, y = mean_price, fill = cut)) +
geom_col() +
labs(title = "Trung bình giá theo loại", x = "Loại", y = "Trung bình giá") +
scale_fill_brewer(palette = "Dark2")
diamonds %>%
group_by(cut) %>%
summarise(mean_depth = mean(depth)) %>%
ggplot(aes(x = cut, y = mean_depth, fill = cut)) +
geom_col() +
labs(title = "Trung bình độ sâu theo loại", x = "Loại", y = "Trung bình độ sâu") +
scale_fill_brewer(palette = "Pastel1")
diamonds %>%
group_by(cut) %>%
summarise(mean_table = mean(table)) %>%
ggplot(aes(x = cut, y = mean_table, fill = cut)) +
geom_col() +
labs(title = "Trung bình chiều rộng theo loại", x = "Loại", y = "Trung bình chiều rộng") +
scale_fill_brewer(palette = "Pastel4")
## Warning: Unknown palette: "Pastel4"
*Biểu đồ này hiển thị giá trị trung bình của chiều rộng của kim cương cho từng loại cắt. Cho biết sự biến động của chiều rộng trung bình của kim cương dựa trên loại cắt.
diamonds %>%
group_by(cut) %>%
summarise(mean_x = mean(x)) %>%
ggplot(aes(x = cut, y = mean_x, fill = cut)) +
geom_col() +
labs(title = "Trung bình chiều dài theo loại", x = "Loại", y = "Trung bình chiều dài") +
scale_fill_brewer(palette = "Dark2")
diamonds %>%
group_by(cut) %>%
summarise(mean_y = mean(y)) %>%
ggplot(aes(x = cut, y = mean_y, fill = cut)) +
geom_col() +
labs(title = "Trung bình chiều rộng theo loại", x = "Loại", y = "Trung bình chiều rộng") +
scale_fill_brewer(palette = "Pastel2")
diamonds %>%
group_by(cut) %>%
summarise(mean_z = mean(z)) %>%
ggplot(aes(x = cut, y = mean_z, fill = cut)) +
geom_col() +
labs(title = "Trung bình chiều cao theo loại", x = "Loại", y = "Trung bình chiều cao") +
scale_fill_brewer(palette = "Set2")
diamonds %>%
group_by(color) %>%
summarise(mean_price = mean(price)) %>%
ggplot(aes(x = color, y = mean_price, fill = color)) +
geom_col() +
labs(title = "Trung bình giá theo màu sắc", x = "Màu sắc", y = "Trung bình giá") +
scale_fill_brewer(palette = "Set1")
diamonds %>%
group_by(clarity) %>%
summarise(mean_price = mean(price)) %>%
ggplot(aes(x = clarity, y = mean_price, fill = clarity)) +
geom_col() +
labs(title = "Trung bình giá theo độ trong suốt", x = "Độ trong suốt", y = "Trung bình giá") +
scale_fill_brewer(palette = "Set2")
diamonds %>%
group_by(clarity, cut) %>%
summarise(count = n()) %>%
ggplot(aes(x = clarity, y = count, fill = cut)) +
geom_col(position = "dodge") +
labs(title = "Số lượng mẫu theo độ trong suốt và loại cắt", x = "Độ trong suốt", y = "Số lượng") +
scale_fill_brewer(palette = "Set3")
## `summarise()` has grouped output by 'clarity'. You can override using the
## `.groups` argument.
diamonds %>%
group_by(cut) %>%
summarise(var_price = var(price)) %>%
ggplot(aes(x = cut, y = var_price, fill = cut)) +
geom_col() +
labs(title = "Phương sai của giá theo loại cắt", x = "Loại cắt", y = "Phương sai giá") +
scale_fill_brewer(palette = "Set3")