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/
578 stars 187 forks source link

Bar plot suddenly saying not enough colours #1557

Open Avtober opened 2 years ago

Avtober commented 2 years ago

Hi there. I initially used the following code to make a bar plot of my classifications. It initially worked fine and gave me different colours. However I recently needed to make another plot and used the same code but now it is saying that there are not enough colours. I am not sure why it was working fine but now is not. Below is the code and the two bar plots.

Nambarplot.platy <- plot_bar(Namdata.platy) Nambarplot.platy <- plot_bar(Namdata.platy, fill = "Genus") Nambarplot.platy <- Nambarplot.platy + facet_wrap(vars(genus), scales = "free_x", nrow = 1) plot(Nambarplot.platy)

Original plot: image

but when I try to make the same plot now it says the following:

n too large, allowed maximum for palette Set1 is 9 Returning the palette you asked for with that many colors. Do you know why this is?

Thanks Anya

ycl6 commented 2 years ago

Hi @Avtober

Did you include scale_fill_brewer in your plot to use the Set1 palette? As the error message says, you have too many genera where as the Set1 palette can only give you 9 colours.

Avtober commented 2 years ago

Hi @ycl6, I don't think I did use that in my plot. I only included the code above.

Thanks Anya

ycl6 commented 2 years ago

Hi @Avtober

If you phyloseq object is Namdata.platy, what if you make a plot with just plot_bar(Namdata.platy, fill = "Genus") (i.e. without adding ggplot2 functions)?

Avtober commented 2 years ago

HI @ycl6

If I try that with just loading phyloseq and not ggplot2 then I get this message:

plot_bar(Namdata.platy, fill = "Genus") Error in scale_fill_brewer(palette = palname, ...) : could not find function "scale_fill_brewer"

If I then load ggplot2 I get the same message as before:

plot_bar(Namdata.platy, fill = "Genus") Warning message: In RColorBrewer::brewer.pal(n, pal) : n too large, allowed maximum for palette Set1 is 9 Returning the palette you asked for with that many colors

Thanks, Anya

ycl6 commented 2 years ago

Hi @Avtober

It's possible that you are not using the phyloseq's plot_bar function, but a function of the same name but from another package. phyloseq's plot_bar should not have include scale_fill_brewer or use the brewer.pal function.

What if you use phyloseq::plot_bar(Namdata.platy, fill = "Genus") to make sure you are using the function from the phyloseq package.

Using your error message, I found this workflow that seems to replace ggplot's scale_colour_discrete and scale_fill_discrete to use Set1 palette. Maybe that's what happened. https://github.com/mbontrager/16S_processing/blob/40cd41c6096bcb35237163158c5372d5deb65181/R/16S_analysis.R#L28-L37

Avtober commented 2 years ago

Thanks @ycl6

I am still getting the same error message so maybe I should uninstall the R colour brewer or uninstall and reinstall phyloseq? Maybe one is overiding the other? I will have a look at that workflow too.

Thanks Anya

Avtober commented 2 years ago

Hi @ycl6

I have just noticed that when I load the ggplot2 library : get this message:

Attaching package: ‘ggplot2’

The following object is masked by ‘.GlobalEnv’:

scale_fill_discrete

Could this be the problem?

Thanks Anya

ycl6 commented 2 years ago

Hi @Avtober

Yes, I think another package has the same function name scale_fill_discrete that's overriding the ggplot2's function.

After you load your packages, run ?scale_fill_discrete and see what is the package. Below is the example of the manual of scale_fill_discrete from ggplot2

scale_colour_discrete         package:ggplot2          R Documentation

Discrete colour scales

Description:

     The default discrete colour scale. Defaults to
     ‘scale_fill_hue()’/‘scale_fill_brewer()’ unless ‘type’ (which
     defaults to the ‘ggplot2.discrete.fill’/‘ggplot2.discrete.colour’
     options) is specified.

Usage:

     scale_colour_discrete(..., type = getOption("ggplot2.discrete.colour"))

     scale_fill_discrete(..., type = getOption("ggplot2.discrete.fill"))
Avtober commented 2 years ago

Hi @ycl6

It was saying that it was ggplot2 but I noticed that Scale_fill_discrete was also in my global environment and had something to do with DEseq. I ended up just starting a new project with my old scripts and now it is working fine.

Thanks for your help Anya