Chiara Homann
Chiara Homann

Reputation: 1

How to fix scale_color_manual? This is not working properly anymore

I am currently struggling with scale_color_manual in ggplot2, although this has never been a problem. I keep getting the error message: "Error: Insufficient values in manual scale. 9 needed but only 0 provided." However, I have clearly provided enough colors, so it just doesn't seem to be registering. Does anyone know what is going on/how I can fix this?

My ggplot2 package version is: 3.3.3. Phyloseq Version is 1.30.0

library(phyloseq)
library(ggplot2)

data("GlobalPatterns")
GP <- prune_species(speciesSums(GlobalPatterns) > 0, GlobalPatterns)

#this usually works to assign colors below
cohortcol<- c("#70AD47","orange", "blue", "green","red",
  "yellow","purple","black","grey")

a<-plot_richness(GP, x="SampleType", color="SampleType",
  measures=c("Chao1", "Shannon")) +
  scale_color_manual(values=alpha(cohortcol, 0.7)) +
  theme(axis.title.x=element_blank(), 
    axis.text.x=element_blank(),
    legend.position="none", axis.title.y = element_blank())
print(a)

It then doesn't print a and gives me my error message. I get the same message if I were to remove cohortcol and just add colors as a vector instead instead.

GP is a phyloseq object.

Restarting R used to do the trick, however, even this has stopped working now.

Thanks for your help!

Upvotes: 0

Views: 1366

Answers (1)

Sinh Nguyen
Sinh Nguyen

Reputation: 4487

You need to provide names to the cohortcol so that ggplot2 would know which color matching with what values in the data

Your code should work fine if you do this

cohortcol<- c("#70AD47","orange", "blue", "green","red",
  "yellow","purple","black","grey")
names(cohortcol) <- c("Feces", "Freshwater", "Freshwater (creek)",
   "Mock", "Ocean", "Sediment (estuary)", "Skin", "Soil", "Tongue")

Here is the full working code. I didn't use phyloseq much but there are multiple warning about deprecated function in your code, pay attention to that too.

library(phyloseq)
library(ggplot2)

data("GlobalPatterns")
GP <- prune_species(speciesSums(GlobalPatterns) > 0, GlobalPatterns)
#> Warning: 'prune_species' is deprecated.
#> Use 'prune_taxa' instead.
#> See help("Deprecated") and help("phyloseq-deprecated").
#> Warning: 'speciesSums' is deprecated.
#> Use 'taxa_sums' instead.
#> See help("Deprecated") and help("phyloseq-deprecated").

#this usually works to assign colors below
cohortcol<- c("#70AD47","orange", "blue", "green","red",
  "yellow","purple","black","grey")
names(cohortcol) <- c("Feces", "Freshwater", "Freshwater (creek)",
  "Mock", "Ocean", "Sediment (estuary)", "Skin", "Soil", "Tongue")

plot_richness(GP, x="SampleType", color="SampleType",
  measures=c("Chao1", "Shannon")) +
  scale_color_manual(values=alpha(cohortcol, 0.7)) +
  theme(axis.title.x=element_blank(), 
    axis.text.x=element_blank(),
    legend.position="none", axis.title.y = element_blank())

Created on 2021-05-01 by the reprex package (v2.0.0)

Upvotes: 1

Related Questions