joey711 / phyloseq

phyloseq is a set of classes, wrappers, and tools (in R) to make it easier to import, store, and analyze phylogenetic sequencing data; and to reproducibly share that data and analysis with others. See the phyloseq front page:
http://joey711.github.io/phyloseq/
582 stars 187 forks source link

Row names as data labels in ordination plots #1494

Open pig-raffles opened 3 years ago

pig-raffles commented 3 years ago

Hi,

This is again probably a simple issue but is there a way to get the row names/numbers from the sample data section of the phyloseq object as a label for data points in ordination plots?

This would allow me to identify outlier samples far quicker

Thanks, again

FelixGiraud commented 3 years ago

It's been a while scince i didn't use Phyloseq but i managed to find some code:

ordinate <- ordinate(phyloseq_object, method ="PCoA", distance= "bray")
p <- plot_ordination(phyloseq_object, ordinate, justDF = T)

sample.id is a column of your p object containing the ids of your samples, if you do not have one in your metadata table, make sure to build it before you run your code

ggplot(data=p, aes(x=Axis.1, y=Axis.2, label= sample.id)) +
  geom_point(size= 3) +
  geom_line() +
  geom_text(vjust = 0, nudge_y = 0.01, size=3)+
  labs(x="PC1", y= "PC2")

Hope that helps.

aemann01 commented 2 years ago

Just in case the above solution didn't work for anyone else -- for me (phyloseq version 1.38.0):

plot_ordination(phyloseq_object, ordinate, justDF = T)

didn't generate a column with sample.id to use as a label. Ended up duplicating my row names as a new column.

sample_data(phyloseq_object)['sample_id'] <- row.names(sample_data(phyloseq_object)) 
# generate ordination and plot
ordcap <- ordinate(phyloseq_object, "CAP", "bray", ~study_group)
plot_ordination(phyloseq_object, ordcap, "samples", label="sample_id") + theme_minimal()
Srvcl commented 1 year ago

Thanks a lot @aemann01. I get the id samples in my phyloseq plot!!