2

I am using the autoplot() function to add ellipsis and polygons to my PCA plot in R, my colour palette for the points is the color.brewer 'Set2' palette. When I am filling my ellipsis with geom='polygon', I want the polygon fill colour to be the same as the colour of the points they are covering, so as to match the palette I am using.

However I get a "'mapping' must be creates using aes()" or "Aesthetics are not valid data columns" error.

I'm a novice in R so any help would be very much appreciated.

autoplot(pca_data + aes(color = group)+      # colours
           ggtitle('Principal Component Analysis')+         # title
           theme(plot.title = element_text(hjust = 0.5))+  #centred title
           stat_ellipse(lwd = 0.75,
                        linetype = 2,
                        geom='polygon',
                        aes(fill = group),
                        alpha = 0.25,
                        show.legend = F,
                        level = 0.8) +
           scale_color_brewer(palette = "Set2")) # ellipsis 

This is my current working and will only fill the polygons to match the default colours of the PCA plot, and not to the color palette I want to use

Phil
  • 7,287
  • 3
  • 36
  • 66
JamesLG
  • 21
  • 1
  • 1
    I'm usually not using autoplot. But have you tried adding `+scale_fill_brewer(palette = "Set2")` to your plot? Next step would be have you also tried to use `+aes(color = group, fill = group)` (and then remove `aes(fill = group)` from the stat_ellipse call?) – tjebo Dec 29 '22 at 18:28

0 Answers0