使用 jjAnno 对分面添加注释
风萧萧兮易水寒
1引言
其实分面和添加注释有着异曲同工之妙,都是 对数据进行分组添加说明。那么是不是添加了分类注释,分面就可以抛弃了了呢? 其实不然,分面当然还有其它作用,例如把不同组的数据分开展示,同时还要 展示组内数据之间的相对差异, 去除不同组别之间极值对数据的展示影响, 这时我们可以设置 scales = 'free_x/y' 参数达到目的。
新功能:
支持对分面图形添加注释。
支持的函数:
annoPoint2 annoSegment annoRect annoTriangle
Note:
如果你使用 facet_grid 同时对 X 和 Y 轴都进行了分面,那么再添加注释是没有什么意义了,反而显得冗余,所以这里 只支持 X 或 Y 的分面图形添加注释。
此外需要注意的是,绘图的时候你需要把映射的变量放在 其它对应的 geom 图层里, 而不是 ggplot 函数里面,否则会报错,例如
ggplot(dot_data)
而不是ggplot(dot_data,aes(x = gene,y = cell))
。
安装:
如果你想获得新的功能,重新安装即可:
# install.packages("devtools")
devtools::install_github("junjunlab/jjAnno")
library(jjAnno)
参考手册地址:
https://junjunlab.github.io/jjAnno-manual/facet-introduction.html
2basic plot
library(jjAnno)
library(tidyverse)
library(ggplot2)
# load data
dot_data <- read.delim('gene-dot.txt',header = T) %>%
arrange(class)
# add cell group
dot_data$cellGroup <- case_when(
dot_data$cell %in% c("1b CoelEpi GATA4", "2a Early somatic", "2b ESGC male") ~ "cell type1",
dot_data$cell %in% c("2b ESGC female", "2c PreGC-I", "2d Sertoil") ~ "cell type2",
dot_data$cell %in% c("3a Early sPAX8", "3b Gi") ~ "cell type3"
)
# order
dot_data$gene <- factor(dot_data$gene,levels = unique(dot_data$gene))
# plot
pdotfc <-
# ggplot(dot_data,aes(x = gene,y = cell)) +
ggplot(dot_data) +
geom_point(aes(fill = mean.expression,size = percentage,
x = gene,y = cell
),
color = 'black',
shape = 21) +
theme_bw(base_size = 14) +
xlab('') + ylab('') +
scale_fill_gradient2(low = 'white',mid = '#EB1D36',high = '#990000',
midpoint = 0.5,
name = 'Mean expression') +
scale_size(range = c(1,8)) +
theme(panel.grid = element_blank(),
axis.text = element_text(color = 'black'),
# aspect.ratio = 0.5,
plot.margin = margin(t = 2,r = 1,b = 1,l = 1,unit = 'cm'),
axis.text.x = element_text(angle = 90,hjust = 1,vjust = 0.5,
face = 'italic')) +
coord_cartesian(clip = 'off')
pdotfc
Or load test data directly:
data(pdotfc)
3annoPoint2
facet by cellGroup
Let's see a facet example:
# facet by cellGroup
pfacet <-
pdotfc +
facet_wrap(~cellGroup,
ncol = 1,scales = 'free_y',
strip.position = 'right')
pfacet
Trying to add point annotation:
# add to top
annoPoint2(object = pfacet,
annoPos = 'top',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type1',
ptSize = 2,
yPosition = 4.2)
It seems that the annoPoint2 function works well! Here remember that you should supply the
X
andY
mapping variables with aes_x and aes_y. Also you need to point out which variable(aesGroName)
and sub-facet group name(myFacetGrou)
should be used to add an annotation.
If you want to add to the bottom, just change the myFacetGrou:
# change cellGroup and add to botomn
annoPoint2(object = pfacet,
annoPos = 'botomn',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type3',
yPosition = -0.6,
ptSize = 2)
facet by class
Facet by another group:
# facet by class
pfacet1 <-
pdotfc +
facet_wrap(~class,
nrow = 1,scales = 'free_x') +
theme(axis.text.y = element_text(margin = margin(r = 1.2,unit = 'cm')))
pfacet1
Adding to left:
# add to left
annoPoint2(object = pfacet1,
annoPos = 'left',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'cellGroup',
myFacetGrou = 'Early supporting',
ptSize = 2,
xPosition = -0.5,
pCol = rep(useMyCol('paired',3),c(3,3,2)))
4annoSegment
Let's see examples for annoSegment.
facet by cellGroup
# facet by cellGroup
pfacet <-
pdotfc +
facet_wrap(~cellGroup,
ncol = 1,scales = 'free_y',
strip.position = 'right') +
theme(plot.margin = margin(t = 5,unit = 'cm'))
# add segment to top
annoSegment(object = pfacet,
annoPos = 'top',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type1',
yPosition = 4.2,
segWidth = 0.8,
lwd = 4,
mArrow = arrow(ends = 'both',length = unit(0.25,'cm')),
addText = T,
textRot = 45,
textHVjust = 0.5,
hjust = 0)
You can also add to the middle facet group:
# add segment to "cell type2" top
annoSegment(object = pfacet,
annoPos = 'top',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type2',
yPosition = 4,
segWidth = 0.8) +
theme(panel.spacing.y = unit(1,'cm'))
facet by class
# facet by class
pfacet1 <-
pdotfc +
facet_wrap(~class,
nrow = 1,scales = 'free_x') +
theme(axis.text.y = element_text(margin = margin(r = 1.2,unit = 'cm')))
# add segment to left
annoSegment(object = pfacet1,
annoPos = 'left',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'cellGroup',
myFacetGrou = 'Early supporting',
xPosition = -0.5,
segWidth = 0.8,
lwd = 4,
addBranch = T,
branRelSegLen = 0.8,
bArrow = arrow(length = unit(0.3,'cm')))
5annoRect
Let's see examples for annoRect.
facet by cellGroup
# facet by cellGroup
pfacet <-
pdotfc +
facet_wrap(~cellGroup,
ncol = 1,scales = 'free_y',
strip.position = 'right') +
theme(plot.margin = margin(t = 5,b = 4,unit = 'cm'),
axis.text.x = element_text(angle = 60,hjust = 1,vjust = 1))
# add rect to top
annoRect(object = pfacet,
annoPos = 'top',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type1',
yPosition = c(4,4.6),
rectWidth = 0.8,
pCol = rep('black',11),
lwd = 2,
addText = T,
textRot = 45,
textHVjust = 0.6,
hjust = 0)
Adding to bottom:
# add rect to botomn
annoRect(object = pfacet,
annoPos = 'botomn',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type3',
yPosition = c(-1.2,0.4),
rectWidth = 0.9,
alpha = 0.5)
Roate the rect:
# add rotated rect to botomn
annoRect(object = pfacet,
annoPos = 'botomn',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'class',
myFacetGrou = 'cell type3',
yPosition = c(-1.5,0.3),
rectWidth = 0.9,
alpha = 0.5,
rotateRect = T,
rectAngle = 50,
rotatedRectShift = 0.75)
facet by class
# facet by class
pfacet1 <-
pdotfc +
facet_wrap(~class,
nrow = 1,scales = 'free_x') +
theme(axis.text.y = element_text(margin = margin(l = 1,unit = 'cm')))
# add to left
annoRect(object = pfacet1,
annoPos = 'left',
aesGroup = T,
aes_x = 'gene',
aes_y = 'cell',
aesGroName = 'cellGroup',
myFacetGrou = 'Early supporting',
xPosition = c(-6,0.2),
rectWidth = 0.9,
alpha = 0.3)
6annoTriangle
Let's see examples for annoRect.
facet by cellGroup
# facet by cellGroup
pfacet <-
pdotfc +
facet_wrap(~cellGroup,
ncol = 1,scales = 'free_y',
strip.position = 'right')
# add rect to top
annoTriangle(object = pfacet,
annoPos = 'top',
aes_x = 'gene',
aes_y = 'cell',
myFacetGrou = 'cell type1',
xPosition = c(1,21),
yPosition = c(4,4.6),
fillCol = c('blue','white','red'))
7结尾
更多参数说明见每个函数的使用文档。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群
(微信交流群需收取20元入群费用(防止骗子和便于管理)
)。
老俊俊微信:
知识星球:
所以今天你学习了吗?
今天的分享就到这里了,敬请期待下一篇!
最后欢迎大家分享转发,您的点赞是对我的鼓励和肯定!
如果觉得对您帮助很大,赏杯快乐水喝喝吧!
往期回顾
◀...