不过是大梦一场空
1引言
翻出之前的一篇推文 跟着 Microbiome 学画图:堆积柱形图的多层注释。如果大家看过的话,可以发现我们需要经过一些复杂的坐标计算才能一层层添加注释。这次我们拿 jjAnno 来实战一下。
原文图:

2构建数据
library(jjAnno)
library(reshape2)
library(ggplot2)
library(RColorBrewer)
# 构造数据
set.seed(123)
df <- as.data.frame(matrix(data = sample(0:50,105,replace = T),ncol = 5))
# 给列命名
colnames(df) <- c('Heterogeneous selection','Homogeneous selection',
'Dispersal limitation','Homogenizing dispersal',
'Undominated')
# 添加分类
df$type <- rownames(df)
df$type1 <- rep(c('Plastic leaf','Phylloplane','Leaf endosphere',
'Rhizoplane','Root endosphere','Rhizosphere soil',
'Bulk soil'),each = 3)
df$type2 <- rep(c('Seeding stage','Tasseling stage','Mature stage'),7)
df$type3 <- rep(c('Air','Plant','Soil'),c(3,12,6))
# check
head(df,3)
# Heterogeneous selection Homogeneous selection Dispersal limitation Homogenizing dispersal Undominated type type1 type2 type3
# 1 30 25 6 30 24 1 Plastic leaf Seeding stage Air
# 2 14 6 26 15 31 2 Plastic leaf Tasseling stage Air
# 3 50 41 31 29 45 3 Plastic leaf Mature stage Air
# wide to long
da <- melt(df,id.vars = c('type','type1','type2','type3'))
# check
head(da,3)
# type type1 type2 type3 variable value
# 1 1 Plastic leaf Seeding stage Air Heterogeneous selection 30
# 2 2 Plastic leaf Tasseling stage Air Heterogeneous selection 14
# 3 3 Plastic leaf Mature stage Air Heterogeneous selection 50
# order
da$type <- factor(da$type,levels = unique(da$type))
绘图:
# plot
p <- ggplot(data = da,aes(x = type,y = value)) +
geom_col(aes(fill = variable),
position = position_fill()) +
scale_fill_brewer(palette = 'Set1',name = 'Ecological process') +
scale_y_continuous(labels = scales::percent_format()) +
theme_bw(base_size = 18) +
coord_cartesian(expand = 0,clip = 'off') +
xlab('') + ylab('') +
theme(plot.margin = margin(t = 1,b = 3,unit = 'cm'))
p

3添加底层注释
添第一层:
# add first annotation
p1 <- annoRect(object = p,
annoPos = 'botomn',
aesGroup = T,
aesGroName = 'type1',
yPosition = c(-0.08,-0.05),
rectWidth = 0.9,
pCol = c('#FA26A0','#388E3C','#77ACF1',
'#FFC947','#B85C38','#AAAAAA','#364547'),
pFill = c('#FA26A0','#388E3C','#77ACF1',
'#FFC947','#B85C38','#AAAAAA','#364547'))

添加第二层:
# add second annotation
p2 <- annoRect(object = p1,
annoPos = 'botomn',
xPosition = 1:21,
yPosition = c(-0.12,-0.09),
rectWidth = 0.9,
pCol = rep(brewer.pal(3,'Set1'),7),
pFill = rep(brewer.pal(3,'Set1'),7))

添加第三层:
# add third annotation
p3 <- annoRect(object = p2,
annoPos = 'botomn',
aesGroup = T,
aesGroName = 'type3',
yPosition = c(-0.15,-0.13),
rectWidth = 0.9,
pCol = rep('black',3),
pFill = rep('black',3),
addText = T,
textHVjust = -0.05,
textCol = rep('black',3))

4添加图例
我们再添加对应注释的图例上去:
# add legend
p4 <- annoLegend(object = p3,
xPosition = 24.6,
yPosition = 0.8,
labels = c('Plastic leaf','Phylloplane','Leaf endosphere',
'Rhizoplane','Root endosphere','Rhizosphere soil',
'Bulk soil'),
pch = 15,
col = c('#FA26A0','#388E3C','#77ACF1',
'#FFC947','#B85C38','#AAAAAA','#364547'),
vgap = 0.1,
cex = 1.2)
# second legend
annoLegend(object = p4,
xPosition = 24.6,
yPosition = 0.2,
labels = c('Seeding stage','Tasseling stage','Mature stage'),
pch = 15,
col = brewer.pal(3,'Set1'),
vgap = 0.1,
cex = 1.2)

5结尾
可以看到, jjAnno 工作的还不错,基本可以完成大部分的任务。
如果大家喜欢的话,欢迎在我的 github
https://github.com/junjunlab/jjAnno
右上角送上你的小星星哦。
往期精品(点击图片直达文字对应教程)
后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集