i'm following this documentation (https://cran.r-project.org/web/packages/ggfortify/vignettes/plot_pca.html) to run PCA on the iris data set.
library(ggfortify)
df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)
autoplot(pca_res, data = iris, colour = 'Species')
I ran the above code and I get three clusters which are colored by species. I only want to plot a specific species. How can I only plot where the species is setosa in this context?
As the object returned by
autoplotis aggplotobject one option would be to manually filter the data passed to thegeom_pointlayer under the hood. In your case this is quite simple as theggplothas only one layer which we can access viap$layers[[1]]and the data used for this layer viap$layers[[1]]$data.EDIT Another option would be to "remove" the undesired categories by making the invisible. This way the original scale of the axes will be preserved. Of course would it also be possible to use the approach with filtering but additionally setting the scale to the one used for the unfiltered data.