Load needed packages from the tidyverse collection.

library(ggplot2)
library(dplyr)

Create toy data frame as example

# set random seed
set.seed(149)

# create data frame
gseaResults <- data.frame(comparison = rep(x = paste("Comparison", letters[1:3]), each = 6),
                          term = rep(x = paste("Pathway", letters[1:6]), 3),
                          NES = runif(n = 18, min = -3, max = 3),
                          pValue = runif(n = 18, min = 0, max = 0.1)) %>%
  
  # in order to create the toy example, the p values are not adjusted
  # usually the packages performing the GSEA provide an adjusted "fdr" p value
  mutate(adjPValue = p.adjust(pValue, method = "none"))

gseaResults
##      comparison      term         NES      pValue   adjPValue
## 1  Comparison a Pathway a  1.69808019 0.023376729 0.023376729
## 2  Comparison a Pathway b -1.78224154 0.044444890 0.044444890
## 3  Comparison a Pathway c -2.46119472 0.041556251 0.041556251
## 4  Comparison a Pathway d  2.93176147 0.073559746 0.073559746
## 5  Comparison a Pathway e -1.56667608 0.006570853 0.006570853
## 6  Comparison a Pathway f -0.07508853 0.044955960 0.044955960
## 7  Comparison b Pathway a -1.96915000 0.044187573 0.044187573
## 8  Comparison b Pathway b -1.54509334 0.047508179 0.047508179
## 9  Comparison b Pathway c -2.79173555 0.015643033 0.015643033
## 10 Comparison b Pathway d  1.89206096 0.003063155 0.003063155
## 11 Comparison b Pathway e  1.48292037 0.013307257 0.013307257
## 12 Comparison b Pathway f  0.51043686 0.040099814 0.040099814
## 13 Comparison c Pathway a -1.57212255 0.079127562 0.079127562
## 14 Comparison c Pathway b -2.09788654 0.090136414 0.090136414
## 15 Comparison c Pathway c -1.72811443 0.017527967 0.017527967
## 16 Comparison c Pathway d -1.82318559 0.008753073 0.008753073
## 17 Comparison c Pathway e -2.01686556 0.051103047 0.051103047
## 18 Comparison c Pathway f  0.51515810 0.078005218 0.078005218

In case you have a list of data frames resulting for applying “fgsea”, you can use the following function:

bind_rows(fGseaResultList, .id = "comparison")

from the dplyr package.

Once with the data frame prepared, the following function creates the plot.

myGseaPlot <- function(gseaDataFrame, adjPCutoff = 0.05) {
  
  # subset data frame using p value cutoff and add status factor
  toPlot <- subset(gseaDataFrame, adjPValue <= adjPCutoff) %>%
    mutate(status = case_when(NES > 0 ~ "Up", NES < 0 ~ "Down")) %>%
    mutate(status = factor(status, levels = c("Up","Down")))
  
  # create plot
  p <- ggplot(data = toPlot, mapping = aes(x = comparison, y = term, color = status, shape = status, fill = NES, size = -log10(adjPValue))) +
    geom_point() +
    scale_shape_manual(values = c(24,25)) +
    scale_fill_gradient2(high = "coral3", low = "deepskyblue3", mid = "white", midpoint = 0) +
    scale_color_manual(values = c(Up = "coral3", Down = "deepskyblue3")) + 
    ggtitle(paste0("GSEA Results adj.P.Value <= ", adjPCutoff)) +
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5),
          axis.title.y =  element_blank(), 
          axis.title.x = element_blank(),
          axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"), 
          axis.text.y = element_text(face = "bold"))
  
  return(p)
}

Use function to create plot from toy example

examplePlot <- myGseaPlot(gseaDataFrame = gseaResults, adjPCutoff = 0.05)
plot(examplePlot)