R/ggenrichGroup.r

Defines functions ggenrichGroup

Documented in ggenrichGroup

#'ggenrichGroup function
#'@export
#'@description
#'ggenrichGroup function is used to draw the enrichment plot of multiple groups.
#'@param file input file, The return value of the preprocess function (ggenrichData function param: format="R_ClusterProfiler",type="groups_pathway")
#'@param labels set the legend label.
#'@param setcolor set color. default ("red","navy","darkgreen","blueviolet","chocolate4","black")
#'@return a plot, the format is pdf
#'@importFrom grDevices dev.off
#'@importFrom utils read.delim
#'@importFrom utils read.csv
#'@examples
#labels <- c("A vs. B", "C vs. D")
#inputfile <- ggenrichData(system.file("extdata", c("gsea_group1.csv","gsea_group2.csv"), package = "ggenrich",mustWork = TRUE),format="R_ClusterProfiler",type="groups_pathway")
#ggenrichGroup(inputfile,labels)


ggenrichGroup <- function(file,
                      labels,
                      setcolor=c("red","navy","darkgreen","blueviolet","chocolate4","black")){
  if (requireNamespace("ggplot2", quietly = TRUE)) {
    p.res <- ggplot2::ggplot(file, ggplot2::aes_(x = ~x, y = ~runningScore)) +
      ggplot2::xlab(NULL) +
      ggplot2::geom_line(ggplot2::aes_(color= file$.id), size=0.6) +
      ggplot2::geom_area(ggplot2::aes_(fill = file$.id),
                         position = ggplot2::position_dodge(width = 0),
                         alpha=0.1) +
      ggplot2::scale_color_manual(values = setcolor,
                                  labels = labels)+
      ggplot2::scale_fill_manual(values = setcolor, guide = FALSE) +
     ggplot2::scale_x_continuous(expand=c(0,0)) +
      ggplot2::geom_hline(yintercept = 0, lty = 1, lwd = 1) +
      ggplot2::labs(x = "Rank", y = "ES",
                    title = "This is the first pathway") +
      ggplot2::theme_bw() +
      ggplot2::theme(axis.text = ggplot2::element_text(size = 12, face = "bold"),
                    legend.position = c(1,1),
                    legend.justification = c(1,1),
                    legend.background = ggplot2::element_blank(),
                    legend.key = ggplot2::element_blank(),
                    legend.title = ggplot2::element_blank()) +
      ggplot2::theme(axis.ticks.x=ggplot2::element_blank(),
                     plot.margin=ggplot2::margin(t=.2, r = .2, b=0, l=.2, unit="cm"))
    i <- 0
    for (term in unique(file$Description)) {
      idx <- which(file$ymin != 0 & file$Description == term)
      file[idx, "ymin"] <- i
      file[idx, "ymax"] <- i + 1
      i <- i + 1
    }
    p2 <- ggplot2::ggplot(file, ggplot2::aes_(x = ~x)) +
      ggplot2::geom_linerange(ggplot2::aes_(ymin=~ymin,
                                            ymax=~ymax,
                                            color=~Description)) +
      ggplot2::xlab(NULL) +
      ggplot2::ylab(NULL) +
      ggplot2::scale_color_manual(values = setcolor) +
      ggplot2::theme_bw() +
      ggplot2::theme(panel.grid = ggplot2::element_blank()) +
      ggplot2::theme(panel.border = ggplot2::element_blank()) +
      ggplot2::theme(legend.position = "none",
                     plot.margin = ggplot2::margin(t=.1, b=0,unit="cm"),
                     axis.ticks = ggplot2::element_blank(),
                     axis.text = ggplot2::element_blank(),
                     axis.line.x = ggplot2::element_blank()) +
      ggplot2::scale_x_continuous(expand=c(0,0)) +
      ggplot2::scale_y_continuous(expand=c(0,0))
    if (requireNamespace("cowplot", quietly = TRUE)) {
      result <- cowplot::plot_grid(p.res, p2, ncol = 1, align="v", rel_heights = c(1.5, .2))
    }
    ggplot2::ggsave("GSEA_multi_groups.pdf",width=5,height=3)
    return(result)
     }
}
ying-ge/ggEnrich documentation built on Nov. 24, 2019, 12:34 p.m.