马赛克图

有两个分类变量时,可以画一个马赛克图来展示数据。


载入R包"ggmasaic"。


install.packages("ggmosaic")
library(ggmosaic)
library(ggplot2) # 需先安装:install.packages("ggplot2")


使用R包"ggmasaic"中的数据作图。


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()


▌欢迎关注公众号:R语言和统计
▌课程相关咨询可添加R师妹微信: kefu_rstats
▌邮箱:contact@rstats.cn
▌网站:www.rstats.cn
我们致力于让R语言和统计变得简单!



2023-02-21 17:37
首页    ggplot2    马赛克图