rggplot2colorsellipsecolorbrewer

stat_ellipse and geom_point same color using color brewer


I'm looking to have ellipses for each group (Pond) to match their point color assignment from R brewer, "YlOrRd". I would like the ellipses to be outlined in black but instead all ellipses are filled with black.

plot <- ggplot(plotpca, aes(x=PC1,y=PC2)) +
  geom_point(aes(fill=Pond),pch=21,size=3) +
  stat_ellipse(geom = "polygon",aes(group=Pond,color=Pond),alpha = 0.25, color="black") +
  scale_fill_brewer(palette="YlOrRd",aesthetics = c("color", "fill")) 
plot(plot)

Gives me: enter image description here

Here is a subset of my df to graph:

> dput(head(plotpca))
structure(list(PC1 = c(-0.0505964065450834, 0.0546247180570643, 
0.0385794513330037, -0.0421393543493591, 0.0284612585699651, 
0.0209291807114026), PC2 = c(0.0534548597921124, 0.00385797355835037, 
0.0217307675631555, -0.0319287619859987, -0.00204735494019053, 
-0.00709853162293151), group = structure(c(5L, 1L, 1L, 1L, 1L, 
1L), levels = c("LIL", "RHM", "SCS", "STN", "STS", "TS"), class = "factor"), 
    Pond = structure(c(6L, 4L, 4L, 4L, 4L, 4L), levels = c("RHM", 
    "TS", "SCS", "LIL", "STN", "STS"), class = "factor"), name = c("Ga0598233", 
    "Ga0598239", "Ga0598240", "Ga0598241", "Ga0598242", "Ga0598243"
    )), percentVar = c(0.570804459528504, 0.31299732413872), row.names = c("Ga0598233", 
"Ga0598239", "Ga0598240", "Ga0598241", "Ga0598242", "Ga0598243"
), class = "data.frame")

Solution

  • You're code is quite close, you'll just want to add fill = Pond to your mapping for stat_ellipse()

    plot <- ggplot(plotpca, aes(x=PC1,y=PC2)) +
          geom_point(aes(fill=Pond),pch=21,size=3) +
          stat_ellipse(geom = "polygon",aes(group=Pond,color=Pond,fill=Pond),alpha = 0.25, color="black") +
          scale_fill_brewer(palette="YlOrRd",aesthetics = c("color", "fill")) 
        plot(plot)