ggplot/Qiime2R PCOA. Error: Aesthetics must be either length 1 or the same as the data

Hi everyone!,

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

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.

Thanks in advance, Jose.
shannon_vector.qza (59.0 KB)weighted_unifrac_pcoa_results.qza (67.1 KB) Metadata.tsv (383 Bytes)

Hi @jose_gacia,
I reclassified this as an “other bioinformatics tools” topic and gave it a more fitting title because this is not actually an error from QIIME 2 — you transferred your data into an R dataframe using qiime2r, but now you are trying to plot with ggplot. Others may be able to provide support but this is not a QIIME 2 technical support issue as originally classified.

similar errors have been reported on this forum though and are worth taking a look: https://forum.qiime2.org/search?q=Aesthetics%20must%20be%20either%20length%201%20or%20the%20same%20as%20the%20data

1 Like

Ok, the folfs at stack Overflow gave me the solution.

I had two errors.

In the first place the variable name is size=shannon_entropy instead of size=shannon like I had

The other error was in the scale_shape_manual function (where each value indicates the shape of the element: circles, triangles etc.). The length of the vector has to be equal to the number of elements in the variable. In my case, it was 3. As you can see the vector has two elements c(16,1). So an extra element had to be added c(18,16,17), for instance.

In any case, here´s the final code I managed to compile

metadata<-read_q2metadata("Metadata.tsv")
uwunifrac<-read_qza("jaccard_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_entropy)) + 
  geom_point(alpha=0.5) + #alpha controls transparency and helps when points are overlapping
  theme_bw() +
  scale_shape_manual(values=c(18,16,17), name="Origen") + #see http://www.sthda.com/sthda/RDoc/figure/graphs/r-plot-pch-symbols-points-in-r.png for numeric shape codes
  scale_size_continuous(name="Shannon Diversity") +
  scale_color_discrete(name="Name") 
ggsave("jaccard_pcoa_R.png", height=5.5, width=7, device="png")
3 Likes

Hi @Nicholas_Bokulich,

Thank you for your answer, the folks of stackoverflow gave me the solution, I have written It down so It may help somebody else in the future.

Cheers, Jose.

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.