使用 jjAnno 轻松给堆积条形图添加注释

不过是大梦一场空

43903a263be97a7b6d7f21663a26fc42.gif

1引言

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

原文图:

22294c2a8faf7b2b58194f428f44932b.png

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
7b33b0575b85a18461277356e32c1f62.png

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'))
0df15b4f3ad89df34d1575292528ed16.png

添加第二层:

# 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))
16b17d3581b2357341eb6090354270ca.png

添加第三层:

# 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))
0841c30e8115de1eb155248030f0a4c7.png

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)
2424e7190228a6b13a495a8e60025f9b.png

5结尾

可以看到, jjAnno 工作的还不错,基本可以完成大部分的任务。

如果大家喜欢的话,欢迎在我的 github https://github.com/junjunlab/jjAnno 右上角送上你的小星星哦。

058130ae598901d60cd52afdab81f02b.png

往期精品(点击图片直达文字对应教程)

b81648002bfc9151253ccafd29a60c08.jpeg

0c8926cedf0049c9b0b8e2c52e195ca4.jpeg

d7f75c931a272906377d22b7fbdfc057.jpeg

9e9d7fce09c9465aceaaeb8dc7dc1805.jpeg

4f40acaa60f43ce32ede0d80ce8f424d.jpeg

d93dcccebd3d42a49a5f554740fda9b3.jpeg

bb0f0ec5389699c99df4a20e8744821f.jpeg

256bbac092022f25cba25bef5f744831.jpeg

19e42399f56800a45dc1a22af6a1e6a5.jpeg

0d63b6ec5f6ad996da4bc726f1b61374.jpeg

7ed06a7fe022b9543868cdc7156d06d4.jpeg

d024968ddc410ca3af32e06475f3d9df.jpeg

4b6224cfc1f49e88d4902e24aca5a323.png

426a3d9247f55eefb90a7f871128cfb4.png

45e116c41b15630417585d2fe60533c8.png

58433257551697f1f3fcdd8e1f10f097.png

297562c9a67b72aa9c4b88dbaac47a46.jpeg

f219f51136a84916e195d518db795cda.jpeg

0227878bfdb9939636f104af01c4404c.jpeg

5d155b0328313068f90ee74338376677.jpeg

e2cad5f54b2614f0a66e90728f65cca4.png

7ca173fbe753a70691440286bbecfe7c.png

06e4eeedb930ddbd1dba74953afd9ed3.jpeg

2b8274ff45d8612c03ed75c29dd1f3f6.png

6f63ebb860354539cd006b9dd72db87e.png

fcdfb0bdde88f52801ae4d2b9fc60be8.jpeg

128d85d0fb597d6b1ed7f14efa3b659f.png

5510c1e9ec57835cf3fd73948f7f1a13.png

机器学习

后台回复“生信宝典福利第一波”或点击阅读原文获取教程合集

6da51cae69da152bb68dfc19acbc47e1.jpeg

bbfa36ea84575578210231a3aa7e5a2f.jpeg

fed993700adf749d01f5d4d18e3f13ff.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值