Skip to content

Commit

Permalink
ggplot2优雅绘制配对关系散点图
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Nov 4, 2023
1 parent e19541d commit 1e4c526
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2023-11/ggplot2优雅绘制配对关系散点图.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "ggplot2优雅绘制配对关系散点图"
date: 2023-11-04T21:05:51Z
draft: ["false"]
tags: [
"fetched",
"R语言数据分析指南"
]
categories: ["Acdemic"]
---
ggplot2优雅绘制配对关系散点图 by R语言数据分析指南
------
<div><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><h3 data-tool="mdnice编辑器"><span></span><span><span></span>欢迎关注R语言数据分析指南</span><span></span></h3><blockquote data-tool="mdnice编辑器"><span>❝</span><p>本周会员交流群内有朋友问绘制配对散点图的问题,此图使用ggpairs绘制比较方便,但是会员群内的老爷们也许有更高的要求,在此小编给出ggplot2的版本进行演示,通过批量绘图拼图的方式来完成,整个过程仅参考。希望对各位观众老爷能有所帮助。<strong>「数据代码已经整合上传到2023VIP交流群」</strong>,加群的观众老爷可自行下载,有需要的朋友可关注文末介绍加入VIP交流群。</p><span>❞</span></blockquote><pre data-tool="mdnice编辑器"><span></span><code><span>library</span>(tidyverse) <br><span>library</span>(GGally) <br><span>library</span>(patchwork) <br><span>library</span>(ggpubr)    <span># 载入 ggpubr 包,提供了添加统计注释的功能</span><br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>ggpairs绘制配对关系散点图</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>ggpairs(iris, columns=1:4, aes(color=Species), upper = <span>"blank"</span>) + <br>  theme_minimal() <span># 使用最小主题</span><br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.8629629629629629" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibh3dbnunXwiaicHA2gIcsVicBp9Pt8UBps2MISDrsHqVpic1EFE1T72BqicIw/640?wx_fmt=png" data-type="png" data-w="1080" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibh3dbnunXwiaicHA2gIcsVicBp9Pt8UBps2MISDrsHqVpic1EFE1T72BqicIw/640?wx_fmt=png"></figure><pre data-tool="mdnice编辑器"><span></span><code>iris %&gt;% as_tibble() %&gt;%  <span># 将 iris 数据集转换为 tibble</span><br>  ggplot(aes(Sepal.Length, Sepal.Width, color=Species), shape=<span>21</span>) + <span># 设置散点图的美学映射</span><br>  geom_point(aes(fill=Species)) + <span># 添加散点图层,填充颜色表示种类</span><br>  scale_color_manual(values=c(<span>"#788FCE"</span>, <span>"#A88AD2"</span>, <span>"#E6956F"</span>)) + <span># 手动设置颜色</span><br>  ggpubr::stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = <span>"~`,`~"</span>), group=<span>1</span>), color=<span>"black"</span>,<br>           label.x.npc = <span>"left"</span>) + <span># 添加统计相关性标签</span><br>  theme_classic() + <span># 使用经典主题</span><br>  theme(legend.position = <span>"none"</span>) <span># 隐藏图例</span><br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.9653333333333334" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibhI6WDH0sJr8uGZ15icsF4I9OzrfdUjrMTR51rSKohOpImutlRxeDsSCA/640?wx_fmt=png" data-type="png" data-w="750" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibhI6WDH0sJr8uGZ15icsF4I9OzrfdUjrMTR51rSKohOpImutlRxeDsSCA/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>定义函数</span><span></span></h3><blockquote data-tool="mdnice编辑器"><span>❝</span><p>定义函数的目的在于批量出图可以简化代码</p><span>❞</span></blockquote><pre data-tool="mdnice编辑器"><span></span><code><span># 用于创建带有相关性标签的散点图</span><br>plot_scatter &lt;- <span>function</span>(data, x, y, colors) {<br>  ggplot(data, aes_string(x = x, y = y, color = <span>"Species"</span>, fill = <span>"Species"</span>)) + <span># 设置散点图的美学映射</span><br>    geom_point(shape = <span>21</span>) + <span># 添加散点图层</span><br>    scale_color_manual(values = colors) + <span># 手动设置颜色</span><br>    scale_fill_manual(values = colors) + <span># 手动设置填充颜色</span><br>    stat_cor(aes(label = paste(..rr.label.., ..p.label.., sep = <span>"~`,`~"</span>), group = <span>1</span>),<br>             color = <span>"black"</span>, label.x.npc = <span>"left"</span>, label.y.npc = <span>"top"</span>, size=<span>3</span>) + <span># 添加统计相关性标签</span><br>    theme_classic() + <span># 使用经典主题</span><br>    theme(legend.position = <span>"none"</span>, <span># 隐藏图例</span><br>          axis.ticks = element_blank(), <span># 隐藏坐标轴刻度</span><br>          axis.text = element_blank()) + <span># 隐藏坐标轴文本</span><br>    labs(x = <span>NULL</span>, y = <span>NULL</span>) <span># 移除坐标轴标签</span><br>}<br><br><span># 选择 iris 数据集中的数值列</span><br>numeric_cols &lt;- iris %&gt;% select_if(is.numeric) %&gt;% names()<br>colors &lt;- c(<span>"#788FCE"</span>, <span>"#A88AD2"</span>, <span>"#E6956F"</span>) <span># 设置颜色</span><br><br><span># 获取所有唯一的列对组合</span><br>combinations &lt;- combn(numeric_cols, <span>2</span>, simplify = <span>FALSE</span>)<br><span># 为每对组合创建散点图</span><br>plots &lt;- map(combinations, ~plot_scatter(as_tibble(iris), .x[<span>1</span>], .x[<span>2</span>], colors))<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>拼图</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>((plots[[<span>1</span>]]+labs(y=<span>"Sepal.Width"</span>)+theme(axis.text.y=element_text(color=<span>"black"</span>,size=<span>8</span>)))+<br>    plot_spacer()+plot_spacer())/<br>((plots[[<span>2</span>]]+labs(y=<span>"Petal.Length"</span>)+theme(axis.text.y=element_text(color=<span>"black"</span>,size=<span>8</span>)))+<br>   plots[[<span>4</span>]]+plot_spacer())/<br>((plots[[<span>3</span>]]+labs(x=<span>"Sepal.Length"</span>,y=<span>"Petal.Width"</span>)+<br>    theme(axis.text=element_text(color=<span>"black"</span>,size=<span>8</span>)))+<br>  (plots[[<span>5</span>]]+labs(x=<span>"Sepal.Width"</span>)+<br>     theme(axis.text.x=element_text(color=<span>"black"</span>,size=<span>8</span>)))+<br>  plots[[<span>6</span>]]+labs(x=<span>"Petal.Length"</span>)+<br>   theme(axis.text.x=element_text(color=<span>"black"</span>,size=<span>8</span>)))<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.8342592592592593" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibhn7nJ9oRruz3u36CfpCge0tjKH0Tu76RPGWdHicyCJbWVqicDv58enFzA/640?wx_fmt=png" data-type="png" data-w="1080" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAbNveNuDmCOTpfdmLpZmgibhn7nJ9oRruz3u36CfpCge0tjKH0Tu76RPGWdHicyCJbWVqicDv58enFzA/640?wx_fmt=png"></figure><blockquote data-tool="mdnice编辑器"><span>❝</span><p>本节内容介绍到此结束,过程仅供参考;有需要学习时间可视化的朋友,欢迎到小编的<strong>「淘宝店铺」</strong> <strong>「R语言数据分析指南」</strong>购买<strong>「2023年度会员文档」</strong>同步更新中<strong>「售价149元」</strong>,内容主要包括各种<strong>「高分论文的图表分析复现以及一些个性化图表的绘制」</strong>均包含数据+代码;按照往年数据小编年产出约在150+以上</p><span>❞</span></blockquote><p data-tool="mdnice编辑器">购买后微信发小编订单截图即邀请进新的会员交流群,小编的文档为按年售卖,只包含当年度的<strong>「除系列课程外」</strong>的文档,有需要往年文档的朋友也可下单购买,需要了解更多信息的朋友欢迎淘宝店铺交流咨询。</p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>淘宝扫一扫</span><span></span></h3><p data-tool="mdnice编辑器"><img data-ratio="1.5902335456475585" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAZ86jzR8HAJ3L0XMqyywfovQjp0icHttM8wNrElV7vmEeEiaNn9LJPdkb4ta7fnHYQvdxLFibFyDpG8A/640?wx_fmt=jpeg&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" data-w="942" src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAZ86jzR8HAJ3L0XMqyywfovQjp0icHttM8wNrElV7vmEeEiaNn9LJPdkb4ta7fnHYQvdxLFibFyDpG8A/640?wx_fmt=jpeg&amp;tp=wxpic&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1"></p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>淘宝店铺(有需要欢迎关注)</span><span></span></h3><p><img data-galleryid="" data-ratio="1.0210420841683367" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAbvhPDLGT8NaialEsht92PTYNJWpmVLfoYGic1uha5FyBrDCibibZCLjiazgvpT1XcdwibfVywD2el0VAgg/640?wx_fmt=jpeg" data-type="jpeg" data-w="998" src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAbvhPDLGT8NaialEsht92PTYNJWpmVLfoYGic1uha5FyBrDCibibZCLjiazgvpT1XcdwibfVywD2el0VAgg/640?wx_fmt=jpeg"></p><h3 data-tool="mdnice编辑器"><span></span><span><span></span>关注下方公众号下回更新不迷路</span><span></span></h3><section><mp-common-profile data-pluginname="mpprofile" data-id="Mzg3MzQzNTYzMw==" data-headimg="http://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZF0rpeZII9Ltl26VbVagriczTria1fib3XgjwwHEHFjPzkmGpqWDVVHBSzhENictUM2iavAKiaM5lc9USw/0?wx_fmt=png" data-nickname="R语言数据分析指南" data-alias="YanJANtwo" data-signature="R语言重症爱好者,喜欢绘制各种精美的图表,喜欢的小伙伴可以关注我,跟我一起学习" data-from="0" data-is_biz_ban="0"></mp-common-profile></section></section><p><mp-style-type data-value="3"></mp-style-type></p></div>
<hr>
<a href="https://mp.weixin.qq.com/s/O7j3c1gfwdbg-t_LnCItKg",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit 1e4c526

Please sign in to comment.