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

plot_ordination function plots shapes 'double' within itself #1686

Closed Robvh-git closed 1 year ago

Robvh-git commented 1 year ago

I'm analysing NGS data using the phyloseq package. I want to create a PCoA (Bray-curtis DM) plot using following code:


pcoa_plot_1<-plot_ordination(physeq,ord_bc_pcoa,color="variable1", shape="variable2") + 
    geom_point(size=4) + 
    scale_color_manual(values = c("var1X"="red","var1Y"="#55BE25","var1Z"="blue")) +
    scale_shape_manual(values = c("var2A"=0,"var2B"=2,"var2C"=3,"var2D"=4,"var2D"=5,"var2E"=6,"varF"=7,"var2G"=8)) +
    ggtitle("title1")
pcoa_plot_1

In the resulting plot, the shapes look like this: image

As you can see, the shapes are plotted twice, at a different size within eachother. Also, if I would manually assign a 'filled' shape to a variable (e.g. shape code 22), it also does not appeared filled on the plot, but still has a empty fill.

When I run another geom_point() plot of ggplot2, I do get the good looking shapes.

How can I fix this?

(PS: I also posted this on stackoverflow, but now think it suits here better: https://stackoverflow.com/questions/76465183/plot-ordination-function-of-phyloseq-plots-shapes-double-within-itself_

ycl6 commented 1 year ago

Hi @Robvh-git It's because you added geom_point(size=4) to the ggplot object when plot_ordination() already produced the object with its own geom_point(). Since there is no way to setting the point size while using the function, the dirty trick will be to make changes to the ggplot object itself, e.g.

library(phyloseq)
library(ggplot2)
data(GlobalPatterns)

GP = prune_taxa(names(sort(taxa_sums(GlobalPatterns), TRUE)[1:50]), GlobalPatterns)
sample_data(GP)$Shape = ifelse(sample_data(GP)$SampleType %in% c("Feces","Skin","Tongue"), "A", "B")
gp_bray_pcoa = ordinate(GP, "CCA", "bray")

# Plot
p = plot_ordination(GP, gp_bray_pcoa, "samples", color="SampleType", shape="Shape") + 
        scale_shape_manual(values = c("A" = 2, "B" = 3))

# Change the point size to 4
p$layers[[1]]$aes_params$size = 4
Robvh-git commented 1 year ago

Hi @ycl6 thanks for the quick reply!

Aha that makes sense, thank you!

Your solution works, many thanks!