Seurat::DotPlot绘图美化-facet方法添加X轴注释标签


###获取Seurat气泡图的绘图数据

data.usage <- DotPlot(sc,features = VariableFeatures(sc)[1:10])$data 
> data.usage
                     avg.exp      pct.exp features.plot id avg.exp.scaled
Htr2c           6.387599e-02   0.88218224         Htr2c  0   -0.241662933
St6gal1         1.504964e-02   0.21474173       St6gal1  0   -0.523974858
AC121788.1      4.451450e-02   0.64422519    AC121788.1  0   -0.310429629
Gm28928         4.653460e-03   0.05803831       Gm28928  0   -0.215970623

####创建x轴分类标签注释

data.anno <- data.frame(
    features.plot = unique(data.usage$features.plot),
    label = c("Cell.A","Cell.A","Cell.B","Cell.C","Cell.C","Cell.D","Cell.E","Cell.F","Cell.G","Cell.H")
)
> data.anno
   features.plot  label
1          Htr2c Cell.A
2        St6gal1 Cell.A
3     AC121788.1 Cell.B
4        Gm28928 Cell.C

###将注释添加到data.usage方便绘图调用

df.plot <- plyr::join(data.usage,data.anno)

###根据需求重排y轴绘图标签顺序

df.plot$id <- factor(df.plot$id,levels = sort(levels(df.plot$id),decreasing = T) )
> df.plot$id
  [1] 0  0  0  0...
  Levels: 9 8 7 6 5 4 3 21 20 2 19 18 17 16 15 14 13 12 11 10 1 0 #自定义的绘图标签顺序

###重绘气泡图并基于facet分面方法为x轴添加注释标签

p <- ggplot(df.plot,aes(x=features.plot,y =  as.numeric(id),size = pct.exp, color = avg.exp.scaled))+
    geom_point() + 
    scale_size("% detected", range = c(0,10)) + #调整绘图点的相对大小
    scale_color_gradientn(colours = viridis::viridis(20),
                          guide = guide_colorbar(ticks.colour = "black",frame.colour = "black"),
                          name = "Average\nexpression") +
    cowplot::theme_cowplot() + 
    ylab("") + xlab("Markers") + theme_bw() +
    scale_y_continuous(breaks = 1:length(levels(df.plot$id)),labels = levels(df.plot$id),sec.axis = dup_axis())+ #复制 y轴 代替边框效果
    facet_grid(~label, scales="free_x",space = "free")+theme_classic() +
    theme(
        axis.text.x = element_text(size=12, angle=0, hjust=0.5, color="black",face="bold"),#x轴标签样式
        axis.text.y = element_text(size=12, color="skyblue",face="bold"),
        axis.title.x = element_text(size=14,colour = 'black',vjust = -0.8,hjust = 0.5),#坐标轴标题
        
        axis.ticks.y = element_blank(),#坐标轴刻度
        axis.text.y.right = element_blank(),#坐标轴标签隐藏
        axis.ticks.x = element_blank(),
        axis.line = element_line(colour = 'grey30',size = 0.2), #坐标轴轴线样式
        
        panel.spacing=unit(0, "mm"), #分面图图块间距
        strip.text.x = element_text(size=15, face="bold",color = "#FFFFFF",
                                    vjust = 0.5,margin = margin(b = 3,t=3)),#分面标签样式
        strip.background = element_rect(colour="grey30", fill="grey60",size = 1)
    )


###分面标签上色

g <- ggplot_gtable(ggplot_build(p))
strips <- which(grepl('strip-', g$layout$name))
cols <- BuenColors::jdb_color_maps[1:length(unique(df.plot$label))] %>% as.vector() 

for (i in seq_along(strips)) {
    k <- which(grepl('rect', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
    l <- which(grepl('titleGrob', g$grobs[[strips[i]]]$grobs[[1]]$childrenOrder))
    g$grobs[[strips[i]]]$grobs[[1]]$children[[k]]$gp$fill <- cols[i]
}
plot(g)

最后再分享一个添加聚类树的绘图方法,在我的GitHub

r - ggplot2 outside panel border when using facet - Stack Overflow

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值