joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
586 stars 186 forks source link

How to replace the deprecated ggplot2 function aes_string? #1654

Closed ricmedveterinario closed 1 year ago

ricmedveterinario commented 1 year ago

I am using the following function, suggested in the post: #https://stackoverflow.com/questions/52747802/how-to-remove-very-thin-bar-plot-outline-#border/75194438?noredirect=1#comment132690525_75194438

plot_bar_2 <- function (physeq, x = "Sample", y = "Abundance", fill = NULL, title = NULL, facet_grid = NULL, border_color = NA) { mdf = psmelt(physeq) p = ggplot(mdf, aes_string(x = x, y = y, fill = fill)) p = p + geom_bar(stat = "identity", position = "stack", color = border_color) 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) }

However, I am getting the following message when I follow the steps:

library(phyloseq) data("GlobalPatterns") gp.ch <- subset_rate(GlobalPatterns, Phylum == "Spirochaetes") p <- plot_bar_2(gp.ch, fill = "Phylum") p + geom_bar(stat = "identity") + scale_fill_manual(values = cbPalette)

or with any other border color if you want

p <- plot_bar_2(gp.ch, fill = "Phylum", border_color = "red")

Error message":

Warning message:

aes_string() was deprecated in ggplot2 3.0.0. ℹ Please use tidy evaluation ideoms with aes() This warning is displayed once every 8 hours. Call lifecycle::last_lifecycle_warnings() to see where this warning was generated.

It would be something reported here at:

https://stackoverflow.com/questions/74414272/how-to-replace-the-deprecated-ggplot2-function-aes-#string-accepting-an-arbitrar

But I don't know how to fix it.

I would like to no longer receive this message, because I believe that this script will no longer #work for some time. Thanks.

ycl6 commented 1 year ago

Hi @ricmedveterinario I modified the function, it should now work under ggplot2_3.4.0

plot_bar_2 <- function(physeq, x = "Sample", y = "Abundance", fill = NULL, title = NULL, 
                       facet_grid = NULL, border_color = NA)
{
        mdf = psmelt(physeq)
        p = if(!is.null(fill) & fill %in% colnames(mdf)) { 
                ggplot(mdf, aes(x = .data[[x]], y = .data[[y]], fill = .data[[fill]]))
        } else {
                ggplot(mdf, aes(x = .data[[x]], y = .data[[y]]))
        }
        p = p + geom_bar(stat = "identity", position = "stack", color = border_color)
        p = p + theme(axis.text.x = element_text(angle = -90, hjust = 0))
        p
}
library(phyloseq)
library(ggplot2)
data("GlobalPatterns")
gp.ch <- subset_taxa(GlobalPatterns, Phylum == "Spirochaetes")

plot_bar_2(gp.ch, fill = "Phylum") 
plot_bar_2(gp.ch, fill = "SampleType") 
ricmedveterinario commented 1 year ago

Hi @ycl6,

It worked using this modified function that you created, I didn't know how to correct it,

Your 16S related scripts always help me a lot,

Thank you and congratulations for your work,

ycl6 commented 1 year ago

Hi @ricmedveterinario I wasn't aware of this, so it's nice to know to avoid using aes_string() from now on and to update any old codes that used it. If you think this has sufficiently resolve your question, feel free to close this.

I think many of the plotting functions in phyloseq also use aes_string(), so they might need to be updated in the future when aes_string() is formally removed from ggplot2. Hope it is something @joey711 will look into when the time comes.

ricmedveterinario commented 1 year ago

yes i agree with you @ycl6 .

Thanks for the help, and I'll close this question.