Kiran
Kiran

Reputation: 13

Removing lines from barplot in R

I have created a barplot for metagenomic data using RStudio

plot_bar(mp3, "Sampletype", fill = "Family", title = title)

But I am getting lines inside the bar.I need clear bars without any lines. How to do it?

Click on the link for the plot below

library("phyloseq"); packageVersion("phyloseq")

library("biomformat"); packageVersion("biomformat")

library("ggplot2"); packageVersion("ggplot2")

library("phyloseq"); packageVersion("phyloseq")

library("biomformat"); packageVersion("biomformat")

library("ggplot2"); packageVersion("ggplot2")

biom1 = biomformat::read_biom(biom_file = "otu_table.json.biom")

mp0 = import_biom(biom1, parseFunction = parse_taxonomy_greengenes)

tax_table(mp0) <- tax_table(mp0)[, 1:7]

treeFile1 = "rep_set.tre"

tree1 = read_tree(treeFile1)

tree1

class(tree1)

mp2 = merge_phyloseq(mp1, tree1) mp2 repseqFile = "seqs_rep_set.fasta"

bs1 = Biostrings::readDNAStringSet(repseqFile) names(bs1) <- gsub("\s.+$", "", names(bs1))

sum(names(bs1) %in% taxa_names(mp2)) mp3 = merge_phyloseq(mp2, bs1)

plot_bar(mp3, "Sampletype", fill = "Family", title = title)

Upvotes: 0

Views: 4075

Answers (1)

eipi10
eipi10

Reputation: 93821

plot_bar from the phyloseq package uses ggplot for plotting. You can look at the code for plot_bar by typing plot_bar in the console, which yields:

function (physeq, x = "Sample", y = "Abundance", fill = NULL, title = NULL, 
          facet_grid = NULL) {
    mdf = psmelt(physeq)
    p = ggplot(mdf, aes_string(x = x, y = y, fill = fill))
    p = p + geom_bar(stat = "identity", position = "stack", color = "black")
    p = p + theme(axis.text.x = element_text(angle = -90, hjust = 0))
    if (!is.null(facet_grid)) {
        p <- p + facet_grid(facet_grid)
    }
    if (!is.null(title)) {
        p <- p + ggtitle(title)
    }
    return(p)
}

As you can see, the function includes this statement:

geom_bar(stat = "identity", position = "stack", color = "black")

The color="black" argument is what causes the black lines. This is a pretty basic bar plot and you can just create your own function based on this code:

library(phyloseq)

my_plot_bar = function (physeq, x = "Sample", y = "Abundance", fill = NULL, title = NULL, 
                        facet_grid = NULL) {
    mdf = psmelt(physeq)
    p = ggplot(mdf, aes_string(x = x, y = y, fill = fill))
    p = p + geom_bar(stat = "identity", position = "stack")
    p = p + theme(axis.text.x = element_text(angle = -90, hjust = 0))
    if (!is.null(facet_grid)) {
        p <- p + facet_grid(facet_grid)
    }
    if (!is.null(title)) {
        p <- p + ggtitle(title)
    }
    return(p)
}

Notice that the only change is that I've removed color="black". You can now run my_plot_bar instead of plot_bar and get a bar plot without the black lines.

my_plot_bar(mp3, "Sampletype", fill = "Family", title = title)

Upvotes: 2

Related Questions