forked from duty-machine/duty-machine
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
15 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
title: "gground —— 绘制圆角矩形就是这么简单" | ||
date: 2024-11-19T01:15:08Z | ||
draft: ["false"] | ||
tags: [ | ||
"fetched", | ||
"生信杂货铺" | ||
] | ||
categories: ["Acdemic"] | ||
--- | ||
gground —— 绘制圆角矩形就是这么简单 by 生信杂货铺 | ||
------ | ||
<div><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com"><h1 data-tool="mdnice编辑器"><span></span><span>gground</span><span></span></h1><p data-tool="mdnice编辑器"><strong>gground</strong> 是一个 <code>ggplot2</code> 的扩展包,可用于生成圆角图形,例如圆角条形图和箱线图等,以帮助用户创建更美观且独特的可视化效果。</p><hr data-tool="mdnice编辑器"><h2 data-tool="mdnice编辑器"><span></span><span>特点</span><span></span></h2><ul data-tool="mdnice编辑器"><li><section>与 <code>ggplot2</code> 完美结合,在同名函数前加上 <code>round</code> 标识,但是语法完全一样,只是多了一个 <code>radius</code> 参数,用于控制圆角的弧度</section></li><li><section><code>radius</code> 参数设置需要谨慎,不要太大。过渡的圆角化会导致数据展示不全</section></li></ul><hr data-tool="mdnice编辑器"><h2 data-tool="mdnice编辑器"><span></span><span>安装</span><span></span></h2><p data-tool="mdnice编辑器">可以直接从 <span>GitHub</span><sup>[1]</sup> 安装,暂时没有上传到 <code>CRAN</code> 上</p><pre data-tool="mdnice编辑器"><span></span><code><span>if</span> (!<span>require</span>(<span>"devtools"</span>, quietly = <span>TRUE</span>))<br> install.packages(<span>"devtools"</span>)<br><br>devtools::install_github(<span>"dxsbiocc/gground"</span>)<br></code></pre><h2 data-tool="mdnice编辑器"><span></span><span>使用方式</span><span></span></h2><p data-tool="mdnice编辑器"><code>gground</code> 提供了各种图形功能,可以无缝集成到 <code>ggplot2</code> 中,帮助用户创建更美观、独特的数据可视化效果。</p><p data-tool="mdnice编辑器">导入</p><pre data-tool="mdnice编辑器"><span></span><code><span>library</span>(gground)<br><span>library</span>(tidyverse)<br></code></pre><h3 data-tool="mdnice编辑器"><span></span><span>1. geom_round_bar</span><span></span></h3><p data-tool="mdnice编辑器">条形图</p><pre data-tool="mdnice编辑器"><span></span><code>ggplot(mpg, aes(y = class)) +<br> geom_round_bar(aes(fill = drv), position = position_stack(reverse = <span>TRUE</span>)) +<br> theme(legend.position = <span>"top"</span>)<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016530" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIc3cTuiaI5Aicx5EtApKs5jEib5D2olkWjP8Q0xiaQCGqQqXQvbklEaEzpA/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIc3cTuiaI5Aicx5EtApKs5jEib5D2olkWjP8Q0xiaQCGqQqXQvbklEaEzpA/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>2. geom_round_col</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>df <- data.frame(trt = c(<span>"a"</span>, <span>"b"</span>, <span>"c"</span>), outcome = c(<span>2.3</span>, <span>1.9</span>, <span>3.2</span>))<br>ggplot(df, aes(trt, outcome)) +<br> geom_round_col()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016531" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI8KM9O85yfkMeP5hZD9cN35wasGXgSS2DhP7LUt1wicBaib3ZiapskGo8w/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI8KM9O85yfkMeP5hZD9cN35wasGXgSS2DhP7LUt1wicBaib3ZiapskGo8w/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>3. geom_round_boxplot</span><span></span></h3><p data-tool="mdnice编辑器">箱线图也很不错</p><pre data-tool="mdnice编辑器"><span></span><code>p <- ggplot(mpg, aes(class, hwy))<br><br>p + geom_round_boxplot(aes(fill = class), outlier.colour = <span>'red'</span>, <br> outlier.shape = <span>1</span>, radius = unit(<span>0.1</span>, <span>'inches'</span>)) + <br> geom_jitter(width = <span>0.2</span>) +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016533" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI4K1Skb00L3S0wianroKYF1LBJbeYDhwldSAUUbuwZFaJWRbfjwiaCJlA/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI4K1Skb00L3S0wianroKYF1LBJbeYDhwldSAUUbuwZFaJWRbfjwiaCJlA/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>4. geom_round_rect</span><span></span></h3><p data-tool="mdnice编辑器">圆角矩形绘制,其他很多图都是基于这个函数</p><pre data-tool="mdnice编辑器"><span></span><code>df <- data.frame(<br> x = rep(c(<span>2</span>, <span>5</span>, <span>7</span>, <span>9</span>, <span>12</span>), <span>2</span>),<br> y = rep(c(<span>1</span>, <span>2</span>), each = <span>5</span>),<br> z = factor(rep(<span>1</span>:<span>5</span>, each = <span>2</span>)),<br> w = rep(diff(c(<span>0</span>, <span>4</span>, <span>6</span>, <span>8</span>, <span>10</span>, <span>14</span>)), <span>2</span>)<br>)<br><br>ggplot(df, aes(xmin = x - w / <span>2</span>, xmax = x + w / <span>2</span>, ymin = y, ymax = y + <span>1</span>)) +<br> geom_round_rect(aes(fill = z), colour = <span>"grey50"</span>, radius = unit(<span>5</span>, <span>'mm'</span>))<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016534" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIXc7RrwxictnvQmNNNLqtjkKZQxBZH2BOWdibf37Vk9iapLr4s2ZQnm68A/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIXc7RrwxictnvQmNNNLqtjkKZQxBZH2BOWdibf37Vk9iapLr4s2ZQnm68A/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>5. geom_round_tile</span><span></span></h3><pre data-tool="mdnice编辑器"><span></span><code>ggplot(mpg) +<br> geom_round_tile(aes(x = factor(cyl), y = factor(drv), fill = factor(class)),<br> radius = unit(<span>0.1</span>, <span>'npc'</span>)) +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016532" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI7cB2w3OYRn74kagPZ3YnP62eqLo2PDR11vKTs2l6icBQFDb6a1PMOdw/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQI7cB2w3OYRn74kagPZ3YnP62eqLo2PDR11vKTs2l6icBQFDb6a1PMOdw/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>6. geom_round_crossbar</span><span></span></h3><p data-tool="mdnice编辑器"><code>crossbar</code> 绘制</p><pre data-tool="mdnice编辑器"><span></span><code>df <- data.frame(<br> trt = factor(c(<span>1</span>, <span>1</span>, <span>2</span>, <span>2</span>)),<br> resp = c(<span>1</span>, <span>5</span>, <span>3</span>, <span>4</span>),<br> group = factor(c(<span>1</span>, <span>2</span>, <span>1</span>, <span>2</span>)),<br> upper = c(<span>1.1</span>, <span>5.3</span>, <span>3.3</span>, <span>4.2</span>),<br> lower = c(<span>0.8</span>, <span>4.6</span>, <span>2.4</span>, <span>3.6</span>)<br>)<br>p <- ggplot(df, aes(trt, resp, colour = group))<br>p + geom_round_crossbar(aes(ymin = lower, ymax = upper), width = <span>0.2</span>) +<br> geom_point(position = position_dodge(width = <span>0.2</span>)) +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016537" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQItSpsU5zWSAp4Ha1mHfjq82MPLqQKFNuiaoFR0TWfLRJqIdiaJ4ic0coWw/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQItSpsU5zWSAp4Ha1mHfjq82MPLqQKFNuiaoFR0TWfLRJqIdiaJ4ic0coWw/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>7. geom_round_histogram</span><span></span></h3><p data-tool="mdnice编辑器">绘制直方图</p><pre data-tool="mdnice编辑器"><span></span><code>ggplot(diamonds, aes(price, fill = cut)) +<br> geom_round_histogram(binwidth = <span>500</span>) +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016538" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIicMkib5LVGPcblJt192XlFAc5wibib2MA7WicrkmKyCoWMNs70KPUnwICiag/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIicMkib5LVGPcblJt192XlFAc5wibib2MA7WicrkmKyCoWMNs70KPUnwICiag/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>8. geom_round_half_boxplot</span><span></span></h3><p data-tool="mdnice编辑器">还支持绘制半个箱线图</p><pre data-tool="mdnice编辑器"><span></span><code>ggplot(iris, aes(x = Species, y = Petal.Width, fill = Species)) +<br> geom_round_half_boxplot() +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016535" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQILoer58aWwUByg0qvspDUEDtoX5XCseet3rfibcFicjqQ6ozBMXbXSlqg/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQILoer58aWwUByg0qvspDUEDtoX5XCseet3rfibcFicjqQ6ozBMXbXSlqg/640?wx_fmt=png&from=appmsg"></figure><p data-tool="mdnice编辑器">也可以与其他半个图形结合使用</p><pre data-tool="mdnice编辑器"><span></span><code>ggplot() +<br> <br> geom_round_half_boxplot(<br> data = iris %>% filter(Species==<span>"setosa"</span>), <br> aes(x = Species, y = Sepal.Length, fill = Species), outlier.color = <span>NA</span>) +<br> <br> ggbeeswarm::geom_beeswarm(<br> data = iris %>% filter(Species==<span>"setosa"</span>),<br> aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side = <span>1</span><br> ) +<br> <br> geom_half_violin(<br> data = iris %>% filter(Species==<span>"versicolor"</span>), <br> aes(x = Species, y = Sepal.Length, fill = Species), side=<span>"r"</span>) +<br> <br> geom_half_dotplot(<br> data = iris %>% filter(Species==<span>"versicolor"</span>), <br> aes(x = Species, y = Sepal.Length, fill = Species), method=<span>"histodot"</span>, stackdir=<span>"down"</span>) +<br> <br> geom_round_half_boxplot(<br> data = iris %>% filter(Species==<span>"virginica"</span>), <br> aes(x = Species, y = Sepal.Length, fill = Species), side = <span>"r"</span>, errorbar.draw = <span>TRUE</span>,<br> outlier.color = <span>NA</span>) +<br> <br> geom_half_point(<br> data = iris %>% filter(Species==<span>"virginica"</span>), <br> aes(x = Species, y = Sepal.Length, fill = Species, color = Species), side = <span>"l"</span>) +<br> <br> scale_fill_manual(values = c(<span>"setosa"</span> = <span>"#cba1d2"</span>, <span>"versicolor"</span>=<span>"#7067CF"</span>,<span>"virginica"</span>=<span>"#B7C0EE"</span>)) +<br> scale_color_manual(values = c(<span>"setosa"</span> = <span>"#cba1d2"</span>, <span>"versicolor"</span>=<span>"#7067CF"</span>,<span>"virginica"</span>=<span>"#B7C0EE"</span>)) +<br> theme(legend.position = <span>"none"</span>)<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016536" data-ratio="0.7032332563510393" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQISeWrN6oLicFG3Sp3Z4pvRibN8lQibdbEGqfRWxiaH04XtlRcrrscoouHtQ/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQISeWrN6oLicFG3Sp3Z4pvRibN8lQibdbEGqfRWxiaH04XtlRcrrscoouHtQ/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>9. polar</span><span></span></h3><p data-tool="mdnice编辑器">绘制极坐标图形,只要添加 <code>coord_polar</code> 即可,很是方便</p><pre data-tool="mdnice编辑器"><span></span><code>group_by(mpg, class) %>%<br> summarise(percent = n() / nrow(mpg)) %>%<br> ggplot(aes(x = <span>1</span>, y = percent, fill = class)) +<br> geom_round_col(colour = <span>"white"</span>, radius = unit(<span>2</span>, <span>'pt'</span>)) +<br> coord_polar(theta = <span>'y'</span>) +<br> xlim(c(<span>0</span>, <span>2</span>)) +<br> theme_minimal()<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016539" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIibiaTt0DRPfkraVJptyTp85kibBpxXVeoRGjqmLPyU6Z0E3ANHrl8rBXw/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIibiaTt0DRPfkraVJptyTp85kibBpxXVeoRGjqmLPyU6Z0E3ANHrl8rBXw/640?wx_fmt=png&from=appmsg"></figure><h3 data-tool="mdnice编辑器"><span></span><span>10. element_round_rect</span><span></span></h3><p data-tool="mdnice编辑器">最后,还支持对主题的修改,继承子 <code>element_rect</code> 函数,可以更好的设置圆角边框。</p><pre data-tool="mdnice编辑器"><span></span><code>ggplot(mpg) +<br> geom_boxplot(aes(x = cyl, y = displ, fill = class)) +<br> theme(<br> legend.background = element_round_rect(linewidth = <span>1</span>, colour = <span>"grey"</span>, radius = unit(<span>5</span>, <span>'pt'</span>))<br> )<br></code></pre><figure data-tool="mdnice编辑器"><img data-imgfileid="100016543" data-ratio="0.707852193995381" data-src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQImXHscpvXThyMXJO4UKClfhpBejrmRDMNVzaFd0NtQS46VbicXuUiakCg/640?wx_fmt=png&from=appmsg" data-type="png" data-w="1732" src="https://mmbiz.qpic.cn/sz_mmbiz_png/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQImXHscpvXThyMXJO4UKClfhpBejrmRDMNVzaFd0NtQS46VbicXuUiakCg/640?wx_fmt=png&from=appmsg"></figure><h2 data-tool="mdnice编辑器"><span></span><span>结尾</span><span></span></h2><hr data-tool="mdnice编辑器"><p data-tool="mdnice编辑器">任何代码的问题都可以在知识星球中提问,自愿加入。<img data-imgfileid="100016544" data-ratio="1.5822222222222222" data-src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIWMev5yVKrtN7ibzNmnhCBL6ib1AgFjLmAKcAJiaPrSR8AzX3nqhziaw21Q/640?wx_fmt=jpeg&from=appmsg" data-type="jpeg" data-w="1125" src="https://mmbiz.qpic.cn/sz_mmbiz_jpg/R14BjDEcNWJVGF7qaD0sn3VD8OwZ8BQIWMev5yVKrtN7ibzNmnhCBL6ib1AgFjLmAKcAJiaPrSR8AzX3nqhziaw21Q/640?wx_fmt=jpeg&from=appmsg"></p><section data-tool="mdnice编辑器"><span>参考资料</span></section><section data-tool="mdnice编辑器"><span><span>[1] </span><p>GitHub: <em>https://github.com/dxsbiocc/gground</em></p></span></section></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/tlxT1x6XmXBmMxNlVr1Jrw",target="_blank" rel="noopener noreferrer">原文链接</a> |