Project: s41467-025-60274-7 | Function: gseaplot.gseaResult (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Custom Gene Set Enrichment Analysis plotting function. Generates enrichment plots with running enrichment score and gene locations for GSEA results visualization.

**KEY TERMS:** {
  "gseaResult": "GSEA analysis results",
  "enrichment score": "running ES calculation",
  "gene locations": "rank positions"
}

**CONTEXT CODE:** ###########################################################################
## Function to display GSEA plot 
## Modified to annotate new features on the plot 
###########################################################################

library(RColorBrewer)
library(ggplot2)
library(cowplot)
library(gridExtra)
library(ggpmisc)
library(colorspace)
library(grid)

##' @rdname gseaplot
##' @exportMethod gseaplot
setMethod("gseaplot", signature(x = "gseaResult"),
          function (x, geneSetID, by = "all", title = "", color='black',
                    color.line="green", color.vline="#FA5860", ...){
            gseaplot.gseaResult(x, geneSetID = geneSetID,
                                by = by, title = title,
                                color = color, color.line = color.line,
                                color.vline = color.vline, ...)
          })

gseaplot.gseaResult <- function (x, geneSetID, by = "all", title = "",
                                 color='black', color.line="green",
                                 color.vline="#FA5860", ...){
  stop("Function implementation has been masked")
}

##' @rdname gseaplot
##' @param color color of line segments
##' @param color.line color of running enrichment score line
##' @param color.vline color of vertical line which indicating the
##' maximum/minimal running enrichment score
##' @return ggplot2 object
##' @importFrom ggplot2 ggplot
##' @importFrom ggplot2 geom_linerange
##' @importFrom ggplot2 geom_line
##' @importFrom ggplot2 geom_vline
##' @importFrom ggplot2 geom_hline
##' @importFrom ggplot2 xlab
##' @importFrom ggplot2 ylab
##' @importFrom ggplot2 xlim
##' @importFrom ggplot2 aes
##' @importFrom ggplot2 ggplotGrob
##' @importFrom ggplot2 geom_segment
##' @importFrom ggplot2 ggplot_gtable
##' @importFrom ggplot2 ggplot_build
##' @importFrom ggplot2 ggtitle
##' @importFrom ggplot2 element_text
##' @importFrom ggplot2 rel
##' @importFrom cowplot plot_grid
##' @author Guangchuang Yu
mygseaplot2 <- function(x, geneSetID, title = "", color="green", base_size = 11,
                      rel_heights=c(1.5, .5, 1), subplots = 1:3,
                      pvalue_table = FALSE, ES_geom="line",res_table=FALSE, 
                      color_code = FALSE, show_core = TRUE) {
  ES_geom <- match.arg(ES_geom, c("line", "dot"))

  len_geneSetID <- length(geneSetID)
  geneList <- position <- NULL ## to satisfy codetool
  if (length(geneSetID) == 1) {
    gsdata <- gsInfo(x, geneSetID)
    color <- "green"
  } else {
    color <- qualitative_hcl(len_geneSetID,"Dark3")
    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=1)
  } else {
    es_layer <- geom_point(aes_(y = ~runningScore, color= ~Description),
                           size=rel(0.5))
  }

  p.res <- p + es_layer +
    ylab("Running Enrichment Score") +
    theme(legend.position="none")

  if (1 %in% subplots) {
    res1 <- p.res
  } else {
    res1 <- NULL
  }

  ## plot gene position
  p2 <- ggplot(gsdata, aes_(x = ~x, ymin = ~ymin, ymax = ~ymax,
                            color = ~position)) +
    geom_linerange(size=rel(0.5)) +
    scale_color_manual(values = c("red", "green")) +
    theme_classic(base_size) +
    theme(legend.position="none",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()) +
    xlab(NULL) + ylab(NULL) +
    scale_x_continuous(expand=c(0,0))

  if (2 %in% subplots) {
    res2 <- p2
  } else {
    res2 <- NULL
  }

  p3 <- ggplot(gsdata, aes_(x = ~x, y = ~geneList)) +
    geom_segment(aes_(xend = ~x, yend = 0, color = ~position), 
                 size = rel(0.5)) +
    scale_color_manual(values = c("red", "green")) +
    theme_classic(base_size) +
    theme(legend.position="none",
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank()) +
    ylab("Ranked list metric") +
    scale_x_continuous(expand=c(0,0)) +


Complete the function implementation replacing "stop("Not implemented")".