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.