GGally与pairs相关关系图_史上最全(一)

本文介绍了如何使用R语言的GGally包结合ggplot2绘制多变量的相关关系图,包括ggmatrix和ggpairs函数的详细用法,展示了不同类型的图例和自定义设置,便于理解数据间的关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

欢迎关注天善智能,我们是专注于商业智能BI,人工智能AI,大数据分析与挖掘领域的垂直社区,学习,问答、求职一站式搞定!

对商业智能BI、大数据分析挖掘、机器学习,python,R等数据领域感兴趣的同学加微信:tstoutiao,邀请你进入数据爱好者交流群,数据爱好者们都在这儿

四川大学在读研究生

简介

对于多个变量之间的相关关系,常常使用相关关系图来可视化,R自带有pairs()函数,

可以画相关关系图,但是比较复杂,我们先介绍基于ggplot2的GGally包。

等介绍完,再介绍pairs()函数。

1.ggmatrix()

ggmatrix()可以将多个ggplot2绘图对象,按照矩阵进行排列。

1.1

矩阵第1列

1library(ggplot2)

2data(tips, package = "reshape")

3

4head(tips)

5

6g1<-ggplot(tips,aes(x=total_bill,fill=sex))+

7geom_density(show.legend=FALSE)

8

9g2<-ggplot(tips,aes(x=total_bill,fill=sex))+

10geom_histogram(position=position_stack(),show.legend=FALSE)+

11facet_grid(rows=vars(time))#以time变量行分面

12

13g3<-ggplot(tips,aes(x=total_bill,y=tip,color=sex))+

14geom_point(show.legend=FALSE)

15

3901436-fdd7bac4a762b7db

1.2

矩阵第2列

1library(ggplot2)

2

3g4<-ggplot(tips,aes(x=time,y=total_bill,fill=sex))+

4geom_boxplot(show.legend=FALSE)

5

6g5<-ggplot(tips,aes(x=time,fill=sex))+

7geom_bar(position=position_stack(),show.legend=FALSE)

8

9g6<-ggplot(tips,aes(x=tip,fill=sex))+

10geom_histogram(position=position_stack(),show.legend=FALSE)+

11coord_flip() +

12facet_grid(cols=vars(time))

13

1.3

矩阵第3列

1library(ggplot2)

2library(dplyr)

3library(tibble)

4

5# 第一个图

6text_1<-round(cor(tips$total_bill,tips$tip),3)

7tips_female<-as.tibble(tips) %>

% filter(sex == "Female") %>% as.data.frame()

8tips_male<-as.tibble(tips) %>% filter(sex == "Male") %>% as.data.frame()

9text_2<-round(cor(tips_female$total_bill,tips_female$tip),3)

10text_3<-round(cor(tips_male$total_bill,tips_male$tip),3)

11mytext<-c(text_1,text_2,text_3)

12mytext<-paste0(c("Cor", "Female", "Male"), ":",mytext)

13mytext<-data.frame(text=mytext,

14x=5,

15y=c(6,4,2),

16stringsAsFactors=FALSE)

17

18g7<-ggplot(data=mytext[-1,],aes(x=x,y=y,label=text,color=text))+

19geom_text(show.legend=F)+

20geom_text(data=mytext[1,],aes(x=x,y=y,label=text),

21color="black")

22

23rm(text_1,tips_female,tips_male,text_2,text_3,mytext)

24

25# 第2个图

26g8<-ggplot(tips,aes(x=time,y=tip,fill=sex))+

27geom_boxplot(show.legend=FALSE)+

28coord_flip()

29

30# 第3个图

31g9<-ggplot(tips,aes(x=tip,fill=sex))+

32geom_density(show.legend=FALSE)

33

1.4

customLayout合并图形

1library(customLayout)

2# 创建画布

3mylay <- lay_new(

4mat = matrix(1:9, ncol =3))

5

6plot_list <-list(g1, g2, g3, g4, g5, g6, g7, g8, g9)

7

8lay_grid(plot_list, mylay)# ggplot2绘图列表传参,传递到画布mylay

9

10rm(g1, g2, g3, g4, g5, g6, g7, g8, g9, mylay)

3901436-4975994a6ffe7014

1.5

ggmatrix合并图形

1library(GGally)

2

3gg_m <- ggmatrix(

4plots = plot_list,# 绘图对象列表

5nrow =3, ncol =3,# 行数和列数

6xAxisLabels = c("Total Bill","Time of Day","Tip"),

7yAxisLabels = c("Total Bill","Time of Day","Tip"),

8byrow =FALSE,# 按列排

9title ="g

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值