Skip to content

Commit

Permalink
ggbreak优雅的截断坐标轴
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Jan 15, 2025
1 parent b165889 commit fe4a2b8
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2025-01/ggbreak优雅的截断坐标轴.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "ggbreak优雅的截断坐标轴"
date: 2025-01-15T08:44:33Z
draft: ["false"]
tags: [
"fetched",
"R语言数据分析指南"
]
categories: ["Acdemic"]
---
ggbreak优雅的截断坐标轴 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>本节来介绍如何对坐标轴进行截断,主要用到<strong>「ggbreak」</strong>绘图过程倒也简单,但就是细节调整比较麻烦;下面小编就来仔细写一下绘图过程,<strong>「数据及代码已经上传VIP群,加群的观众老爷请自行下载」</strong>,细节还算有一些,请细细品味</p><span>❞</span></blockquote><blockquote data-tool="mdnice编辑器"><span>❝</span><p><code>有需要学习数据可视化的朋友欢迎加入小编2022年度VIP群</code>,目前群内已经上传公众号文档<strong>「数据+代码约200篇」</strong>,<code>VIP交流群(1)已经500人满员</code>,随着内容不断增多,为了更好的创做内容现在进群需<strong>「付费149元」</strong>,若转发此文档到朋友圈集赞20个可优惠20元,早进早享受;<strong>「一定让你感受到物超所值」</strong></p><span>❞</span></blockquote><h3 data-tool="mdnice编辑器"><span></span><span><span></span>加载R包</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code><span>library</span>(tidyverse)<br><span>library</span>(ggbreak)<br><span>library</span>(MetBrewer)<br><span>library</span>(ggprism)<br><span>library</span>(magrittr)<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>数据清洗</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>df &lt;- read_tsv(<span>"data.xls"</span>) %&gt;% slice(.,-<span>1</span>) %&gt;%<br>  filter(sample !=<span>"Geometric mean"</span>) %&gt;% <br>  pivot_longer(-sample) %&gt;% <br>  group_by(name) %&gt;% <br>  summarise(value_mean=mean(value),sd=sd(value),se=sd(value)/sqrt(n()))<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>绘制基础条形</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>p &lt;- df %&gt;% ggplot(aes(name,value_mean))+<br>  geom_errorbar(aes(ymax=value_mean+se,ymin=value_mean-se),width=<span>0.2</span>,color=<span>"grey30"</span>)+<br>  geom_col(width = <span>0.5</span>,color=<span>"grey50"</span>)+<br>  labs(x=<span>NULL</span>,y=<span>NULL</span>)+<br>  theme(axis.text.y=element_text(color=<span>"black"</span>,size=<span>8</span>,margin=margin(r=<span>1</span>)),<br>      axis.text.x=element_text(color=<span>"black"</span>,size=<span>8</span>,margin=margin(t=<span>5</span>),<br>                               angle = <span>90</span>,vjust=<span>0.5</span>,hjust=<span>1</span>),<br>      axis.title.x = element_text(size=<span>11</span>,margin=margin(t=<span>8</span>),color=<span>"black"</span>,face=<span>"bold"</span>),<br>      plot.margin=unit(c(<span>0.3</span>,<span>0.5</span>,<span>0.5</span>,<span>0.5</span>),units=,<span>"cm"</span>), <br>      panel.background = element_blank(),   <span># 移除灰色背景框</span><br>      axis.line = element_line(color=<span>"black"</span>),<br>      axis.ticks.length.x = unit(<span>.1</span>, <span>"cm"</span>),<br>      axis.ticks.length.y = unit(<span>.2</span>, <span>"cm"</span>))<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加坐标截断</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>p +  scale_y_break(c(<span>180</span>,<span>200</span>),scales=<span>2</span>,expand = c(<span>0</span>,<span>0</span>)) <span># scales控制截断的比例,默认是固定宽度</span><br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.67" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZoRl17YSKpJHyPEibk4YWouyXttS56GaZyu9fVQpSCXM94anSKWZIs0znl7Bywib0iaHUAVbGicmeHgg/640?wx_fmt=png" data-type="png" data-w="800" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZoRl17YSKpJHyPEibk4YWouyXttS56GaZyu9fVQpSCXM94anSKWZIs0znl7Bywib0iaHUAVbGicmeHgg/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>通过space=0来将我们不需要展示的部分缩小,之后再通过<code>scale_y_continuous</code>定义新的刻度范围,但是如上操作会添加一条辅助线,因此再通过<strong>「theme」</strong>函数将其隐藏</p><span>❞</span></blockquote><pre data-tool="mdnice编辑器"><span></span><code>p +scale_y_break(c(<span>0</span>,<span>200</span>),space=<span>0</span>)+<br>  scale_y_continuous(limits = c(<span>0</span>,<span>600</span>),expand = c(<span>0</span>,<span>0</span>),<br>                     breaks = c(<span>200</span>,<span>300</span>,<span>400</span>,<span>500</span>,<span>600</span>),<br>                     labels = c(<span>200</span>,<span>300</span>,<span>400</span>,<span>500</span>,<span>600</span>))+<br>  theme(axis.ticks.y.right=element_blank(),<br>         axis.text.y.right = element_blank(),<br>        axis.line.y.right = element_blank())<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.67" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZoRl17YSKpJHyPEibk4YWou9HEhHRP3L5NnaRmiakfh9DgphPCsIKYqQzRwicvoTeeDibaiaBgMrX4k7A/640?wx_fmt=png" data-type="png" data-w="800" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAZoRl17YSKpJHyPEibk4YWou9HEhHRP3L5NnaRmiakfh9DgphPCsIKYqQzRwicvoTeeDibaiaBgMrX4k7A/640?wx_fmt=png"></figure><blockquote data-tool="mdnice编辑器"><span>❝</span><p>本节介绍到此结束,喜欢的观众老爷欢迎分享转发;<strong>「当然更推荐大家加入我的VIP交流群」</strong>扫描下方二维码加小编微信<strong>「付费149元」</strong>后邀请进群,<strong>「一定让你感受到物超所值」</strong>,<strong>「添加小编微信请备注来意,以便高效处理」</strong></p><span>❞</span></blockquote><h3 data-tool="mdnice编辑器"><span></span><span><span></span>小编微信</span><span></span></h3><p><img data-galleryid="" data-ratio="1" data-s="300,640" data-src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAaNu6sU2UCYkxFq9ibq75wuaO0lqFoYz1icUo4jh3N2icAWECmibgPvqyReur0FCp7JNKO0icnRAsbO9ug/640?wx_fmt=jpeg" data-type="jpeg" data-w="430" src="https://mmbiz.qpic.cn/mmbiz_jpg/EibnicgwScTAaNu6sU2UCYkxFq9ibq75wuaO0lqFoYz1icUo4jh3N2icAWECmibgPvqyReur0FCp7JNKO0icnRAsbO9ug/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/Fp-QZIU0HZRGXCHuf8YF8A",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit fe4a2b8

Please sign in to comment.