马赛克图
有两个分类变量时,可以画一个马赛克图来展示数据。
载入R包"ggmasaic"。
install.packages("ggmosaic")
library(ggmosaic)
library(ggplot2) # 需先安装:install.packages("ggplot2")
summary(titanic)
ggplot(data = titanic) +
geom_mosaic(aes(x = product(Class), fill = Survived), show.legend = TRUE)
ggplot(data = titanic) +
geom_mosaic(aes(x = product(Class), fill = Survived), show.legend = TRUE) +
scale_fill_manual(values = c("cadetblue4", "lightsalmon3")) +
theme_light()
2023-02-21 17:37