# 绘图技巧 | 这种精美的”排序图“怎么做？(附练习数据）

• R-ggbump包基本绘图简介
• R-ggbump包实例演示

## R-ggbump包基本绘图函数简介

R-ggbump包主要包含：geom_bump()和geom_sigmoid(),两个函数主要绘制随时间变化的平滑曲线排名图，内置参数也几乎相同，如下：

```( mapping = NULL,
data = NULL,
geom = "line",
position = "identity",
na.rm = FALSE,
show.legend = NA,
smooth = 8,
direction = "x",
inherit.aes = TRUE,
...)
```

Example Of geom_bump()

Example Of geom_sigmoid()

## R-ggbump包实例演示

### geom_bump()绘图函数

「样例一：」我们直接构造数据并对结果继续美化操作，代码如下：

```library(tidyverse)
library(ggtext)
library(hrbrthemes)
library(wesanderson)
library(LaCroixColoR)
library(RColorBrewer)
library(ggbump)
#数据构建和处理
test_01 <- tibble(country = c("India", "India", "India", "Sweden", "Sweden", "Sweden", "Germany", "Germany", "Germany", "Finland", "Finland", "Finland"),
year = c(2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013, 2011, 2012, 2013),
value = c(492, 246, 246, 369, 123, 492, 246, 369, 123, 123, 492, 369))
test_01_plot <- test_01 %>% group_by(year) %>% mutate(rank=rank(value, ties.method = "random")) %>% ungroup
#可视化绘制
charts01_cus <- ggplot(data = test_01_plot,aes(x = year,y = rank,color=country))+
ggbump::geom_bump(size=2,smooth = 8) +
#添加圆点
geom_point(size=8)+
# 添加文本信息
geom_text(data = test_01_plot %>% filter(year==min(year)),
aes(x=year-.1,label=country),size=6,fontface="bold",hjust = 1) +
geom_text(data = test_01_plot %>% filter(year == max(year)),
aes(x = year + .1, label = country), size = 6,fontface="bold",hjust = 0) +
#修改刻度
scale_x_continuous(limits = c(2010.6, 2013.4),
breaks = seq(2011, 2013, 1)) +
scale_color_manual(values = lacroix_palette("Pamplemousse", type = "discrete"))+
labs(
title = "Example of <span style='color:#D20F26'>ggbump::geom_bump function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_bump()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")+
hrbrthemes::theme_ft_rc(base_family = "Roboto Condensed") +
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=12),
plot.caption = element_markdown(face = 'bold',size = 10),
legend.position = "none",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())+
scale_y_reverse()
```

Exercise Of geom_bump()

「样例二：」

```#读入数据

#定义颜色
cols <- c(
"#882B1A", "#676564", "#E8751A", "#779AC4", "#646E3F",
"#9D49B9", "#C09F2F", "#65955B", "#284D95","#B34525")
#可视化绘制
charts02_cus <- ggplot(data = df,aes(x = race_num,y = rank,color=team_name,group=team_name)) +
geom_bump(smooth = 7, size = 2.5) +
geom_point(data = df %>% filter(race_num == 1),size = 5) +
geom_point(data = df %>% filter(race_num == 7),size = 5, shape = 21, fill = "black",
stroke = 2) +
geom_text(data = df %>% filter(race_num == 7),aes(x = 7.12,label = team_name),
family = "Cinzel",fontface = 'bold',size = 4, hjust = 0) +
#添加序号
geom_point(data = tibble(x = .8, y = 1:10), aes(x = x, y = y),
inherit.aes = F,shape=21,color = "grey95",size = 5,stroke = 1.) +
geom_text(data = tibble(x = .8, y = 1:10), aes(x = x, y = y, label = y),
inherit.aes = F,size=2.5,fontface = 'bold',
color = "grey95")+
coord_cartesian(clip = "off") +
scale_x_continuous(
expand = c(.01, .01),
limits = c(.8, 8.1),
breaks = 1:7,
labels = c("Begain",glue::glue("Race {1:5}"), "Finish"),
sec.axis = dup_axis()
) +
scale_y_reverse(
expand = c(.05, .05),
breaks = 1:16
) +
scale_color_manual(
values = cols,
guide = F
) +
labs(x="",y="",
title = "Example of <span style='color:#D20F26'>ggbump::geom_bump() function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_bump()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")+
theme(
plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "gray50",face = 'bold',
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=12,color = "gray50"),
plot.caption = element_markdown(face = 'bold',size = 10),
plot.background = element_rect(fill = "black", color = "black"),
panel.background = element_rect(fill = "black", color = "black"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.text.x.top = element_text(size = 9,color = "grey95",family = "Cinzel",face = 'bold'),
axis.text.y.left = element_blank(),
axis.ticks = element_blank()
)
```

(这里涉及到很多关于主题设置的语句，小编在绘制之后可是又对一些常用的语句熟悉了一遍哦，希望小伙伴们多练习)

Exercise2 Of geom_bump

### geom_sigmoid()绘图函数

```data_test <- tibble(
x = c(0.5,0.5,1,1,1,1,1,1),
xend = c(1, 1, 3, 3, 3 ,3, 3, 3),
y = c(4, 4, 6, 6, 6, 2, 2, 2),
yend = c(6,2,7,6,5,3,2,1),
group = c("Python","R","Numpy","Pandas","Matplolib","Dplyr","Data.table","Ggplot2")
)
#可视化绘制
charts04_cus <- ggplot(data_test) +
geom_sigmoid(data = data_test %>% filter(xend < 3),
aes(x = x, y = y, xend = xend, yend = yend, group = factor(group)),
direction = "x", color = "#cb7575", size = 2, smooth = 6) +
geom_sigmoid(data = data_test %>% filter(group %in% c("Numpy","Pandas","Matplolib")),
aes(x = x, xend = xend, y = y, yend = yend, group=group),
direction = "x",color = "#cb7575", size = 2, smooth = 12) +
geom_sigmoid(data = data_test %>% filter(y==2),
aes(x = x, xend = xend, y = y, yend = yend, group=group),
direction = "x",color = "#cb7575", size = 2, smooth = 11) +
geom_label(data = tibble(x = 0.1, y = 4, label = "DataScience"),
aes(x, y, label = label), inherit.aes = F, size = 10, color = "white",
fill = "#004E66",family = "Cinzel",nudge_x = -.15) +
geom_label(data = data_test %>% filter(xend < 3),
aes(x = xend, y = yend, label = group),inherit.aes = F, size = 8,
color = "white", fill = "#004E66", family = "Cinzel",
hjust=0.5,nudge_y = .45,nudge_x = .3)+
geom_label(data = data_test %>% filter(xend == 3),
aes(x = xend, y = yend, label = group),inherit.aes = F, size = 7,
color = "white", fill = "#004E66", family = "Cinzel",hjust=0) +
labs(x="",y="",
title = "Example of <span style='color:#D20F26'>ggbump::geom_sigmoid() function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>geom_sigmoid()</span>",
caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>")+
scale_x_continuous(limits = c(-.5,4)) +
theme(plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "gray50",face = 'bold',
size = 20, margin = margin(t = 1, b = 12)),
plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=12,color = "gray50"),
plot.caption = element_markdown(face = 'bold',size = 10),
panel.grid = element_blank(),
axis.line = element_blank(),
axis.ticks = element_blank(),
axis.text.y = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
panel.background = element_rect(fill = "#353848"),
plot.background = element_rect(fill = "#353848",colour = "#353848"))
```

(还是一样，使用了很多常用的主题设置语句，希望小伙伴们掌握、熟悉)

Exercise Of geom_sigmoid

## 数据获取

0 条评论

• ### 绘图技巧 | 这么漂亮的桑葚图你确定不来看看怎么做的？(附练习数据)

今天小编继续给大家送上优秀可视化教程推文，同时，我们也提供练习数据哦~本期的重点是是关于桑葚图(Sankey Diagram),中文名字叫法不同，我们还是以英文...

• ### 微生物组—宏基因组分析专题研讨会(2020.2)

在广大粉丝的期待下，《生信宝典》联合《宏基因组》在2020年2月14-16日，北京鼓楼推出《宏基因组分析》专题培训第七期，为大家提供一条走进生信大门的捷径、为同...

• ### 这里有 300 篇 Python 与机器学习类原创笔记

主要包括计算机科学中基本的算法与数据结构，结合算法思想和Leetcode实战，总结介绍。

• ### 最悲催的三流职业：一位机械设计师的神吐槽，想成为优秀设计师必看

按照国内的某种说法，人才分四类：首先是搞艺术，因为中华艺术是神圣的所以咱们常人无法达到；然后一流人才做销售，在中国各种政府采购及规则，能把销售做好不亚于搞艺术。...

• ### 有了这款神器，人人都是作曲家！

摘要：人工智能作曲APP Amper Music的简单介绍和测评，以及人工智能和机器学习的小科普

• ### 前沿 | DeepMind提出SPIRAL：使用强化对抗学习，实现会用画笔的智能体

选自DeepMind 作者：Ali Eslami等 机器之心编译 参与：路雪 近日，DeepMind 发布博客，提出一种结合了对抗训练和强化学习的智能体 SPI...

• ### 8年Android老码农现身分享：一个月面试6家大厂屡败屡战，那些面试学习上走过的弯路

他说他们部门调整，虽然最后他留了下来，但还是非常焦虑。人无远虑必有近忧，他这次被刺激到了，想提高一下自己，以免下次再有类似的心惊肉跳。但怎么提高呢？

• ### 如何学好数据结构与算法

随着科学技术的发展，人工智能已渗透到各个行业，算法工程师非常火爆，急缺大量人才，年薪也越来越高。刚毕业30-40万很常见。很多人想入手学习算法，那么多算法，究竟...

• ### 知道肠道菌种组成之后怎么做功能注释？

在广大粉丝的期待下，《生信宝典》联合《宏基因组》在2019年11月1-3日，北京鼓楼推出《宏基因组分析》专题培训第六期，为大家提供一条走进生信大门的捷径、为同行...

• ### Python 全栈 191 问（附答案）

作为程序员，你的电脑里、书架上，一定少不了 Python 的资料和课程。免费的电子书，花钱买的课，实体书籍...

• ### 解决决策树的过拟合

参看书籍：Machine Learning(Tom Mitchell) 之前我们已经比较详细的介绍啦决策树的相关知识，如ID3（Machine Learning...

• ### 26秒训练ResNet，用这些技巧一步步压缩时间，Jeff Dean都称赞：干得漂亮

现在，谷歌AI掌门人Jeff Dean转发推荐了一个训练ResNet的奇技淫巧大礼包，跟着它一步一步实施，训练9层ResNet时，不仅不需要增加GPU的数量，甚...

• ### 半天2k赞火爆推特！李飞飞高徒发布33条神经网络训练秘技

AI大佬Andrej Karpathy (简称AK) ，刚刚发布了一篇长长长长博客，苦口婆心地列举了33条技巧和注意事项，全面避免大家踩坑，推特已有2,300多...

• ### 10个数据可视化技巧,让你一看就懂！

我必须对你说实话：当我学习数据科学时，我完全低估了绘图的重要性。没错，那时一切都一团糟：我从头开始学习 python、熟悉了所有可能的算法、理解了所有东西背后的...

• ### 数模竞赛|数学建模比赛该如何准备？

作者介绍：苗枫，华中科技大学管理学院18级博士研究生，本科时全国大学生数学建模国赛一等奖，并多次带队获得美国数学建模竞赛一等奖

• ### Excel小技巧65：在单元格区域中精确放置图表

在Excel中绘制嵌入的图表时，Excel会将图表放置在合适的地方，如下图1所示。

• ### 吴恩达《ML Yearning》| 关于学习曲线的分析&与人类级别的表现对比

MachineLearning YearningSharing 是北京科技大学“机器学习研讨小组”旗下的文献翻译项目，其原文由Deep Learning.ai ...

• ### 如何在算法比赛中获得出色的表现 :改善模型的5个重要技巧

如果你最近才开始使用Kaggle，或者你是这个平台的老用户，你可能想知道如何轻松地提高你的模型的性能。以下是我在Kaggle之旅中积累的一些实用技巧。建立自己的...

• ### 【学习】1月份推荐给程序员们的技术书书单

时光飞逝，不知不觉，微信君已经和小伙伴们走过了2014，感谢你们的支持。小编会在2015年加倍努力，与你们一起分享好书。 2015年，首月，好多技术书的付印计划...