引言
jjAnno 是我三年前写的一个简单的工具,来给 ggplot 周围添加不同的注释图形。本质上是使用 annotation_custom 来实现的。最近 ggplot 4.0.0 版本更新了,可能再使用 jjAnno 会报错。比如 生信技能树 的这篇 Nature Genetics 杂志特别版单细胞 marker 基因气泡图,我也是在下面评论了一下后续有时间更新一下。这次对代码进行了更新,使用 ggplot 4.0.0 应该是没有问题的。
安装
install.packages(“devtools”)
devtools::install_github(“junjunlab/jjAnno”)
示例
画个基础图形:
library(jjAnno)
library(scales)
library(ggplot2)
data(“dot_data”)
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
pdot <-
ggplot(dot_data,aes(x = gene,y = cell)) +
geom_point(aes(fill = mean.expression,size = percentage),
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,13)) +
theme(panel.grid = element_blank(),
axis.text = element_text(color = ‘black’),
aspect.ratio = 0.5,
plot.margin = margin(t = 1,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’)
pdot
添加注释:
add segment
annoSegment(object = pdot,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = 8.8,
segWidth = 0.5)
添加链接框:
add branch
annoSegment(object = pdot,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = 8.8,
segWidth = 0.5,
addBranch = T,
lwd = 2,
branDirection = -1,
pCol = rep(‘black’,11))
文字标签:
add text
annoSegment(object = pdot,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = 8.8,
segWidth = 0.5,
addText = T,
textRot = 45,
hjust = 0)
细胞分组:
mapping cell group
annoSegment(object = pdot,
annoPos = ‘left’,
aesGroup = T,
aesGroName = ‘cellGroup’,
xPosition = -3.5,
segWidth = 0.5,
addText = T,
textRot = 90,
textHVjust = -0.5,
textSize = 14)
添加矩形注释:
mapping by group
annoRect(object = pdot,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = c(9,9.5),
rectWidth = 0.8)
add to botomn
annoRect(object = pdot,
annoPos = ‘botomn’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = c(-2,0.25),
rectWidth = 0.8,
alpha = 0.35)
mapping cell group
annoRect(object = pdot,
annoPos = ‘left’,
aesGroup = T,
aesGroName = ‘cellGroup’,
xPosition = c(-3.5,0.25),
alpha = 0.3,
rectWidth = 0.8,
addText = T,
textRot = 90,
textSize = 14,
textCol = rep(‘black’,3),
textHVjust = -2.3)
添加倾斜矩形:
add group label
annoRect(object = pdot_test,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
yPosition = c(8.6,10.4),
rotateRect = T,
alpha = 0.5,
rectWidth = 0.9,
rectAngle = 50,
normRectShift = 0.15,
addText = T,
textHVjust = 1,
textRot = 60,
hjust = 0,
textShift = 0.5)
点注释:
add text label
annoPoint2(object = pdot,
annoPos = ‘top’,
aesGroup = T,
aesGroName = ‘class’,
ptSize = 2,
yPosition = 9,
addText = T,
textRot = 45,
hjust = 0,
textHVjust = 0.4)
还有一些其他好玩的,大家可以自己去看看说明文档。
结尾
路漫漫其修远兮,吾将上下而求索。
欢迎加入生信交流群。加我微信我也拉你进 微信群聊老俊俊生信交流群(微信交流群需收取 20 元入群费用,一旦交费,拒不退还!(防止骗子和便于管理)) 。
声明:来自老俊俊的生信笔记,仅代表创作者观点。链接:https://eyangzhen.com/3984.html