david-barnett / microViz

R package for microbiome data visualization and statistics. Uses phyloseq, vegan and the tidyverse. Docker image available.
https://david-barnett.github.io/microViz/
GNU General Public License v3.0
101 stars 11 forks source link

Logical variable becomes numeric when plotting CCA #137

Closed hkaspersen closed 8 months ago

hkaspersen commented 8 months ago

I am using the following code to generate a CCA plot:

bracken_fixed %>%
  tax_select("Bacteria", ranks_searched = "Rank1", strict_matches = TRUE) %>%
  tax_agg(rank = "Rank6") %>%
  tax_transform(trans = "compositional") %>%
  ord_calc(
    constraints = "antibiotic",
    method = "CCA"
  ) %>%
  ord_plot(
    size = 2,
    color = "antibiotic",
    plot_taxa = 1:10,
    tax_vec_length = 3,
    auto_caption = NA,
    tax_lab_style = tax_lab_style(
      type = "text", 
      max_angle = 90, 
      aspect_ratio = 0.7,
      size = 3, 
      fontface = "bold"
    )
  )

The "antibiotic" variable is a TRUE/FALSE variable, and in the plot I was hoping to be able to plot this TRUE/FALSE relationship. However, in the plot the value becomes numeric. Is this something that happens in the CCA analysis? How can I fix this?

test

david-barnett commented 8 months ago

Hi Hakon

ord_calc is converting the logical variable antibiotic into a numeric (dummy) variable with levels 0 (false) and 1 (true). ord_calc can't handle factor or character variables as constraints.

microViz doesn't have a way of plotting centroids for factor constraints, if that is what you are after? You could look at vegan's own ordination calculation and plotting functionality, e.g. with cca and plot if you want this.

hkaspersen commented 8 months ago

Thanks for the clarification! I was only interested in plotting it as a character variable on the dots, so that I can see which ones are true and which are false properly. I fixed it by creating another column with the same data and used that one to plot instead, and that worked fine.