Plotting different gene ontology categories in a barplot
0
0
Entering edit mode
8 months ago
aradhana • 0

Hi, I have performed gene ontology analysis on a set of differentially expressed genes using the Enrichr package in R.I wanted to construct a barplot in R which shows the count of genes on one axis with their respective go terms. I am new to R, so I am not very familiar with plotting using ggplot2. I have a dataset that has the following columns: Gene Ontology Class - Biological Process, Cellular Component, or Molecular Function, GO terms - GO terms associated with a particular GO class, Number of genes -contains the count of genes associated with the respective GO terms, Adjusted p-value, https://ibb.co/XDPJFTj

I want to create a plot like the one shown below. (taken from a publication) example1

I will be really grateful if anybody can help me out.

HI Gene ontology R • 1.0k views
ADD COMMENT
1
Entering edit mode

Please post example data. Without example data (data images do not help), it will be difficult to follow the question. Graph in the image is faceted by 3 comparisons (a,b and c in the image). In your image, there is no such data. Without that data, following is an example how to facet and fill colour. Use following steps for faceting and colouring and change it as per your needs:

  1. Load data into R
  2. Load ggplot2
  3. Plot GO term on Y-axis and count on X-axis using geom_bar function.
  4. Facet by comparison (missing from your data image)
  5. Fill by class (GO terms)
  6. Move the legend to the bottom of the plot

Try this example code:

set.seed(100)

df2=data.frame(class=sample(c("BP","MF","CC"), 30, replace=T), "GO Term"=paste0("term_", sample(1:30, 30, replace = F)), count=sample(10:30, 30, replace = T), comparison=sample(c("A","B","C"), 30, replace = T))

ggplot(df2, aes(count, GO.Term, fill = class)) +
    geom_bar(stat = "identity") +
    facet_wrap( ~ comparison, scales = "free", nrow = 3) +
    geom_text(
        aes(label = count),
        color = "black",
        hjust = -0.1,
        size = 4,
        position = position_dodge(0.9)
    ) +
    theme(
        legend.position = "bottom",
        panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        strip.text.x = element_text(size = 14, face = "bold"),
        strip.background = element_blank()
    )

Rplot03

ADD REPLY
0
Entering edit mode

Hi, Thank you so much for your response. my dataset looks like this:


>

    Class              GO Term                                 Count     Adjusted.P.value

    <chr>              <chr>                                     <dbl>            <dbl>

    1 Cellular Component cytoskeleton (GO:0005856)                    9             0.00155
    2 Cellular Component secretory granule lumen (GO:0034774)         6          0.0133 

    3 Cellular Component ficolin-1-rich granule lumen (GO:1904813)     4          0.0133 

    4 Cellular Component tertiary granule lumen (GO:1904724)           4          0.0133 

    5 Cellular Component tertiary granule (GO:0070820)                 4           0.0253 
    6 Cellular Component ficolin-1-rich granule (GO:0101002)           4          0.0321 
    7  Molecular Function   cadherin binding (GO:0045296)              7          0.00314
    8 Molecular Function    aldehyde-lyase activity (GO:0016832)       2          0.00897                   
   9  Biological Process    glucose metabolic process (GO:0006006)     4        0.00227
 10   Biological Process glycolytic process through glucose-6-phosphate (GO:0061620)    3  0.00241
 11   Biological Process    canonical glycolysis (GO:0061621)   3   0.00241
 12  Biological Process glucose catabolic process to pyruvate (GO:0061718)  3   0.00241

I want to re-arrange and order the bars according to their ontology. For example, all the biological process bars will be arranged together, followed by molecular function and cellular component. But I don't know how to go about that.

ADD REPLY
0
Entering edit mode

Please post expected image. See if following works

library(ggplot2)
library(tidyr)
library(dplyr)
library(stringi)

df3 %>%
    extract(GO.Term, into = c("Term", "id"), "(.*)\\s\\((.*)\\)") %>%
    mutate(Term=stri_trans_totitle(Term)) %>%
    ggplot(aes(Count, reorder(Term,Count), fill = Class)) +
    geom_bar(stat = "identity") +
    facet_wrap( ~ Class, nrow = 3, , scales = "free") +
    theme(legend.position = "bottom",
          axis.title.y = element_blank()
    ) +
    geom_text(
        aes(label = paste(Count, ", Adj.P= ", round(Adjusted.P.value,4))),
        color = "black",
        size = 4,
        hjust=-0.1,
        position = position_dodge(0.9)
    )

Rplot01

ADD REPLY
0
Entering edit mode

It is working. I can't thank you enough for helping me in solving this issue.

ADD REPLY
0
Entering edit mode

I have updated the image/code and check it out.

ADD REPLY
0
Entering edit mode

Thank you so much. It seems my input dataset is missing few details as you have correctly pointed out. I am going to rearrange my dataset and follow the steps you've mentioned. Thank you again.

ADD REPLY

Login before adding your answer.

Traffic: 2261 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6