How to make pcoa biplot in R using q2-deicode ordination

Here is a minimal example but @cmartino can you please advise on the most appropriate way of selecting the taxa and for scaling their lengths? I am not 100% what I did is valid.

library(tidyverse)
library(qiime2R)

ord<-read_qza("~/Downloads/ordination.qza")
meta<-read_tsv(file = "~/Downloads/metadata.txt") %>% 
      rename(SampleID=`#SampleID`) %>%
      filter(SampleID!="#q2:types")
tax<-read_qza("~/Downloads/taxonomy.qza")$data %>%
  rename(FeatureID=Feature.ID)


#create the base plot with only the arrows
baseplot<-
  ggplot() +
  theme_bw() +
  xlab(paste(round(100*ord$data$ProportionExplained[1],2),"%")) +
  ylab(paste(round(100*ord$data$ProportionExplained[2],2),"%")) +
  ggtitle("DEICODE biplot of Moving Pictures Data") +
  geom_segment(data=ord$data$Species %>% 
                 mutate(a=sqrt(PC1^2+PC2^2)) %>% # calculate the distance from the origin
                 top_n(8, a) %>% #keep 8 furthest away points
                 mutate(PC1=PC1*0.3, PC2=PC2*0.3) %>% # scale arrows linearly... is this ok? 
                 left_join(tax),
               aes(x=0, xend=PC1, y=0, yend=PC2, color=Taxon),
               arrow = arrow(length = unit(0.3,"cm"))
              )

# now overlay samples
baseplot +
  geom_point(
    data=ord$data$Vectors %>%
      left_join(meta),
      aes(x=PC1, y=PC2, fill=BodySite), 
      shape=21
      )

12 Likes