Search code examples
rggplot2geom-bar

ggplot : several histogram as one


I want to plot the results of a benchmark of several bioinformatics tools, using ggplot. I would like t have all the bars on the same graph instead of having one graph for each tool. I already have an output with LibreOffice (see image below), but I want to re-do it with ggplot.

For now I have this kind of code for each tool (example with the first one) :

data_reduced <- read.table("benchmark_groups_4sps", sep="\t", header=TRUE)

p<-ggplot(data=data_reduced, aes(x=Nb_sps, y=OrthoFinder)) +
    geom_bar(stat="identity", color="black", fill="red") +
    xlab("Number of species per group") + ylab("Number of groups") +
    geom_text(aes(label=OrthoFinder), vjust=1.6, color="black", size=3.5)

But I have not found out how to paste together all the graphes, but not how to merge them into a single one.

wanted output (from LibreOffice)

My input data :

Nb_species  OrthoFinder FastOrtho   POGS (no_para)  POGS (soft_para)    proteinOrtho
4   125 142 152 202 114
5   61  65  42  79  44
6   37  29  15  21  8
7   19  17  4   7   5
8   15  10  1   0   0
9   10  2   0   0   0

Thanks !


Solution

  • Maybe this can help you in the right direction:

    # sample data
    df = data.frame(Orthofinder=c(1,2,3), FastOrtho=c(2,3,4),   POGs_no_para=c(1,2,2))
    
    library(reshape2)
    library(dplyr)
    
    # first let's convert the dataset: Convert to long format and aggregate.
    df = melt(df, id.vars=NULL)
    df = df %>% group_by(variable,value) %>% count()
    
    # Then, we create a plot.
    ggplot(df, aes(factor(value), n, fill = variable)) + 
      geom_bar(stat="identity", position = "dodge") + 
      scale_fill_brewer(palette = "Set1")
    

    There is enough documentation around on formatting a plot, so I'll leave that to you ;) Hope this helps!

    EDIT: Since the question was changed to work with a different dataset as origin while I was typing my answer, here is the modified code to work with that:

    df = data.frame(Nb_species = c(4,5,6,7),  OrthoFinder=c(125,142,100,110), FastOrtho=c(100,120,130,140))
    
    library(reshape2)
    library(dplyr)
    df = melt(df, id.vars="Nb_species")
    
    ggplot(df, aes(factor(Nb_species), value, fill = variable)) + 
      geom_bar(stat="identity", position = "dodge") + 
      scale_fill_brewer(palette = "Set1")