查看原文
其他

ggplot 随心所欲的添加注释

The following article is from 老俊俊的生信笔记 Author JunJunLab

1引言

本文思路来自于 小明的数据分析笔记本 推文跟着Nature Plants学作图:R语言ggplot2画分组折线图并对坐标轴添加一些额外注释。在很多文章里也会看见各种复杂的图形。

本文借鉴以上思路绘制几个图形分享给大家,可以借鉴参考。

2连续变量

library("ggplot2")
library("grid")
library("ggsci")

# load data
data("mtcars")

# plot
ggplot(mtcars,aes(x = mpg,y = disp)) +
  geom_line(size = 1,aes(color = mpg)) +
  scale_color_gradient2(low = 'red',mid = '#CCFF99',high = '#99CCFF',
                        name = '',midpoint = 22.5) +
  # botomn rect
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#99CCCC')),
                    xmin = 10,xmax = 15,
                    ymin = -25,ymax = 0) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#FFCC99')),
                    xmin = 15,xmax = 25,
                    ymin = -25,ymax = 0) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#FF6666')),
                    xmin = 25,xmax = 35,
                    ymin = -25,ymax = 0) +
  # point
  annotation_custom(grob = circleGrob(gp = gpar(fill = '#33CC33',col = 'white',
                                                lty = 'solid',lwd = 1.5)),
                    xmin = 11.5,xmax = 13.5,
                    ymin = -25,ymax = 0) +
  annotation_custom(grob = circleGrob(gp = gpar(fill = '#99CCFF',col = 'white',
                                                lty = 'solid',lwd = 1.5)),
                    xmin = 19,xmax = 21,
                    ymin = -25,ymax = 0) +
  annotation_custom(grob = circleGrob(gp = gpar(fill = '#FFFF99',col = 'white',
                                                lty = 'solid',lwd = 1.5)),
                    xmin = 29,xmax = 31,
                    ymin = -25,ymax = 0) +
  # right
  annotation_custom(grob = segmentsGrob(gp = gpar(col = "#CC0033",
                                                  lwd = 2,
                                                  lineend = "square"),
                                        arrow = arrow(angle=20,
                                                      length = unit(2,'mm'))),
                    xmin = 36,xmax = 36,
                    ymin = 260,ymax = max(mtcars$disp))+
  annotation_custom(grob = segmentsGrob(gp = gpar(col = "grey50",
                                                  lwd = 2,
                                                  lineend = "square"),
                                        arrow = arrow(angle=20,ends = 'first',
                                                      length = unit(2,'mm'))),
                    xmin = 36,xmax = 36,
                    ymin = min(mtcars$disp),ymax = 240) +
  # left
  annotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,
                                              fill = '#0099CC')),
                    xmin = 5.5,xmax = 6.5,
                    ymin = min(mtcars$disp),ymax = 250) +
  annotation_custom(grob = rectGrob(gp = gpar(col = 'black',alpha = 0.7,
                                              fill = '#009933')),
                    xmin = 5.5,xmax = 6.5,
                    ymin = 250,ymax = max(mtcars$disp)) +
  theme_bw(base_size = 16) +
  coord_cartesian(clip = "off") +
  theme(aspect.ratio = 0.6,
        plot.margin = margin(0,0,0,1,unit = 'cm'),
        legend.position = 'right') +
  xlab('') + ylab('') +
  guides(color = guide_colorbar(barheight = 14))

3分类变量

# test data
col_df <- data.frame(gene = c('MYC','SOX2','NANOG','ACTB','GAPDH'),
                     exp = rev(seq(0.6,1,0.1)))

col_df
#    gene exp
# 1   MYC 1.0
# 2  SOX2 0.9
# 3 NANOG 0.8
# 4  ACTB 0.7
# 5 GAPDH 0.6

pal_lancet()(8)
# [1] "#00468BFF" "#ED0000FF" "#42B540FF" "#0099B4FF" "#925E9FFF" "#FDAF91FF" "#AD002AFF"
# [8] "#ADB6B6FF"

# order
col_df$gene <- factor(col_df$gene,levels = col_df$gene)

# plot
ggplot(col_df,aes(x = gene,y = exp,fill = gene)) +
  geom_col(show.legend = F,fill = '#FF9900',width = 0.6) +
  # botomn rect
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#00468BFF')),
                    xmin = 0.7,xmax = 1.3,
                    ymin = -0.15,ymax = -0.1) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#ED0000FF')),
                    xmin = 1.7,xmax = 2.3,
                    ymin = -0.15,ymax = -0.1) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#42B540FF')),
                    xmin = 2.7,xmax = 3.3,
                    ymin = -0.15,ymax = -0.1) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#0099B4FF')),
                    xmin = 3.7,xmax = 4.3,
                    ymin = -0.15,ymax = -0.1) +
  annotation_custom(grob = rectGrob(gp = gpar(col = NA,
                                              fill = '#925E9FFF')),
                    xmin = 4.7,xmax = 5.3,
                    ymin = -0.15,ymax = -0.1) +
  scale_fill_lancet(name = '') +
  scale_y_continuous(expand = c(0,0)) +
  theme_classic(base_size = 16) +
  xlab('') +
  theme(aspect.ratio = 1,
        axis.text.x = element_text(color = 'black')) +
  coord_cartesian(clip = "off")

4结尾

确定好坐标就可以添加自己想要绘制的图形了。




  





Seurat 官网单细胞教程一 (数据整合)

NC 文章单细胞部分图复现

Seurat 官网单细胞教程一 (基础教程)

左下角自定义箭头坐标轴 (批量添加和美化)

单细胞绘图数据提取个性化绘图

UMAP/t-SNE 左下角自定义箭头坐标轴

优雅的可视化细胞群 Marker 基因

GENES & DEVELOPMENT 单细胞结果复现

加速你的单细胞数据分析

Cell 教我学画图之累积分布曲线

◀...

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

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