1
votes

I'm trying to add ellipses in ggplot (from an ordination in Pkg vegan) based on values that were cut into intervals/groups for coloring purposes using geom_point. I think it is easier if I provide an example using the iris data:

data("iris")
T <- metaMDS(iris[1:4]) #perform an ordination with the package vegan
ScIris <- as.data.frame(scores(T)) #extract the values for plotting
RandomNumbers <- runif(150, 1, 100) #generate metaData used for colors
ScIris$test_stat <- RandomNumbers

The code below produces the plot with points colored correctly but only adds one ellipse around all the points.

ggplot(ScIris, aes(NMDS1, NMDS2)) +
geom_point(aes(colour = cut(test_stat, c(0, 25, 50, 75, 85,95, 99, 100))),
             size = 5) +
   stat_ellipse() +
  scale_color_manual(name = "proportions",
                     values = c("(0,25]" = "black",
                                "(25,50]" = "dark green",
                                  "(50,75]" = "green",
                                  "(75,85]" = "yellow",
                                   "(85,95]" = "orange",
                                    "(95,99]" = "purple",
                                    "(99,100]" = "blue",
                     labels = c("0", "25", "50", "75", "85", "95","<100")))

Based on this post ggplot2 draw individual ellipses but color by group I tried modifying the stat_ellipse argument but then nothing will plot correctly.

stat_ellipse(aes(x=NMDS1, y=NMDS2, colour = cut(test_stat, c(0, 25, 50, 75, 85,95, 99, 100), group=cut(test_stat, c(0, 25, 50, 75, 85,95, 99, 100)),type = "norm"))

How can I add the ellipse for each grouping/cut group? So a black ellipse for points between 0-25, blue ellipse for 99-100, etc. ggplot is great but there is a steep learning curve.

1

1 Answers

1
votes

You create the groups on the fly inside geom_point (that's what cut does) but you need to use the groups again for the ellipses and their colors. So it is better the define the groups first.

library("tidyverse")
library("vegan")

data("iris")
T <- metaMDS(iris[1:4]) #perform an ordination with the package vegan

# For reproducibility
set.seed(1234)

ScIris <- as.data.frame(scores(T)) %>%
  mutate(test_stat = runif(150, 1, 100)) %>%
  mutate(group = cut(test_stat, c(0, 25, 50, 75, 85, 95, 99, 100)))

ggplot(ScIris, aes(NMDS1, NMDS2)) +
  geom_point(aes(colour = group), size = 5) +
  stat_ellipse(aes(color = group, group = group)) +
  scale_color_manual(name = "proportions",
                     values = c("(0,25]" = "black",
                                "(25,50]" = "dark green",
                                "(50,75]" = "green",
                                "(75,85]" = "yellow",
                                "(85,95]" = "orange",
                                "(95,99]" = "purple",
                                "(99,100]" = "blue",
                                labels = c("0", "25", "50", "75", "85", "95","<100")))
#> Too few points to calculate an ellipse
#> Warning: Removed 1 rows containing missing values (geom_path).

Created on 2019-03-31 by the reprex package (v0.2.1)