Skip to content

Commit

Permalink
gtExtras优雅的拓展表格绘制技巧
Browse files Browse the repository at this point in the history
  • Loading branch information
ixxmu committed Aug 28, 2023
1 parent e7cd4a4 commit 4933cea
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/2023-08/gtExtras优雅的拓展表格绘制技巧.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
title: "gtExtras优雅的拓展表格绘制技巧"
date: 2023-08-28T22:21:27Z
draft: ["false"]
tags: [
"fetched",
"R语言数据分析指南"
]
categories: ["Acdemic"]
---
gtExtras优雅的拓展表格绘制技巧 by R语言数据分析指南
------
<div><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><blockquote data-tool="mdnice编辑器"><span>❝</span><p>本节来介绍如何使用<strong>「gtExtras」</strong>包来针对<strong>「gt」</strong>绘制的表格做增强,下面小编就来通过几个案例做演示,希望各位观众老爷能够喜欢。更多详细内容请参考作者官方文档</p><span>❞</span></blockquote><h3 data-tool="mdnice编辑器"><span></span><span><span></span>官方文档</span><span></span></h3><blockquote data-tool="mdnice编辑器"><span>❝</span><p>https://jthomasmock.github.io/gtExtras/</p><span>❞</span></blockquote><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><h3 data-tool="mdnice编辑器"><span></span><span><span></span>加载R包</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>install.packages(<span>"gtExtras"</span>)<br><span>library</span>(tidyverse)<br><span>library</span>(gtExtras)<br><span>library</span>(gt)<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span><span></span>修改主题</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars) %&gt;% gt() %&gt;% gt_theme_538()<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.4222462203023758" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtX7Qm2jT8rsrrIAbHr3N3weEet3uZAjU0OdichibclibjatoViaiajicZCpWg/640?wx_fmt=png" data-type="png" data-w="926" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtX7Qm2jT8rsrrIAbHr3N3weEet3uZAjU0OdichibclibjatoViaiajicZCpWg/640?wx_fmt=png"></figure><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars) %&gt;% gt() %&gt;% gt_theme_espn()<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.5358695652173913" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtryUwBL9XvXjZEzdEQExG374KPicWhvVHLFsyyO7ic2odK9BMXUsAIJ4Q/640?wx_fmt=png" data-type="png" data-w="920" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtryUwBL9XvXjZEzdEQExG374KPicWhvVHLFsyyO7ic2odK9BMXUsAIJ4Q/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加标题</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars) %&gt;% gt() %&gt;% <br>  gt_theme_nytimes() %&gt;% <br>  tab_header(title = <span>"Table styled like the NY Times"</span>)<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.6896551724137931" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtDxz4ATTXGevADiapicMEdiczODic1opmGuI3qm780tAB5wicdbUwBEKh8aA/640?wx_fmt=png" data-type="png" data-w="812" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtDxz4ATTXGevADiapicMEdiczODic1opmGuI3qm780tAB5wicdbUwBEKh8aA/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加颜色</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars) %&gt;% gt::gt() %&gt;% gt_hulk_col_numeric(mpg)<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.5852017937219731" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtlJKT0N95icxDe7mJTOAX0KpGdqfXQUxADicf7iaOUHwRZrRBlNt6icGjng/640?wx_fmt=png" data-type="png" data-w="892" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtlJKT0N95icxDe7mJTOAX0KpGdqfXQUxADicf7iaOUHwRZrRBlNt6icGjng/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>增加颜色范围</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars) %&gt;% gt::gt() %&gt;%<br>  gt_hulk_col_numeric(mpg:disp, trim = <span>TRUE</span>) <br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.5852017937219731" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtB8E0vyTZftdt52boRzKNmtJiaDYnRbWxaZOeoN2gn2aKaTrFSiaicgZrA/640?wx_fmt=png" data-type="png" data-w="892" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtB8E0vyTZftdt52boRzKNmtJiaDYnRbWxaZOeoN2gn2aKaTrFSiaicgZrA/640?wx_fmt=png"></figure><pre data-tool="mdnice编辑器"><span></span><code>mtcars %&gt;% head() %&gt;% gt() %&gt;%<br>  gt_color_rows(cyl, <br>    palette = <span>"ggthemes::colorblind"</span>, <br>    domain = range(mtcars$cyl),<br>    pal_type = <span>"discrete"</span>)<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.5852017937219731" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtHfZnezSHia5RkmIyaw5LxYuIHcroUsbpaj4j235zNvvnGqjU7JbahHw/640?wx_fmt=png" data-type="png" data-w="892" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtHfZnezSHia5RkmIyaw5LxYuIHcroUsbpaj4j235zNvvnGqjU7JbahHw/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加离散型颜色</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars[,<span>1</span>:<span>5</span>]) %&gt;% <br>  tibble::rownames_to_column(<span>"car"</span>) %&gt;% gt() %&gt;% <br>  gt_highlight_rows(rows = <span>2</span>, font_weight = <span>"normal"</span>) <br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.7532467532467533" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtrI7DE3Yapp230r0jpoy9zVyLylDmLo2eEZFna1C8r0E3ScabwfywBg/640?wx_fmt=png" data-type="png" data-w="693" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtrI7DE3Yapp230r0jpoy9zVyLylDmLo2eEZFna1C8r0E3ScabwfywBg/640?wx_fmt=png"></figure><h4 data-tool="mdnice编辑器"><span></span><span><span></span>突出展示数据</span><span></span></h4><pre data-tool="mdnice编辑器"><span></span><code>head(mtcars[,<span>1</span>:<span>5</span>]) %&gt;% <br>  tibble::rownames_to_column(<span>"car"</span>) %&gt;% <br>  gt() %&gt;% <br>  gt_highlight_rows(<br>    rows = <span>5</span>, <br>    fill = <span>"lightgrey"</span>,<br>    bold_target_only = <span>TRUE</span>,<br>    target_col = car)<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.7114337568058077" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtwHML7qJAVHpksmD1IBae0rCPAicp9bhbdpDNyLkhUJnlmB7Jpl3gnyw/640?wx_fmt=png" data-type="png" data-w="551" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtwHML7qJAVHpksmD1IBae0rCPAicp9bhbdpDNyLkhUJnlmB7Jpl3gnyw/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加折线图</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>mtcars %&gt;%<br>   dplyr::group_by(cyl) %&gt;%<br>   dplyr::summarize(mpg_data = list(mpg), .groups = <span>"drop"</span>) %&gt;%<br>   gt() %&gt;%<br>   gt_plt_sparkline(mpg_data)<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.9817073170731707" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtsPTtmJsibWkkN3qKkY5FQ1icjOibSI6fQInq2QU6sCWwlCnfanfK2vNicA/640?wx_fmt=png" data-type="png" data-w="328" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtsPTtmJsibWkkN3qKkY5FQ1icjOibSI6fQInq2QU6sCWwlCnfanfK2vNicA/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>添加条形图</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>mtcars %&gt;%<br>  head() %&gt;%<br>  dplyr::select(cyl, mpg) %&gt;%<br>  dplyr::mutate(mpg_pct_max = round(mpg/max(mpg) * <span>100</span>, digits = <span>2</span>),<br>                mpg_scaled = mpg/max(mpg) * <span>100</span>) %&gt;%<br>  dplyr::mutate(mpg_unscaled = mpg) %&gt;%<br>  gt() %&gt;%<br>  gt_plt_bar_pct(column = mpg_scaled, scaled = <span>TRUE</span>) %&gt;%<br>  gt_plt_bar_pct(column = mpg_unscaled, scaled = <span>FALSE</span>, fill = <span>"blue"</span>, background = <span>"lightblue"</span>) %&gt;%<br>  cols_align(<span>"center"</span>, contains(<span>"scale"</span>)) %&gt;%<br>  cols_width(<span>4</span> ~ px(<span>125</span>),<span>5</span> ~ px(<span>125</span>))<br></code></pre><figure data-tool="mdnice编辑器"><img data-ratio="0.5865168539325842" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCticJBGAFE4ZyQ2EKLxwraw2gBKOkXUAOSEDqcw1Aic1ZvTWntAvvZvXIQ/640?wx_fmt=png" data-type="png" data-w="890" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCticJBGAFE4ZyQ2EKLxwraw2gBKOkXUAOSEDqcw1Aic1ZvTWntAvvZvXIQ/640?wx_fmt=png"></figure><blockquote data-tool="mdnice编辑器"><span>❝</span><p>可以看到使用gtExtras包来对gt的表格进行进一步处理还是很方便的,,那么本节介绍到此结束,有需要学习个性化数据可视化的朋友,欢迎到小编的<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><figure data-tool="mdnice编辑器"><img data-ratio="1.6141479099678457" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtpNNDXwQ7RYG0icIia16YbIQmj7qQkRribK7qJv4WbV525G14InXiaeAibdA/640?wx_fmt=png" data-type="png" data-w="622" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtpNNDXwQ7RYG0icIia16YbIQmj7qQkRribK7qJv4WbV525G14InXiaeAibdA/640?wx_fmt=png"></figure><h3 data-tool="mdnice编辑器"><span></span><span><span></span>2023会员群案例展示</span><span></span></h3><p data-tool="mdnice编辑器"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtqZQJrFGW0yoIuiaCB49TEEe55QYv4CWFMKEmzAt9CMXylOvh5d1nX5g/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtqZQJrFGW0yoIuiaCB49TEEe55QYv4CWFMKEmzAt9CMXylOvh5d1nX5g/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtkduvbsEr6SKVJwcuxWJkLiaGWFY44z6b5rGWzZq75fTy4zoMdtTWaLQ/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtkduvbsEr6SKVJwcuxWJkLiaGWFY44z6b5rGWzZq75fTy4zoMdtTWaLQ/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtV9hxL0sThSGTnE43Vt07SVeM4rYJRmZBpZywibI12Oq7enB4kGt7YNw/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtV9hxL0sThSGTnE43Vt07SVeM4rYJRmZBpZywibI12Oq7enB4kGt7YNw/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtGgSq2qicFlE7W7F6zkqCJdxUzLIUkt7d5PvTnXbK4oIJOD2KKoX2Ntg/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtGgSq2qicFlE7W7F6zkqCJdxUzLIUkt7d5PvTnXbK4oIJOD2KKoX2Ntg/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCt6SXbbYOVxtibMlmrXKUEqvbzwKp0G0iczVCmkL92F5g3AJ6MgZEOk81Q/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCt6SXbbYOVxtibMlmrXKUEqvbzwKp0G0iczVCmkL92F5g3AJ6MgZEOk81Q/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtPYzSBXBia7zXn7aibNbSWDu2y6iaxOYVM20jgIyg0tDvvj4uQfCujn2Lg/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtPYzSBXBia7zXn7aibNbSWDu2y6iaxOYVM20jgIyg0tDvvj4uQfCujn2Lg/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCt1IECd5hu1EMdMBibibYFess0XQhIRRPRibw5INZOl9fRjNU2hWOsNJykw/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCt1IECd5hu1EMdMBibibYFess0XQhIRRPRibw5INZOl9fRjNU2hWOsNJykw/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtP5SdDTPps381X53iaoxAHPYhXFHydzAdgbLHEB7BUXKPF70p4lIoFQA/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtP5SdDTPps381X53iaoxAHPYhXFHydzAdgbLHEB7BUXKPF70p4lIoFQA/640?wx_fmt=png"><img data-ratio="0.4255555555555556" data-src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtzXOHmPzUVwJToFcBRqLD6iagTjzrTVqHKuIvx4VvH8uVXOkBcia6lFzg/640?wx_fmt=png" data-type="png" data-w="900" src="https://mmbiz.qpic.cn/mmbiz_png/EibnicgwScTAa12smtXYcoWupdPRkqyVCtzXOHmPzUVwJToFcBRqLD6iagTjzrTVqHKuIvx4VvH8uVXOkBcia6lFzg/640?wx_fmt=png"></p></section><p><br></p><p><mp-style-type data-value="3"></mp-style-type></p></div>
<hr>
<a href="https://mp.weixin.qq.com/s/V3eLhGY4F-aBrKZ_KBS88A",target="_blank" rel="noopener noreferrer">原文链接</a>

0 comments on commit 4933cea

Please sign in to comment.