gseaplot3修改一下clusterProfiler默认绘图函数

这篇具有很好参考价值的文章主要介绍了gseaplot3修改一下clusterProfiler默认绘图函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

直接使用clusterProfiler::gseaplot2绘图会出现下边的结果,导致四周显示不全,线的粗细也没办法调整,因为返回的是一个aplot包中的gglist对象,没太多研究。

p1 <- clusterProfiler::gseaplot2(gsea_result, gsea_result$ID, pvalue_table = T, base_size = 18, ES_geom='line')

gseaplot3修改一下clusterProfiler默认绘图函数,数据可视化

自定义gseaplot3函数增加了size参数调整线的粗细,也调整了margin四周边距,可以在下边gseaplot3函数的基础上继续调整,如果需要的话。

p2 <- gseaplot3(gsea_result, gsea_result$ID, pvalue_table = T, base_size = 18, ES_geom='line', size=1.8)

gseaplot3修改一下clusterProfiler默认绘图函数,数据可视化

gseaplot3 <- function (x, geneSetID, title = "", color = "green", base_size = 15, size=1.8,
    rel_heights = c(1.5, 0.5, 1), subplots = 1:3, pvalue_table = FALSE, 
    ES_geom = "line") 
{
    library(grid)
    library(DOSE)
    gseaScores <- getFromNamespace("gseaScores", "DOSE")
    ES_geom <- match.arg(ES_geom, c("line", "dot"))
    geneList <- position <- NULL
    if (length(geneSetID) == 1) {
        gsdata <- gsInfo(x, geneSetID)
    }
    else {
        gsdata <- do.call(rbind, lapply(geneSetID, gsInfo, object = x))
    }
    p <- ggplot(gsdata, aes_(x = ~x)) + xlab(NULL) + theme_classic(base_size) + 
        theme(panel.grid.major = element_line(colour = "grey92"), 
            panel.grid.minor = element_line(colour = "grey92"), 
            panel.grid.major.y = element_blank(), panel.grid.minor.y = element_blank()) + 
        scale_x_continuous(expand = c(0, 0))
    if (ES_geom == "line") {
        es_layer <- geom_line(aes_(y = ~runningScore, color = ~Description), 
            size = size)
    }
    else {
        es_layer <- geom_point(aes_(y = ~runningScore, color = ~Description), 
            size = size, data = subset(gsdata, position == 1))
    }
    p.res <- p + es_layer + theme(legend.position = c(0.8, 0.8), 
        legend.title = element_blank(), legend.background = element_rect(fill = "transparent"))
    p.res <- p.res + ylab("Running Enrichment Score") + theme(axis.text.x = element_blank(), 
        axis.ticks.x = element_blank(), axis.line.x = element_blank(), 
        plot.margin = margin(t = 0.2, r = 0.2, b = 0, l = 0.2, 
            unit = "cm"))
    i <- 0
    for (term in unique(gsdata$Description)) {
        idx <- which(gsdata$ymin != 0 & gsdata$Description == 
            term)
        gsdata[idx, "ymin"] <- i
        gsdata[idx, "ymax"] <- i + 1
        i <- i + 1
    }
    p2 <- ggplot(gsdata, aes_(x = ~x)) + geom_linerange(aes_(ymin = ~ymin, 
        ymax = ~ymax, color = ~Description)) + xlab(NULL) + ylab(NULL) + 
        theme_classic(base_size) + theme(legend.position = "none", 
        plot.margin = margin(t = -0.1, b = 0, unit = "cm"), axis.ticks = element_blank(), 
        axis.text = element_blank(), axis.line.x = element_blank()) + 
        scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 
        0))
    if (length(geneSetID) == 1) {
        v <- seq(1, sum(gsdata$position), length.out = 9)
        inv <- findInterval(rev(cumsum(gsdata$position)), v)
        if (min(inv) == 0) 
            inv <- inv + 1
        col <- c(rev(brewer.pal(5, "Blues")), brewer.pal(5, "Reds"))
        ymin <- min(p2$data$ymin)
        yy <- max(p2$data$ymax - p2$data$ymin) * 0.3
        xmin <- which(!duplicated(inv))
        xmax <- xmin + as.numeric(table(inv)[as.character(unique(inv))])
        d <- data.frame(ymin = ymin, ymax = yy, xmin = xmin, 
            xmax = xmax, col = col[unique(inv)])
        p2 <- p2 + geom_rect(aes_(xmin = ~xmin, xmax = ~xmax, 
            ymin = ~ymin, ymax = ~ymax, fill = ~I(col)), data = d, 
            alpha = 0.9, inherit.aes = FALSE)
    }
    df2 <- p$data
    df2$y <- p$data$geneList[df2$x]
    p.pos <- p + geom_segment(data = df2, aes_(x = ~x, xend = ~x, 
        y = ~y, yend = 0), color = "grey")
    p.pos <- p.pos + ylab("Ranked List Metric") + xlab("Rank in Ordered Dataset") + 
        theme(plot.margin = margin(t = -0.1, r = 0.2, b = 0.2, 
            l = 0.2, unit = "cm"))
    if (!is.null(title) && !is.na(title) && title != "") 
        p.res <- p.res + ggtitle(title)
    if (length(color) == length(geneSetID)) {
        p.res <- p.res + scale_color_manual(values = color)
        if (length(color) == 1) {
            p.res <- p.res + theme(legend.position = "none")
            p2 <- p2 + scale_color_manual(values = "black")
        }
        else {
            p2 <- p2 + scale_color_manual(values = color) + theme(legend.position = "none",plot.margin=margin(t = 0.1, r = 0.2, b = 0.1, l = 0.2, unit = "cm"))
        }
    }
    if (pvalue_table) {
        pd <- x[geneSetID, c("Description", "pvalue", "p.adjust")]
        rownames(pd) <- pd$Description
        pd <- pd[, -1]
        for (i in seq_len(ncol(pd))) {
            pd[, i] <- format(pd[, i], digits = 3)
        }
        tp <- tableGrob2(pd, p.res)
        p.res <- p.res + theme(legend.position = "none",plot.margin=margin(t = 0.8, r = 0.2, b = 0.2, l = 0.2,unit = "cm")) + annotation_custom(tp, 
            xmin = quantile(p.res$data$x, 0.5), xmax = quantile(p.res$data$x, 
                0.95), ymin = quantile(p.res$data$runningScore, 
                0.75), ymax = quantile(p.res$data$runningScore, 
                0.9))
    }
    plotlist <- list(p.res, p2, p.pos)[subplots]
    n <- length(plotlist)
    plotlist[[n]] <- plotlist[[n]] + theme(axis.line.x = element_line(), 
        axis.ticks.x = element_line(), axis.text.x = element_text())
    if (length(subplots) == 1) 
        return(plotlist[[1]] + theme(plot.margin = margin(t = 0.2, 
            r = 0.2, b = 0.2, l = 0.2, unit = "cm")))
    if (length(rel_heights) > length(subplots)) 
        rel_heights <- rel_heights[subplots]
    aplot::gglist(gglist = plotlist, ncol = 1, heights = rel_heights)

}

gsInfo <- function(object, geneSetID) {
    geneList <- object@geneList

    if (is.numeric(geneSetID))
        geneSetID <- object@result[geneSetID, "ID"]

    geneSet <- object@geneSets[[geneSetID]]
    exponent <- object@params[["exponent"]]
    df <- gseaScores(geneList, geneSet, exponent, fortify=TRUE)
    df$ymin <- 0
    df$ymax <- 0
    pos <- df$position == 1
    h <- diff(range(df$runningScore))/20
    df$ymin[pos] <- -h
    df$ymax[pos] <- h
    df$geneList <- geneList

    df$Description <- object@result[geneSetID, "Description"]
    return(df)
}

tableGrob2 <- function(d, p = NULL) {
    # has_package("gridExtra")
    d <- d[order(rownames(d)),]
    tp <- gridExtra::tableGrob(d)
    if (is.null(p)) {
        return(tp)
    }

    # Fix bug: The 'group' order of lines and dots/path is different
    p_data <- ggplot_build(p)$data[[1]]
    # pcol <- unique(ggplot_build(p)$data[[1]][["colour"]])
    p_data <- p_data[order(p_data[["group"]]), ]
    pcol <- unique(p_data[["colour"]])
    ## This is fine too
    ## pcol <- unique(p_data[["colour"]])[unique(p_data[["group"]])]  
    j <- which(tp$layout$name == "rowhead-fg")

    for (i in seq_along(pcol)) {
        tp$grobs[j][[i+1]][["gp"]] <- gpar(col = pcol[i])
    }
    return(tp)
}

gseaplot3修改一下clusterProfiler默认绘图函数,数据可视化文章来源地址https://www.toymoban.com/news/detail-808310.html

到了这里,关于gseaplot3修改一下clusterProfiler默认绘图函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • tinymce修改默认字体

    tinymce引入后样式如下: 那么想使用px作为字体单位,还有想自定义字体怎么办呢? 只需在tinymce配置里加上以下参数: 加入参数后效果如下: 我想默认字体大小12px,默认字体 PingFang SC 怎么办? 这个通过配置是无法实现的,要在 content.min.css 修改默认样式 方法:在 content.m

    2024年02月13日
    浏览(43)
  • 修改docker默认存储位置

    有以下两种修改 方式 方法1. 修改docker默认存储位置 step 1: docker默认存储路径在 /var/lib/docker 下 step 2: 修改默认路径:修改docker的systemd的配置文件 step 3: 修改 docker.service 文件 vim /usr/lib/systemd/system/docker.service 修改内容如下: step 4: 编辑 /etc/docker/daemon.json 文件, 如果不存在得手

    2024年02月12日
    浏览(38)
  • docker修改默认存储位置

    修改默认的docker存储路径 默认路径为:/var/lib/docker 修改为:/data/docker 使用 docker info | grep “Docker Root Dir” 查看docker默认存储位置 创建新的存储目录,将旧的文件复制到新的目录(这个时候最好不好有容器在运行,安装时最好就修改好路径) 修改docker的systemd的docker.service的配置

    2024年02月12日
    浏览(33)
  • windows默认编码格式修改

    1.命令提示符界面输入 chcp 936 对应 GBK 65001 对应 UTF-8 2.临时更改编码格式 chcp 936(或65001) 3.永久更改编码格式 依次开控制面板-时钟和区域-区域-管理-更改系统区域设置,然后按下图所示,勾选使用UTF-8语言支持。然后重启电脑。此时再使用chcp查看当前默认编码为65001 对应 UTF

    2024年02月05日
    浏览(30)
  • git 修改默认分支

    点击管理 进入分支管理页面 新建分支 新建成功以后 进入仓库中的管理 修改默认分支为新建的分支 此时就可以删除master分支

    2024年02月06日
    浏览(27)
  • git修改默认分支

    git checkout 分支   切换到当前分支  git branch --set-upstream-to=origin/complete(远程分支名)   设置当前分支的上游分支为远程分支complete  git branch --unset-upstream master   取消master上游分支的身份  现在,使用git commit,git push 命令可以直接提交到指定的远程分支中。 参考:git 创建分支

    2024年01月20日
    浏览(29)
  • clickhouse修改默认密码

    1.明文密码  找到下面的语句,增加 明文密码 2. sha256密码  修改user.xml里对应的密码和标签 3. sha1密码  修改user.xml里对应的密码和标签  

    2024年02月12日
    浏览(25)
  • 修改pip默认安装位置

    默认安装位置是C:Users用户名AppDataRoamingPythonPython38Scripts。浅浅建议不把这些东西放到系统盘,而是放到 Python 自己的安装目录的对应的子目录中! 首先,使用如下命令 python -m site 查看 这里的 USER_BASE 和 USER_SITE 其实就是默认的通过 pip 自动下载的脚本和依赖安装包的基础

    2023年04月18日
    浏览(43)
  • windows修改默认端口3389

    1、防火墙安全组、云防火墙安全组添加修改替换的端口 2、注册表修改 按“win+R”后输入regedit,进入注册表编辑; HKEY_LOCAL_MACHINESystemCurrentControlSetControlTerminal Server WinStationsRDP-Tcp ; 找到右侧的 \\\"PortNumber\\\",用十进制方式显示,默认为3389,改为15312端口 HKEY_LOCAL_MACHINESYSTEM

    2024年02月16日
    浏览(30)
  • unity 修改默认脚本

       

    2024年02月13日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包