
今天小编给大家介绍一个绘图小娇巧-多图汇总时图标签(如A、B (a)、(b)、(I)和(II)等),这类技巧尤其是在科学文献中经常用到,虽然可以手动添加此类标签,但在绘图过程中自动标记处理则可方便的多。主要内容如下:
在R语言绘制图表小编主要使用ggplot2和一些第三方包,这里小编就介绍R-ggpubr和R-patchwork包绘制方法(R-ggplot2包可通过labs(tag="**") 方法进行添加,这里不多做介绍),详细内容如下:
在R-ggpubr包中,你可以通过ggarrange()函数在一个页面中添加多个子图和添加对应的tag,如下:
library(tidyverse)
library(hrbrthemes)
library(ggpubr)
library(patchwork)
data("ToothGrowth")
df <- ToothGrowth
df$dose <- as.factor(df$dose)
wdata = data.frame(
sex = factor(rep(c("F", "M"), each=200)),
weight = c(rnorm(200, 55), rnorm(200, 58)))
#可视化绘制
# Box plot
bxp <- ggboxplot(df, x = "dose", y = "len",
color = "dose", palette = "jco") +
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")
# Dot plot
dp <- ggdotplot(df, x = "dose", y = "len",
color = "dose", palette = "jco")+
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")
#Density plot
des01 <- ggdensity(wdata, x = "weight",
add = "mean", rug = TRUE,
color = "sex", fill = "sex",
palette = c("#00AFBB", "#E7B800"))
# Density plot
dens02 <- ggdensity(df, x = "len", fill = "dose", palette = "jco")+
hrbrthemes::theme_ipsum(base_family = "Roboto Condensed")
# 添加子图tag
ggarrange(bxp, dp, des01,dens02, ncol = 2, nrow = 2,labels = c("(A)","(B","C)","D"))

Example Of ggpubr::ggarrange()
「注意」:这里主要就是介绍ggpubr::ggarrange()函数,其可以设置行列数和labels,灵活添加子图tag,更多详细内容可参考:ggpubr::ggarrange()标签添加[1]
R-patchwork包主要用于灵活实现多子图的拼接和位置调整等操作,这里则介绍其添加Tag功能的绘图函数plot_annotation(),如下:
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
patchwork <- (p1 + p2) / p3
patchwork + plot_annotation(
title = "Example of <span style='color:#D20F26'>patchwork::plot_annotation function</span>",
subtitle = "processed charts with <span style='color:#1A73E8'>plot_annotation()</span>",
caption = "Visualization by <span style='color:#0057FF'>DataCharm</span>",
tag_levels = 'A',
) &
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=15),
plot.caption = element_markdown(hjust = 1,face = 'bold',size = 12))

Example Of patchwork::plot_annotation()
「注意」:这里的plot_annotation()函数还可以实现多种其他Tag样式,图添加前缀(tag_prefix参数) 和后缀(tag_suffix参数) 等,更多详细内容可参考:patchwork::plot_annotation()[2]
以上就是小编列举的关于R语言绘制多子图Tag的样例,感兴趣的小伙伴可自行查阅,详细理解~~
介绍完R绘制图表添加Tag的技巧后,小编再介绍如何使用Python进行绘制,这里小编主要介绍的库为Python-ProPlot库,首先,我们看一下官网关于A-b-c labels的例子,如下:
import proplot as pplt
fig = pplt.figure(space=0, refwidth='10em')
axs = fig.subplots(nrows=3, ncols=3)
axs.format(
abc='A.', abcloc='ul',
xticks='null', yticks='null', facecolor='gray5',
xlabel='x axis', ylabel='y axis',
suptitle='A-b-c label offsetting, borders, and boxes',
)
axs[:3].format(abcloc='l', titleloc='l', title='Title')
axs[-3:].format(abcbbox=True) # also disables abcborder
# axs[:-3].format(abcborder=True) # this is already the default

Example01 Of proplot format set
「注意」:从这里可以看出proplot库对子图Tag的设置提供了多种样式,如:abcloc、abcbbox、abcborder等。
接下来举一个具体的图表例子,如下:
import proplot as pplt
import numpy as np
pplt.rc.cycle = '538'
fig, axs = pplt.subplots(ncols=2, span=False, share='labels', refwidth=2.3)
labels = ['a', 'bb', 'ccc', 'dddd', 'eeeee']
hs1, hs2 = [], []
# On-the-fly legends
state = np.random.RandomState(51423)
for i, label in enumerate(labels):
data = (state.rand(20) - 0.45).cumsum(axis=0)
h1 = axs[0].plot(
data, lw=4, label=label, legend='ul',
legend_kw={'order': 'F', 'title': 'column major'}
)
hs1.extend(h1)
h2 = axs[1].plot(
data, lw=4, cycle='Set3', label=label, legend='r',
legend_kw={'lw': 8, 'ncols': 1, 'frame': False, 'title': 'modified\n handles'}
)
hs2.extend(h2)
# Outer legends
ax = axs[0]
ax.legend(
hs1, loc='b', ncols=3, title='row major', order='C',
facecolor='gray2'
)
ax = axs[1]
ax.legend(hs2, loc='b', ncols=3, center=True, title='centered rows')
axs.format(abc="(A)",xlabel='xlabel', ylabel='ylabel', suptitle='A-b-c labels formatting demo')

Example01 Of proplot format set
此外,这个例子还详细说明了使用proplot库进行图例添加的例子,更多详细内容可参考:proplot A-b-c labels 设置[3]。
这里补充一点:刚学习Python绘图的小伙伴,小编真心建议学习下ProPlot库,特别是对学术图表绘制时。
今天的推文,小编简单介绍了如何使用R和Python在绘图过程中灵活添加子图Tag,这一技巧在绘制多个子图时则十分方便有效。这里小编建议小伙伴们可以多使用Python-ProPlot库进行绘制哈~~
[1]ggpubr::ggarrange()标签添加: https://rpkgs.datanovia.com/ggpubr/reference/ggarrange.html。
[2]patchwork::plot_annotation(): https://patchwork.data-imaginist.com/articles/guides/annotation.html。
[3]proplot A-b-c labels 设置: https://proplot.readthedocs.io/en/latest/subplots.html。