R语言绘制散点图结合边际分布图

2023-11-20 15:50

本文主要是介绍R语言绘制散点图结合边际分布图,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本博客主要介绍使用R语言利用ggplot绘制散点图,并且在图像的两边绘制边际分布图(包括边际直方图与边际密度函数)

我们这里介绍两种方法进行绘制:

  1. 主要使用ggExtra结合ggplot2两个R包进行绘制。(胜在简洁方便)
  2. 使用cowplotggpubr进行绘制。(胜在灵活且美观)

下面的绘图我们均以iris数据集为例。


1. 使用ggExtra结合ggplot2

1)传统散点图

# library
library(ggplot2)
library(ggExtra)# classic plot
p <- ggplot(iris) +geom_point(aes(x = Sepal.Length, y = Sepal.Width, color = Species), alpha = 0.6, shape = 16) +  # alpha 调整点的透明度;shape 调整点的形状theme_bw() +theme(legend.position = "bottom") + # 图例置于底部labs(x = "Sepal Length", y = "Sepal Width") # 添加x,y轴的名称
p


下面我们一行代码添加边际分布(分别以密度曲线与直方图的形式来展现):

2)密度函数

# marginal plot: density
ggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)

3)直方图

# marginal plot: histogram
ggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)

4)箱线图(宽窄的显示会有些问题)

# marginal plot: boxplot
ggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)

5)小提琴图(会有重叠,不建议使用)

# marginal plot: violin
ggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)

6)密度函数与直方图同时展现

# marginal plot: densigram
ggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)


2. 使用cowplotggpubr

1)重绘另一种散点图

# Scatter plot colored by groups ("Species")
sp <- ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",color = "Species", palette = "jco",size = 3, alpha = 0.6) +border() +theme(legend.position = "bottom")
sp

2)有缝拼接

① 密度函数
library(cowplot)# Marginal density plot of x (top panel) and y (right panel)
xplot <- ggdensity(iris, "Sepal.Length", fill = "Species",palette = "jco")
yplot <- ggdensity(iris, "Sepal.Width", fill = "Species", palette = "jco") +rotate()# Cleaning the plots
sp <- sp + rremove("legend")
yplot <- yplot + clean_theme() + rremove("legend")
xplot <- xplot + clean_theme() + rremove("legend")
# Arranging the plot using cowplot
plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))

② 未被压缩的箱线图
# Marginal boxplot of x (top panel) and y (right panel)
xplot <- ggboxplot(iris, x = "Species", y = "Sepal.Length", color = "Species", fill = "Species", palette = "jco",alpha = 0.5, ggtheme = theme_bw())+rotate()
yplot <- ggboxplot(iris, x = "Species", y = "Sepal.Width",color = "Species", fill = "Species", palette = "jco",alpha = 0.5, ggtheme = theme_bw())
# Cleaning the plots
sp <- sp + rremove("legend")
yplot <- yplot + clean_theme() + rremove("legend")
xplot <- xplot + clean_theme() + rremove("legend")
# Arranging the plot using cowplot
plot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))

3)无缝拼接

# Main plot
pmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +geom_point() +color_palette("jco")
# Marginal densities along x axis
xdens <- axis_canvas(pmain, axis = "x") +geom_density(data = iris, aes(x = Sepal.Length, fill = Species),alpha = 0.7, size = 0.2) +fill_palette("jco")
# Marginal densities along y axis
# Need to set coord_flip = TRUE, if you plan to use coord_flip()
ydens <- axis_canvas(pmain, axis = "y", coord_flip = TRUE) +geom_density(data = iris, aes(x = Sepal.Width, fill = Species),alpha = 0.7, size = 0.2) +coord_flip() +fill_palette("jco")
p1 <- insert_xaxis_grob(pmain, xdens, grid::unit(.2, "null"), position = "top")
p2 <- insert_yaxis_grob(p1, ydens, grid::unit(.2, "null"), position = "right")
ggdraw(p2)


参考

  • Articles - ggpubr: Publication Ready Plots——Perfect Scatter Plots with Correlation and Marginal Histograms
  • Marginal distribution with ggplot2 and ggExtra

这篇关于R语言绘制散点图结合边际分布图的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/395725

相关文章

从基础到高级详解Go语言中错误处理的实践指南

《从基础到高级详解Go语言中错误处理的实践指南》Go语言采用了一种独特而明确的错误处理哲学,与其他主流编程语言形成鲜明对比,本文将为大家详细介绍Go语言中错误处理详细方法,希望对大家有所帮助... 目录1 Go 错误处理哲学与核心机制1.1 错误接口设计1.2 错误与异常的区别2 错误创建与检查2.1 基础

Go语言中json操作的实现

《Go语言中json操作的实现》本文主要介绍了Go语言中的json操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录 一、jsOChina编程N 与 Go 类型对应关系️ 二、基本操作:编码与解码 三、结构体标签(Struc

Python绘制TSP、VRP问题求解结果图全过程

《Python绘制TSP、VRP问题求解结果图全过程》本文介绍用Python绘制TSP和VRP问题的静态与动态结果图,静态图展示路径,动态图通过matplotlib.animation模块实现动画效果... 目录一、静态图二、动态图总结【代码】python绘制TSP、VRP问题求解结果图(包含静态图与动态图

python语言中的常用容器(集合)示例详解

《python语言中的常用容器(集合)示例详解》Python集合是一种无序且不重复的数据容器,它可以存储任意类型的对象,包括数字、字符串、元组等,下面:本文主要介绍python语言中常用容器(集合... 目录1.核心内置容器1. 列表2. 元组3. 集合4. 冻结集合5. 字典2.collections模块

基于Go语言开发一个 IP 归属地查询接口工具

《基于Go语言开发一个IP归属地查询接口工具》在日常开发中,IP地址归属地查询是一个常见需求,本文将带大家使用Go语言快速开发一个IP归属地查询接口服务,有需要的小伙伴可以了解下... 目录功能目标技术栈项目结构核心代码(main.go)使用方法扩展功能总结在日常开发中,IP 地址归属地查询是一个常见需求:

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

GO语言短变量声明的实现示例

《GO语言短变量声明的实现示例》在Go语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路