如何使用R语言绘制散点图结合边际分布图
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要为大家展示了"如何使用R语言绘制散点图结合边际分布图",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用R语言绘制散点图结合边际分布图"这篇
千家信息网最后更新 2025年11月07日如何使用R语言绘制散点图结合边际分布图
这篇文章主要为大家展示了"如何使用R语言绘制散点图结合边际分布图",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"如何使用R语言绘制散点图结合边际分布图"这篇文章吧。
主要使用ggExtra结合ggplot2两个R包进行绘制。(胜在简洁方便)使用cowplot与ggpubr进行绘制。(胜在灵活且美观)
下面的绘图我们均以iris数据集为例。
1. 使用ggExtra结合ggplot2
1)传统散点图
# librarylibrary(ggplot2)library(ggExtra)# classic plotp <- 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: densityggMarginal(p, type = "density", groupColour = TRUE, groupFill = TRUE)
3)直方图
# marginal plot: histogramggMarginal(p, type = "histogram", groupColour = TRUE, groupFill = TRUE)
4)箱线图(宽窄的显示会有些问题)
# marginal plot: boxplotggMarginal(p, type = "boxplot", groupColour = TRUE, groupFill = TRUE)
5)小提琴图(会有重叠,不建议使用)
# marginal plot: violinggMarginal(p, type = "violin", groupColour = TRUE, groupFill = TRUE)
6)密度函数与直方图同时展现
# marginal plot: densigramggMarginal(p, type = "densigram", groupColour = TRUE, groupFill = TRUE)
2. 使用cowplot与ggpubr
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")sp2)有缝拼接
① 密度函数
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 plotssp <- sp + rremove("legend")yplot <- yplot + clean_theme() + rremove("legend")xplot <- xplot + clean_theme() + rremove("legend")# Arranging the plot using cowplotplot_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 plotssp <- sp + rremove("legend")yplot <- yplot + clean_theme() + rremove("legend")xplot <- xplot + clean_theme() + rremove("legend")# Arranging the plot using cowplotplot_grid(xplot, NULL, sp, yplot, ncol = 2, align = "hv", rel_widths = c(2, 1), rel_heights = c(1, 2))3)无缝拼接
# Main plotpmain <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + color_palette("jco")# Marginal densities along x axisxdens <- 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)以上是"如何使用R语言绘制散点图结合边际分布图"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
边际
密度
分布图
语言
内容
函数
直方图
篇文章
线图
学习
帮助
调整
简洁
美观
一行
两个
代码
传统
同时
名称
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
nec容错服务器是哪个国家的
网络安全现状与对策
重庆服务器机房价格实惠
奥松服务器
软件开发设计专业排名
sql数据库生成脚本
濮阳淘宝软件开发
自动建数据库表
什么叫软件开发环境
储能EMS软件开发
软件开发的本质可以概括为
手机怎么弄虚拟服务器
qq公开数据库查询系统
软件开发阶段数据确认
软件开发人员简历模板
奥的斯电梯服务器呼梯
重庆安防时钟监控网管服务器
服务器错误且稍后再试304
小程序上传图片到服务器
刺猬猫服务器维护中
网络安全公司利润排行
工作站可以做为服务器吗
数据库安全系数
神经网络技术的研究现状
数据库由于索引重复值
冠唐数据库二次开发
用户体验软件开发工程师
pve服务器有什么用
网络安全普法宣传
思科模拟器web服务器怎么编译