前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言绘图-Dotplot气泡图

R语言绘图-Dotplot气泡图

原创
作者头像
十维农场主
修改2023-05-18 08:59:54
1.3K0
修改2023-05-18 08:59:54
举报

富集分析气泡图dotplot, 本脚本只需要一个表格数据(如果是clusterProfiler包输出的富集分析结果可直接使用),也可以是其他软件工具的结果,按需改成本脚本所需格式即可。

  • 结果展示:
  • 数据格式: (列名必须设置为这四个!, 本展示数据第一列为行名,可以不需要这个行名)

Description

pvalue

Count

GeneRatio

GO:0023061

signal release

5.246744e-06

29

0.06

GO:0050673

epithelial cell proliferation

6.129368e-06

27

0.06

GO:0045444

fat cell differentiation

7.622508e-06

18

0.04


  1. 可将数据按需排序好,图的顺序就是表格的顺序,默认展示前15个通路。
  2. 代码只有# if name == "main":后需要按需修改。
  3. 读取表格、选取表格指定行数、调用函数画图处代码,请按实际情况更改。
  4. 请注意示例数据的第四列,我们需要将他转化为数字,(/前)除(以/后),如果已经是数字请删除函数内部前二行代码。
代码语言:text
复制
# Dotplot 气泡图
library(tidyverse)
DEG_Dotplot <- function(df, title="") {
  # """
  # data格式如下(需有Description,pvalue,Count,GeneRatio列)
  # !!! 其中GeneRatio必须为数值型,如下图所示,可能需要准换
  # GeneRatio表示点颜色,Count表点的大小,横坐标为p值
  # df表的顺序是什么,画图的顺序就是什么
  #                             Description       pvalue Count GeneRatio
  #GO:0023061                signal release 5.246744e-06    29      0.06
  #GO:0050673 epithelial cell proliferation 6.129368e-06    27      0.06
  #GO:0045444      fat cell differentiation 7.622508e-06    18      0.04
  # resultdir = './result_stringtie/p005fc15'
  # filemark = 'GO_BP_top20_common'
  # 此函数可能需要使用scale_x_continuous调整x轴刻度
  # """
  # 如果已经时数字请删除这二行代码
  df$GeneRatio <- as.numeric(str_split(df$GeneRatio, pattern = '/', simplify = T)[,1])/
                  as.numeric(str_split(df$GeneRatio, pattern = '/', simplify = T)[,2])
  dotplot <- ggplot(cbind(df, Order = nrow(df):1)) +
    geom_point(mapping = aes(x = -log10(pvalue), y = Order, 
                             size = Count, fill = GeneRatio),
               shape = 21) + 
    scale_fill_gradientn(colours = c("grey", "gold", "red")) + #自定义配色
    scale_y_continuous(position = "left", 
                       breaks = 1:nrow(df), 
                       labels = Hmisc::capitalize(rev(df$Description))) +
    #scale_x_continuous(breaks = c(3, 4,5,6),
    #                   #breaks = seq(0, xmax+5, 5),
    #                   limits = c(3,6),
    #                   expand = expansion(mult = c(.05, .05))) + #两边留空
    labs(x = "-Log10(PValue)", y = NULL) +
    guides(size = guide_legend(title = "Gene count"),
           fill = guide_colorbar(title = "GeneRatio")) +
    ggtitle(title) +
    theme_bw() +
    theme(panel.grid =element_blank(),
          panel.grid.major = element_line(color = "gray", linetype = "dashed"),
          panel.border = element_rect(color = "black", linewidth = 1),
          axis.text = element_text(size = 13, family = 'Times'),
          title = element_text(size = 13, family = 'Times')) #去除网格线
  dotplot %>% ggplotGrob()#  %>% cowplot::plot_grid()
  # fname=paste0(resultdir, '/', filemark,'.pdf')
  # ggsave(fname, width = 8, height = 10)
  return(dotplot)
}


# if __name__ == "__main__":
keggdf <- xlsx::read.xlsx("kegg.1.xlsx", sheetIndex = 1)
range(round(-log10(keggdf$pvalue)))
#
p_df <- keggdf[1:15,]
dotplot = DEG_Dotplot(p_df, title="xx")
ggsave(file = "Dotplot_KEGG_top10.pdf", width = 8, height = 7)
#gh <- ggplotGrob(dotplot)
#gd <- ggplotGrob(dotplotk)
#cowplot::plot_grid(gh, gd, rel_widths = c(1.2, 1))

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档