rggplot2qiime

Error: Aesthetics must be either length 1 or the same as the data (9): size


Im trying to plot my PCOA with qiime2R and ggplot2 since I find 2D UniFrac plots more informative than those given with Emperor in 3D. So I followed the qiime2R tutorial, but Im having trouble adding different aesthetics, I have seen other post but I don’t really understand the issue. Here’s my code.

The folks from the qiime2 forum could´t help me, so perhaps you can.

library(ggplot2)
library(qiime2R)

 metadata<-read_q2metadata("Metadata.tsv")
    uwunifrac<-read_qza("weighted_unifrac_pcoa_results.qza")
    shannon<-read_qza("shannon_vector.qza")$data %>% rownames_to_column("SampleID") 

    uwunifrac$data$Vectors %>%
      select(SampleID, PC1, PC2) %>%
      left_join(metadata) %>%
      left_join(shannon) %>%
      ggplot(aes(x=PC1, y=PC2, color=`Name`, shape=`Origen`, size=shannon)) +  
      geom_point(alpha=0.5) +#alpha controls transparency and helps when points are overlapping
      theme_q2r() +
      scale_shape_manual(values=c(16,1), name="Name") + 
      scale_size_continuous(name="Shannon Diversity") +
      scale_color_discrete(name="Name")
    ggsave("PCoA.pdf", height=4, width=5, device="pdf")

When I try to compile I get the following error message:

Error: Aesthetics must be either length 1 or the same as the data (9): size

But If I only include 1 aesthetic as told, for instance:

ggplot(aes(x=PC1, y=PC2, color=Name))

It does compile. I was wondering If there’s a way of including all the aesthetic like I tried in the first place.

Here's a look into my data

> uwunifrac$data$Vectors %>%
+     select(SampleID, PC1, PC2) %>%
+     left_join(metadata) %>%
+     left_join(shannon) Joining, by = "SampleID" Joining, by = "SampleID"   



 SampleID         PC1          PC2                  Name               Origen shannon_entropy
1   D1_16S  0.18825645 -0.047168594 Lixiviado de mina (2)    Minas de Riotinto        6.791300
2   D2_16S  0.09366309  0.106100155     Corta Atalaya (3)    Minas de Riotinto        4.375214
3   D3_16S  0.08844727  0.003118801         Rio Tinto (4)    Minas de Riotinto        5.090431
4   D4_16S -0.10670870  0.494599196            Odiel 3,4%            Rio Odiel        6.285716
5   D5_16S -0.51231492  0.021180538            Odiel 7,7%            Rio Odiel        5.881951
6   D6_16S -0.30245182 -0.339569170             Odiel 15%            Rio Odiel        5.440700
7   D7_16S  0.21368026 -0.041278615                  Pool Balsas de fosfoyesos        7.715696
8   D8_16S  0.20073878 -0.072788430          Piezometro 1 Balsas de fosfoyesos        7.547468
9   D9_16S  0.13668959 -0.124193881          Piezometro 2 Balsas de fosfoyesos        7.671805

So now I realize, the name of the variable in aes( should have been shannon_entropy insted of shannon. Changing that I get a new error message

Error: Insufficient values in manual scale. 3 needed but only 2 provided.

Thanks in advance, Jose.


Solution

  • For your updated question:

    You are providing two shape values (16 and 1) for Origen, but Origen has 3 values (Minas ..., Rio ..., and Balsas...). You must provide a shape value for each Origen value.