Đọc dữ liệu

d<-read.csv(file.choose(), header=T)

Chọn các biến định tính

tbdt <- c("Gender", "MaritalStatus", "Homeowner",
          "City", "StateorProvince", "Country",
          "ProductFamily", "ProductDepartment", "ProductCategory")
dc <- d[, tbdt]
head(dc)
##   Gender MaritalStatus Homeowner          City StateorProvince Country
## 1      F             S         Y   Los Angeles              CA     USA
## 2      M             M         Y   Los Angeles              CA     USA
## 3      F             M         N     Bremerton              WA     USA
## 4      M             M         Y      Portland              OR     USA
## 5      F             S         Y Beverly Hills              CA     USA
## 6      F             M         Y Beverly Hills              CA     USA
##   ProductFamily ProductDepartment      ProductCategory
## 1          Food       Snack Foods          Snack Foods
## 2          Food           Produce           Vegetables
## 3          Food       Snack Foods          Snack Foods
## 4          Food            Snacks                Candy
## 5         Drink         Beverages Carbonated Beverages
## 6          Food              Deli          Side Dishes
dq <-table(d$MaritalStatus)/sum(nrow(d))
dq
## 
##         M         S 
## 0.4883704 0.5116296

Lập bảng tần số

tso <-table(dc$MaritalStatus)
tso
## 
##    M    S 
## 6866 7193

vậy bộ dữ liệu này có 6866 người là đã kết hôn và 7193 người là độc thân.

Lập bảng tần suất

tsuat <-table(dc$MaritalStatus)/sum(nrow(d))*100
tsuat
## 
##        M        S 
## 48.83704 51.16296

vậy bộ dữ liệu này có 48.8370439, 51.1629561% là đã kết hôn và NA, NA% là độc thân.

Biểu đồ

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
ggplot(dc, aes(x = MaritalStatus)) +
  geom_bar(fill = "steelblue") +
  labs(title = "Biểu đồ cột: MaritalStatus",
       x = "Tình trạng hôn nhân",
       y = "Tần số") +
  theme_minimal()

tsodf <- as.data.frame(tso)
names(tsodf) <- c("MaritalStatus", "TanSo")
library(ggplot2)
ggplot(tsodf, aes(x = "", y = TanSo, fill = MaritalStatus)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  labs(title = "Biểu đồ tròn: Tần số MaritalStatus") +
  theme_void() +
  geom_text(aes(label = TanSo),
            position = position_stack(vjust = 0.5),
            color = "white", size = 4)

tsuat_df <- as.data.frame(tsuat)
names(tsuat_df) <- c("MaritalStatus", "Tansuat")
library(ggplot2)
ggplot(tsuat_df, aes(x = MaritalStatus, y = Tansuat)) +
  geom_col(fill = "green") +
  labs(title = "Biểu đồ cột: MaritalStatus",
       x = "Tình trạng hôn nhân",
       y = "Tần suất (%)") +
  theme_minimal()

library(ggplot2)

ggplot(tsuat_df, aes(x = "", y = Tansuat, fill = MaritalStatus)) +
  geom_col(width = 1, color = "white") +
  coord_polar(theta = "y") +
  labs(title = "Biểu đồ tròn: MaritalStatus") +
  theme_void() +  
  geom_text(aes(label = paste0(Tansuat, "%")), 
            position = position_stack(vjust = 0.5),
            color = "white", size = 4)