听说你想插入 GO 和 KEGG 图形注释?

1引言

昨天推文 ClusterGVis 添加自定义图形注释 末尾演示了一下插入 GO 条形图。有粉丝问可不可以再加个 KEGG 的图, 其实你用拼图就好了,返回拼图的 list,然后再插入为注释

2示例

首先是 GO 和 KEGG 富集:

# enrich for clusters
enrich <- enrichCluster(object = st.data,
                        OrgDb = org.Hs.eg.db,
                        type = "BP",
                        organism = "hsa",
                        pvalueCutoff = 0.5,
                        topn = 5,
                        seed = 5201314)

# check
head(enrich,3)
#            group                                         Description       pvalue    ratio
# GO:0002573    C1                   myeloid leukocyte differentiation 0.0003941646 66.66667
# GO:0050870    C1            positive regulation of T cell activation 0.0005222343 66.66667
# GO:1903039    C1 positive regulation of leukocyte cell-cell adhesion 0.0006265618 66.66667

enrich.KEGG <- enrichCluster(object = st.data,
                        OrgDb = org.Hs.eg.db,
                        type = "KEGG",
                        organism = "hsa",
                        pvalueCutoff = 0.9,
                        topn = 5,
                        seed = 5201314)

# check
head(enrich.KEGG,3)
#          group           Description     pvalue    ratio
# hsa00640    C1 Propanoate metabolism 0.01115231 33.33333
# hsa05216    C1        Thyroid cancer 0.01288734 33.33333
# hsa00620    C1   Pyruvate metabolism 0.01635129 33.33333

然后是分别画图再拼图:

palette = c("Grays","Light Grays","Blues2","Blues3","Purples2","Purples3","Reds2","Reds3","Greens2")

# loop
lapply(seq_along(unique(enrich$group)), function(x){
  # go plot
  tmp <- enrich |> dplyr::filter(group == unique(enrich$group)[x]) |>
    dplyr::arrange(desc(pvalue))

  tmp$Description <- factor(tmp$Description,levels = tmp$Description)

  # plot
  p <-
    ggplot(tmp) +
    geom_col(aes(x = -log10(pvalue),y = Description,fill = -log10(pvalue)),
             width = 0.75) +
    geom_line(aes(x = log10(ratio),y = as.numeric(Description)),color = "grey50") +
    geom_point(aes(x = log10(ratio),y = Description),size = 3,color = "orange") +
    theme_bw() +
    scale_y_discrete(position = "right",
                     labels = function(x) stringr::str_wrap(x, width = 40)) +
    scale_x_continuous(sec.axis = sec_axis(~.,name = "log10(ratio)")) +
    colorspace::scale_fill_binned_sequential(palette = palette[x]) +
    ylab("")

  # plot kegg
  tmp.kg <- enrich.KEGG |> dplyr::filter(group == unique(enrich.KEGG$group)[x]) |>
    dplyr::arrange(desc(pvalue))

  tmp.kg$Description <- factor(tmp.kg$Description,levels = tmp.kg$Description)

  # plot
  pk <-
    ggplot(tmp.kg) +
    geom_segment(aes(x = 0,xend = -log10(pvalue),y = Description,yend = Description),
                 lty = "dashed",linewidth = 0.75) +
    geom_point(aes(x = -log10(pvalue),y = Description,color = -log10(pvalue)),size = 5) +
    theme_bw() +
    scale_y_discrete(position = "right",
                     labels = function(x) stringr::str_wrap(x, width = 40)) +
    colorspace::scale_color_binned_sequential(palette = palette[x]) +
    ylab("") + xlab("-log10(pvalue)")

  # combine
  cb <- cowplot::plot_grid(plotlist = list(p,pk))

  return(cb)
}) -> gglist

# assign names
names(gglist) <- paste("C",1:9,sep = "")

最后插入到主图:

# insert bar plot
pdf('sc_ggplot_gokegg.pdf',height = 20,width = 22,onefile = F)
visCluster(object = st.data,
           plot.type = "both",
           line.side = "left",
           column_names_rot = 45,
           markGenes = pbmc.markers$gene,
           cluster.order = c(1:9),
           ggplot.panel.arg = c(5,0.5,32,"grey90",NA),
           gglist = gglist)
dev.off()
图片

看着问题不大,还行。

3结尾

路漫漫其修远兮,吾将上下而求索。


欢迎加入生信交流群。加我微信我也拉你进 微信群聊 老俊俊生信交流群 (微信交流群需收取 20 元入群费用,一旦交费,拒不退还!(防止骗子和便于管理)) 。QQ 群可免费加入, 记得进群按格式修改备注哦。

声明:文中观点不代表本站立场。本文传送门:https://eyangzhen.com/242436.html

联系我们
联系我们
分享本页
返回顶部