查看原文
其他

jjAnno 优雅的帮你添加注释

JunJunLab 老俊俊的生信笔记 2023-06-15


人从众𠈌

1引言

冰冻三尺,非一日之寒,见的图多了,于是渐渐发现自己的绘图会遇到一些瓶颈。下次再遇见则艰难的踌躇不前。

绘图的时候,大部分我们都是在图里进行各种操作的,但是在图外(坐标轴外)画图好像不是一件容易的事,当然是有解决方案的,比如 Ai 或者 Ps, 又或者多写一些代码来完成,我们看看下面这张来自 nature 文章的部分图片:

  • 你会不会好奇图旁边那些各种各样的注释(文字,线段,矩形等等)是如何添加的?
  • 有没有 R 包可以做这样的事情?

好在 ggplot 提供了一个 annotation_custom 这样一个函数可以基于 grid 基础上绘制一些基础图形, 于是我对一些基础函数进行了封装,编写了 jjAnno 这样一个包,可以方便的在图像上添加不同类型的注释。

可以做什么:

  • useMyCol: 提供 ArchR 包里的颜色。
  • annoPoint: 添加点注释。
  • annoRect: 添加矩形注释。
  • annoSegment: 添加线段注释。
  • annoImage: 添加图片注释。
  • annoLegend: 添加图例注释。

2安装

# install.packages("devtools")
devtools::install_github("junjunlab/jjAnno")

library(jjAnno)

github 地址:

https://github.com/junjunlab/jjAnno

参考手册地址:

https://github.com/junjunlab/jjAnno/wiki/jjAnno-0.0.1

注意:

你的 ggplot 图应该加上:

coord_cartesian(clip = 'off')

3useMyCol

查看所有颜色主题名称:

# show all plattes
useMyCol(showAll = T)

# [1] "stallion"     "stallion2"    "calm"         "kelly"        "bear"         "ironMan"
# [7] "circus"       "paired"       "grove"        "summerNight"  "zissou"       "darjeeling"
# [13] "rushmore"     "captain"      "horizon"      "horizonExtra" "blueYellow"   "sambaNight"
# [19] "solarExtra"   "whitePurple"  "whiteBlue"    "comet"        "greenBlue"    "beach"
# [25] "coolwarm"     "fireworks"    "greyMagma"    "fireworks2"   "purpleOrange"

绘制颜色:

# plot colors
show_col(useMyCol('stallion',20))
# ironMan
show_col(useMyCol('ironMan',15))
# whitePurple
show_col(useMyCol('whitePurple',9))

4annoPoint

annoPoint 用来给图形添加点注释。

加载测试数据

测试数据:

df <- data.frame(x = 1:10,y = sample(1:10,10),
                 x1 = LETTERS[1:10])

绘图:

library (ggplot2)

p <-
  ggplot(df, aes(x,x1)) +
  geom_point(aes(size = x,color = x,alpha = x)) +
  theme_bw(base_size = 14) +
  scale_x_continuous(breaks = seq(1,10,1)) +
  # scale_y_continuous(breaks = seq(1,10,1)) +
  scale_color_gradient(low = 'grey50',high = '#339933',name = '') +
  coord_cartesian(clip = 'off') +
  theme(aspect.ratio = 1,
        plot.margin = margin(t = 1,r = 1,b = 1,l = 1,unit = 'cm'),
        axis.text.x = element_text(vjust = unit(-1.5,'native')),
        legend.position = 'left') +
  xlab('') + ylab('')

p

或者使用包里的内置数据:

data(p)

添加注释

简单绘图:

# default plot
annoPoint(object = p,
          annoPos = 'top',
          xPosition = c(1:10))

指定多个 yPosition:

# specify yPosition
annoPoint(object = p,
          annoPos = 'top',
          xPosition = c(1:10),
          yPosition = rep(c(2,4,2,6,4),each = 2))

添加到右边:

# add right
annoPoint(object = p,
          annoPos = 'right',
          yPosition = c(1:10))

添加到左边:

# left
annoPoint(object = p,
          annoPos = 'left',
          yPosition = c(1:10))

调整 xPosition:

# supply xPosition to ajust
annoPoint(object = p,
          annoPos = 'right',
          yPosition = c(1:10),
          xPosition = 0.3)

改变点大小和形状:

# change point size and shape
annoPoint(object = p,
          annoPos = 'top',
          xPosition = c(1:10),
          ptSize = 2,
          ptShape = 25)

添加多层注释::

# add multiple annotations
p1 <- annoPoint(object = p,
                annoPos = 'top',
                xPosition = c(1:10),
                ptSize = 2,
                ptShape = 25)

annoPoint(object = p1,
          annoPos = 'right',
          yPosition = c(1:10),
          ptSize = 2,
          ptShape = 23)

5annoRect

annoRect 用来给图添加矩形注释。

简单绘图:

# default plot
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10))

调整 yPosition:

# adjust yPosition
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11,11.5))

调整透明度:

# change color alpha
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11,11.5),
         alpha = 0.5)

设置矩形宽度:

# adjust rectWidth
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11,11.5),
         rectWidth = 0.9)

改变边缘颜色和填充颜色:

# change rect color and fill
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11,11.5),
         rectWidth = 0.9,
         pCol = rep('black',10),
         pFill = rep('grey80',10))

调整矩形线型和线粗:

# change rect lty and lwd
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11,11.5),
         rectWidth = 0.9,
         pCol = rep('black',10),
         pFill = rep('grey80',10),
         lty = 'dashed',
         lwd = 2)

添加部分注释:

# only add some annotations
annoRect(object = p,
         annoPos = 'top',
         xPosition = c(1,3,5,7,9),
         yPosition = c(11,11.5))

手动添加注释:

# supply your own coordinates to annotate
annoRect(object = p,
         annoPos = 'top',
         annoManual = T,
         xPosition = list(c(1,6),
                          c(5,10)),
         yPosition = c(11,11.5),
         pCol = rep('black',2),
         lty = 'solid',
         lwd = 2)

多个不同矩形的 yPosition:

# multiple yPosition
annoRect(object = p,
         annoPos = 'top',
         annoManual = T,
         xPosition = list(c(1,6),
                          c(5,10)),
         yPosition = list(c(11,11.5),
                          c(11.5,12)),
         pCol = rep('black',2),
         lty = 'solid',
         lwd = 2)

添加到右边:

# add to right
annoRect(object = p,
         annoPos = 'right',
         yPosition = c(1:10),
         xPosition = c(10.5,11),
         rectWidth = 0.8)

添加多个注释:

# add multiple
p1 <- annoRect(object = p,
               annoPos = 'top',
               xPosition = c(1:10),
               yPosition = c(11,11.5),
               rectWidth = 0.9,
               pCol = rep('black',10),
               pFill = rep('grey80',10),
               lty = 'dashed',
               lwd = 2)

# add second annotation
p2 <- annoRect(object = p1,
               annoPos = 'right',
               yPosition = c(1:10),
               xPosition = c(10.5,11),
               rectWidth = 0.8)

# add third annotation
annoRect(object = p2,
         annoPos = 'top',
         xPosition = c(1:10),
         yPosition = c(11.8,12.3),
         rectWidth = 0.8)

add GO plot with annotation

加载测试数据:

data(pgo)

pgo

绘图:

# Plot
pgo <-
ggplot(df1,aes(x = log10PValue,y = Term)) +
  geom_point(aes(size = log10PValue,
                 color = log10PValue)) +
  scale_color_gradient2(low = 'blue',mid = 'green',high = 'red',
                        name = '',midpoint = 2.1) +
  scale_y_discrete(position = 'right') +
  scale_x_continuous(breaks = seq(0,2.8,0.4),limits = c(0,2.8)) +
  theme_grey(base_size = 14) +
  theme(legend.position = 'left',
        plot.margin = margin(t = 1,r = 1,b = 1,l = 1,unit = 'cm'),
        aspect.ratio = 2.5
        ) +
  coord_cartesian(clip = 'off') +
  ylab('')

annotate

添加 15 个矩形:

# another example annotation GO terms
annoRect(object = pgo,
         annoPos = 'right',
         yPosition = c(1:15),
         pCol = rep('transparent',15),
         pFill = rep(c('#F5F0BB','#C4DFAA','#90C8AC'),each = 5),
         xPosition = c(3,10),
         rectWidth = 1)

手动添加 3 个矩形注释:

# annotate mannually
annoRect(object = pgo,
         annoPos = 'right',
         annoManual = T,
         xPosition = c(3,10),
         yPosition = list(c(0.5,5.5,10.5),
                          c(5.5,10.5,15.5)),
         pCol = rep('black',3),
         pFill = c('#F5F0BB','#C4DFAA','#90C8AC'))

6annoSegment

annoSegment 用来给图添加线段。

简单绘图:

# default plot
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10))

调整矩形宽度:

# adjust rectWidth
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8)

添加箭头:

library(ggplot2)

# add segments with arrow
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.9,
            lwd = 1,
            mArrow = arrow(ends = 'both',
                           length = unit(0.2,'cm'),
                           type = 'closed'))

设置颜色:

# change rect color and fill
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            pCol = useMyCol('horizon',10))

添加分支:

# add branch
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4)

调整分支方向:

# change branch direction
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4,
            branDirection = -1)

添加到底部:

# add to botomn
annoSegment(object = p,
            annoPos = 'botomn',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4)

添加到右边:

# add to right
annoSegment(object = p,
            annoPos = 'right',
            yPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4,
            branDirection = -1)

给分支添加箭头:

# add to botomn and make branch taged with arrow
annoSegment(object = p,
            annoPos = 'botomn',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 3,
            bArrow = arrow(length = unit(0.2,'cm'),
                           ends = 'last'))

添加文字:

# add text label
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4,
            branDirection = -1,
            addText = T,
            textLabel = paste0('anno',1:10,sep = ''),
            textRot = 0,
            textSize = 15)

调整文字:

# add text space from segments
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4,
            branDirection = -1,
            addText = T,
            textLabel = paste0('anno',1:10,sep = ''),
            textRot = 45,
            textSize = 15,
            textHVjust = 0.5)

改变颜色:

# change text color
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            addBranch = T,
            lwd = 4,
            branDirection = -1,
            addText = T,
            textLabel = paste0('anno',1:10,sep = ''),
            textRot = 45,
            textSize = 15,
            textHVjust = 0.5,
            textCol = rep('grey50',10))

调整 yPosition:

# change yPosition
annoSegment(object = p,
            annoPos = 'top',
            xPosition = c(1:10),
            segWidth = 0.8,
            pCol = useMyCol('horizon',10),
            yPosition = 5)

手动注释:

# supply your own coordinates to annotate
annoSegment(object = p,
            annoPos = 'top',
            annoManual = T,
            xPosition = list(c(1,6),
                             c(5,10)),
            yPosition = 11,
            pCol = c('green','orange'))

添加分支:

# add branch to plot
annoSegment(object = p,
            annoPos = 'top',
            annoManual = T,
            addBranch = T,
            lwd = 3,
            segWidth = 0.5,
            xPosition = list(c(1,6),
                             c(5,10)),
            yPosition = c(11,11),
            pCol = c('green','orange'),
            branDirection = -1)

添加到右边:

# add to right
annoSegment(object = p,
            annoPos = 'right',
            yPosition = c(1:10),
            segWidth = 0.8)

注释 GO 图形:

# annotate mannually
annoSegment(object = pgo,
            annoPos = 'left',
            annoManual = T,
            xPosition = rep(-0.15,3),
            yPosition = list(c(1,6,11),
                             c(5,10,15)),
            pCol = c('#F5F0BB','#C4DFAA','#90C8AC'),
            segWidth = 1)

7annoImage

annoImage 用来给图添加图形注释。

加载内置图形:

img1 <- system.file("extdata/animal-img/""1.jpg", package = "jjAnno")
img2 <- system.file("extdata/animal-img/""2.jpg", package = "jjAnno")
img3 <- system.file("extdata/animal-img/""3.jpg", package = "jjAnno")
img4 <- system.file("extdata/animal-img/""4.jpg", package = "jjAnno")
img5 <- system.file("extdata/animal-img/""5.jpg", package = "jjAnno")
img6 <- system.file("extdata/animal-img/""6.jpg", package = "jjAnno")
img7 <- system.file("extdata/animal-img/""7.jpg", package = "jjAnno")
img8 <- system.file("extdata/animal-img/""8.jpg", package = "jjAnno")
img9 <- system.file("extdata/animal-img/""9.jpg", package = "jjAnno")
img10 <- system.file("extdata/animal-img/""10.jpg", package = "jjAnno")

imgs <- c(img1,img2,img3,img4,img5,img6,img7,img8,img9,img10)

简单绘制:

# add legend
annoImage(object = p,
          annoPos = 'top',
          xPosition = c(1:10),
          images = imgs,
          yPosition = c(11,12))

调整宽度:

# change width
annoImage(object = p,
          annoPos = 'top',
          xPosition = c(1:10),
          images = imgs,
          yPosition = c(11,11.8),
          segWidth = 0.8)

Add to right:

# add to right
annoImage(object = p,
          annoPos = 'right',
          yPosition = c(1:10),
          images = imgs,
          xPosition = c(11,11.8),
          segWidth = 0.8)

添加到底部:

# add to botomn
annoImage(object = p,
          annoPos = 'botomn',
          xPosition = c(1:10),
          images = imgs,
          yPosition = c(-1.2,-0.4),
          segWidth = 0.8)

手动注释:

# annotate manually
annoImage(object = p,
          annoPos = 'right',
          yPosition = list(c(1:10),
                           c(1:10)),
          images = imgs,
          annoManual = T,
          xPosition = list(rep(c(11,11.8),each = 5),
                           rep(c(11.8,12.6),each = 5)),
          segWidth = 0.8)

8annoLegend

annoLegend 用来给图添加图例注释。

简单注释:

# add legend
annoLegend(object = p,
           labels = paste('legend ',1:5),
           pch = 21,
           col = 'black',
           fill = useMyCol('paired',5),
           textSize = 15)

调整位置:

# change pos
annoLegend(object = p,
           relPos = c(0.2,0.9),
           labels = paste('legend ',1:5),
           pch = 21,
           col = 'black',
           fill = useMyCol('paired',5),
           textSize = 15)

设置不同形状:

# multiple shapes
annoLegend(object = p,
           relPos = c(0.2,0.9),
           labels = paste('legend ',1:5),
           pch = 21:25,
           col = 'black',
           fill = useMyCol('paired',5),
           textSize = 15)

自定义坐标:

# define x and y position
annoLegend(object = p,
           xPosition = 12,
           yPosition = 5,
           labels = paste('legend ',1:5),
           pch = 21:25,
           col = 'black',
           fill = useMyCol('paired',5),
           textSize = 15)

9示例

加载内置数据:

data("pdot")

pdot

添加注释:

# add segment
P1 <- annoSegment(object = pdot,
                  annoPos = 'top',
                  xPosition = c(1:21),
                  yPosition = 8.8,
                  segWidth = 0.7,
                  pCol = c(useMyCol('stallion',20),'orange'))

# add rect1
P2 <- annoRect(object = P1,
               annoPos = 'left',
               annoManual = T,
               yPosition = list(c(0.5,4.5),
                                c(4.5,8.5)),
               xPosition = c(-3.5,0.3),
               pCol = rep('white',2),
               pFill = useMyCol('calm',2),
               alpha = 0.5)

# add rect2
P3 <- annoRect(object = P2,
               annoPos = 'left',
               annoManual = T,
               yPosition = list(c(2.5),
                                c(6.5)),
               xPosition = c(-3.5,16.5),
               pCol = 'black',
               pFill = 'transparent',
               lty = 'dashed',
               lwd = 3)

# add branch
P4 <- annoSegment(object = P3,
                  annoPos = 'top',
                  annoManual = T,
                  xPosition = list(c(1,3,4,7,9,11,12,15,17,19,20),
                                   c(2,3,6,8,10,11,14,16,18,19,21)),
                  yPosition = 9.2,
                  segWidth = 0.8,
                  pCol = rep('black',11),
                  addBranch = T,
                  branDirection = -1,
                  lwd = 3)

# add text
text <- c('Mesothelial','Extragonadal','Gonadal','Supporting lineage','Interstitial lineage',
          'Testis fate','Early supporting','Granulosa','Sertoli','sPAX8','Mesenchymal')

# add segment branch and text
annoSegment(object = P4,
            annoPos = 'top',
            annoManual = T,
            xPosition = list(c(1,3,4,7,9,11,12,15,17,19,20),
                             c(2,3,6,8,10,11,14,16,18,19,21)),
            yPosition = 9.2,
            segWidth = 0.8,
            pCol = rep('black',11),
            addBranch = T,
            branDirection = -1,
            lwd = 3,
            addText = T,
            textLabel = text,
            textCol = c(rep('black',5),'blue',rep('black',5)),
            textRot = 45,
            hjust = 0)

10结尾

更多参数见:

?useMyCol
?annoPoint
?annoRect
?annoSegment
?annoImage
?annoLegend

有其它建议或者疑问请在 github 提问。




  老俊俊生信交流群 (微信交流群需收取20元入群费用(防止骗子和便于管理))



老俊俊微信:


知识星球:



今天的分享就到这里了,敬请期待下一篇!

最后欢迎大家分享转发,您的点赞是对我的鼓励肯定

如果觉得对您帮助很大,赏杯快乐水喝喝吧!




  





grid 给图添加图片

富集图形添加注释美化

R 中的控制结构

ggpolar 绘制极坐标图形

transPlotR 优雅的绘制基因转录本结构

ggarchery 添加个性化箭头

scRNAtoolVis 0.0.3 版本更新

customize 你的 GSEA 图

GseaVis 优雅的可视化 GSEA 富集结果

GSEA 图是如何画出来的? (源码解析)

◀...

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存