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/
586 stars 187 forks source link

plot_ordination() bugs? #1547

Closed marwa38 closed 2 years ago

marwa38 commented 2 years ago

hello .. I am not sure if these are bugs? please let me know

bray.pcoa <- ordinate(ps.3.rare, method = "PCoA", distance = "bray")

# Plot using plot_ordination function of PhyloSeq:
plot_ordination(ps.3.rare, bray.pcoa, color = "part", axes = c(1,2)) +
  geom_point(size = 2) +
  labs(title = "PCoA of Bray Curtis Distances", color = "part") +
  coord_equal() +
  stat_ellipse() 

without coord_equal() and without stat_ellipse() image

with coord_equal() image

with using coord_equal() and stat_ellipse() image with using stat_ellipse() image

Could you please advise why I am not getting the correct aspect ratio of the plot? even after using coord_equal() ? and why I am getting this warning message when tried using stat_ellipse()?

Warning message:
In MASS::cov.trob(data[, vars]) : Probable convergence failure

let me know if you would like to have a look into ps.3.rare many thanks

ycl6 commented 2 years ago

Hi @marwa38

I think it is working perfectly. If you observe closely, you will notice the spacing in both axes when using stat_ellipse and coord_equal are the same. I think the different breaks in the X- and Y-axis is making it hard to notice this.

For example:

library(phyloseq)
library(ggplot2)
data(enterotype)

ps <- enterotype

bray.pcoa <- ordinate(ps, method = "PCoA", distance = "bray")

p1 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) +
        labs(title = "Original")

p2 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + coord_equal() +
        labs(title = "coord_equal")

p3 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + stat_ellipse() +
        labs(title = "stat_ellipse")

p4 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + coord_equal() + stat_ellipse() +
        labs(title = "coord_equal + stat_ellipse")

gridExtra::grid.arrange(p1, p2, p3, p4)

plot_ordination1

If we set the same breaks settings for X- and Y-axis

p1 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) +
        scale_x_continuous(breaks = seq(-1,1,0.2)) + scale_y_continuous(seq(-1,1,0.2)) +
        labs(title = "Original")

p2 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + coord_equal() +
        scale_x_continuous(breaks = seq(-1,1,0.2)) + scale_y_continuous(seq(-1,1,0.2)) +
        labs(title = "coord_equal")

p3 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + stat_ellipse() +
        scale_x_continuous(breaks = seq(-1,1,0.2)) + scale_y_continuous(seq(-1,1,0.2)) +
        labs(title = "stat_ellipse")

p4 <- plot_ordination(ps, bray.pcoa, color = "Enterotype", axes = c(1,2)) +
        geom_point(size = 1) + coord_equal() + stat_ellipse() +
        scale_x_continuous(breaks = seq(-1,1,0.2)) + scale_y_continuous(seq(-1,1,0.2)) +
        labs(title = "coord_equal + stat_ellipse")

gridExtra::grid.arrange(p1, p2, p3, p4)

plot_ordination2

As for the warning message with cov.trob. It's to do with how the ellipse was calculated when using stat_ellipse, the default is using multivariate t-distribution and that uses the cov.trob function from the MASS package. Although you can change the tol setting in cov.trob to adjust for the convergence tolerance for fitting, the stat_ellipse doesn't allow you to adjust this. If you want to explore more about this, you can approach the ggplot2's developers.

marwa38 commented 2 years ago

many thanks @ycl6, this is so helpful and insightful :)