0

I am trying to produce a CCA graph from a large microbiome dataset of turtles that includes some additional information like diet and climate. I am trying to see how these factors influence microbial populations in my animals. The issue that I'm getting is that for some reason the plot is including diet (carnivore and omnivore) as a species. I have checked the raw data and it is all ok, no mistakes.

See below for my code and plot:

Current plot


type constraining_variables_formula <- ~ Diet + Climate

cca_result <- ordinate(
  physeq = turtles_prop,
  method = "CCA",
  formula = constraining_variables_formula
)

sample_scores <- vegan::scores(cca_result, display = "sites")

bp_scores <- vegan::scores(cca_result, display = "bp")
bp_df <- data.frame(labels = rownames(bp_scores), bp_scores)

sample_data_df$Diet <- trimws(as.character(sample_data_df$Diet))
sample_data_df$Species <- trimws(as.character(sample_data_df$Species))

sample_df_plot <- data.frame(
  CCA1 = sample_scores[, "CCA1"],
  CCA2 = sample_scores[, "CCA2"],
  Diet = as.factor(sample_data(turtles_prop)$Diet),
  Species = as.factor(sample_data(turtles_prop)$Species)
)


p_final <- ggplot(data = sample_df, aes(x = CCA1, y = CCA2)) +
  geom_point(aes(color = Diet)) +
  geom_jitter(
    aes(shape = Diet, color = Species),
    size = 3,           # Adjust the point size
    width = 0.05,       # Adjust the amount of horizontal jitter
    height = 0.05       # Adjust the amount of vertical jitter
  ) +
  geom_segment(
    data = bp_df,
    aes(x = 0, y = 0, xend = CCA1, yend = CCA2),
    arrow = arrow(length = unit(0.02, "npc")),
    color = "black",
    linewidth = 0.5,
    inherit.aes = FALSE # Prevents inheriting aesthetics from the main plot
  ) +
  geom_text_repel(
    data = bp_df,
    aes(x = CCA1, y = CCA2, label = labels),
    color = "black",
    size = 3.5,
    box.padding = 0.5,
    inherit.aes = FALSE # Prevents inheriting aesthetics
  ) +
  labs(
      x = paste("CCA1 (", format(summary(cca_result)$cont$importance[2, "CCA1"] * 100, digits = 3), "%)", sep = ""),
      y = paste("CCA2 (", format(summary(cca_result)$cont$importance[2, "CCA2"] * 100, digits = 3), "%)", sep = ""),
      color = "Species", # Title for the color legend
      shape = "Diet"     # Title for the shape legend
    ) +
      theme_bw() +
  geom_hline(yintercept = 0, linetype = "dashed", color = "gray") +
  geom_vline(xintercept = 0, linetype = "dashed", color = "gray")

print(p_final)
1
  • You are specifying the color aesthetic in geom_point() as "Diet" and in geom_jitter() as "Species". ggplot combines all levels in one color scale but chooses the name that was supplied last, so in this case "Species". Commented Oct 23 at 8:32

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.