【问题标题】:Removing lines from barplot in R从 R 中的条形图中删除线条
【发布时间】:2018-01-18 05:17:25
【问题描述】:

我使用 RStudio 为宏基因组数据创建了条形图

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

但我在栏内出现线条。我需要没有任何线条的清晰栏。怎么做?

库(“phyloseq”); packageVersion("phyloseq")

图书馆(“生物格式”); packageVersion("biomformat")

图书馆(“ggplot2”); packageVersion("ggplot2")

库(“phyloseq”); packageVersion("phyloseq")

图书馆(“生物格式”); packageVersion("biomformat")

图书馆(“ggplot2”); packageVersion("ggplot2")

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

mp0 = import_biom(biom1, parseFunction = parse_taxonomy_greengenes)

tax_table(mp0)

treeFile1 = "rep_set.tre"

tree1 = read_tree(treeFile1)

树1

类(树1)

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

bs1 = Biostrings::readDNAStringSet(repseqFile) 名称(bs1)

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

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

【问题讨论】:

  • 我试图用谷歌搜索你的绘图函数的名称,但没有找到任何东西。它来自哪里?
  • 如果这是您编写的函数,请显示代码和一些示例数据。如果这是一个包中的函数,请包含该包。
  • 假设您的函数使用 ggplot,它是否包含这样的语句:geom_bar(colour="black")?如果是这样,删除 colour="black" 将删除条形部分上的黑色轮廓。
  • 它使用 Phyloseq 包。

标签: r qiime


【解决方案1】:

来自phyloseq 包的plot_bar 使用ggplot 进行绘图。您可以通过在控制台中输入 plot_bar 来查看 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", 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)
}

如您所见,该函数包含以下语句:

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

color="black" 参数是导致黑线的原因。这是一个非常基本的条形图,您可以根据此代码创建自己的函数:

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)
}

请注意,唯一的变化是我删除了color="black"。您现在可以运行 my_plot_bar 而不是 plot_bar 并获得没有黑线的条形图。

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

【讨论】: