Việc 2: Soạn biểu đồ phân bố histogram
library(ggplot2)
p = ggplot(data = df, aes(x = log(income), fill=factor(gender))) + geom_histogram(fill = "darkgreen", col = "white") + labs(x="Income(logscale)", y="Frequency", title = " Phân bố thu nhập")
show(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).

Hãy vẽ biểu đồ phân bố thu nhập (income) theo giới tính
(gender)
p = ggplot(data = df, aes(x = log(income), fill=factor(gender))) + geom_histogram(col = "white") + labs(x="Income(logscale)", y="Frequency", title = " Phân bố thu nhập")
show(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).

p <- ggplot(data = df, aes(x = log(income), fill = factor(gender))) +
geom_histogram(col = "white", position = "stack") +
labs(x = "Income (logscale)", y = "Frequency", title = "Phân bố thu nhập") +
scale_fill_manual(
values = c("1" = "red", "2" = "purple"), # gán màu theo giá trị cụ thể
name = "Giới tính",
labels = c("Nam", "Nữ"))
show(p)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_bin()`).

Theo tỉ lệ
p = ggplot(data = df, aes(x = log(income), fill=factor(gender))) + geom_density(alpha=0.5, col = "white") + labs(x="Income(logscale)", y="Frequency", title = " Phân bố thu nhập")
show(p)
## Warning: Removed 241 rows containing non-finite outside the scale range
## (`stat_density()`).
