qiime2r: % explained variance in PCoA plot

Thank you very much for the tutorial,i just recently discovered it and its awesome. I am relatively new to Qiime 2R and i may have a basic clarification.Is it possible to get the percentage of explained variance on the x and y axis in the unweighted unifrac (or any other ordination plots) in qiime 2R?

1 Like

I don’t know about Qiime2R, but you always can unzip a pcoa.qza file, parse “/data/ordination.txt” file and find there proportions for each ax in order (you should multiply it by 100)

Hi @evon,
Welcome to the forum!

Here is an example of this using Qiime2r to import an ordination artifact from Qiime2.
In short you’ll just need to use the xlab and ylab

  xlab(paste(round(100*ord$data$ProportionExplained[1],2),"%")) +
  ylab(paste(round(100*ord$data$ProportionExplained[2],2),"%")) + 
1 Like

Thanks Mehrbod. A quick breakdown of these dense lines:
-ord$data: Would be the imported artifact from read_qza()
-$ProportionExplained: is a vector of the proportion of variation explained
-[1] or [2] is denoting which axis you want the variation from, this would assume axis 1 is on the x axis and 2 is on the y.
-100* is multiplying by 100 to get a percent
-round(., 2) is saying to round to 2 decimal places
-paste(., “%”) is saying to combine the number rounded to 2 decimals with a percent sign in the axis label
-xlab() and ylab() are the ggplot functions to create the x and y label respectively.


Thank you everyone for help :slightly_smiling_face:

Thanks jbisanz. Great job on this pretty package.

Hey, I hope it is okay if I am also posting a PCoA issue in here, as this thread was the closest one to my issue, still being open.

First of all thanks a lot for creating this great package @jbisanz

I used the PCoA from your tutorial as:
faithpd<-read_qza(“alpha_faithpd.qza”)$data %>% rownames_to_column(“SampleID”)

uwunifrac$data$Vectors %>%
  select(SampleID, PC1, PC2) %>%
  left_join(metadata) %>%
  left_join(faithpd) %>%
  ggplot(aes(x=PC1, y=PC2, color=`horizon`, shape=`layer`, size=faithpd)) +
  geom_point(alpha=0.5) + 
  theme_q2r() +
  scale_shape_manual(values=c(16,1), name="Layer") + 
  scale_size_continuous(name="Phylogenetic diversity") +
ggsave("PCoA.pdf", height=4, width=5, device="pdf") 

But get the error message:
Joining, by = “SampleID”
Joining, by = “SampleID”
Error: Aesthetics must be either length 1 or the same as the data (9): size
Run rlang::last_error() to see where the error occurred.
Aesthetics must be either length 1 or the same as the data (9): size
1. ±(function (x, …) …
2. -ggplot2:::print.ggplot(x)
3. ±ggplot2::ggplot_build(x)
4. -ggplot2:::ggplot_build.ggplot(x)
5. -ggplot2:::by_layer(function(l, d) l$compute_aesthetics(d, plot))
6. -ggplot2:::f(l = layers[[i]], d = data[[i]])
7. -l$compute_aesthetics(d, plot)
8. -ggplot2:::f(…, self = self)
9. -ggplot2:::check_aesthetics(evaled, n)

Can you please help me understanding what I did not consider here? Thanks a lot

The tip off is Error: Aesthetics must be either length 1 or the same as the data (9): size. This suggests something is funky with the size=faithpd. If you look at the the faithpd table you imported I think you will find the column storing the value is called faith_pd such that you need to set up the mapping as ggplot(aes(... size=faith_pd)) +