Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoplot for prcomp - changing symbols

Tags:

plot

r

ggplot2

pca

I've got a little plotting predicament and I'm hoping you all have a quick and easy answer for me. I ran a PCA using prcomp(), and made my first attempt at plotting it using ggplot. I have succeeded at making an aesthetically pleasing figure, however I have just had colleagues point out that it isn't currently colour-blind friendly.

The colour scheme that I have used cannot easily be changed (as there are many many other figures that this PCA plot needs to coordinate with), so I have been trying to change my figure to include different symbols for each group.

I have found another post that mentions using scale_shape_manual() to add in these symbols, but when I try to implement it, the symbol type does not change. It also doesn't give me an error, which has me unsure of where to start troubleshooting.

An example with everyone's favourite PCA dataset, iris, follows:

library(ggplot2)
library(ggfortify)

df <- iris[c(1, 2, 3, 4)]
iris.pca<-(prcomp(df))

autoplot(iris.pca, data=iris, colour="Species", frame=TRUE)+
  scale_colour_manual(values=c("forestgreen","red","blue"))+
  scale_fill_manual(values=c("forestgreen","red","blue"))+
  scale_shape_manual(values=c(25,22,23))+
  theme_bw()

If anyone has suggestions on how to easily change these symbols, or another plotting function that is more cooperative for this sort of change, I would greatly appreciate it.

like image 409
DoctorSpruce Avatar asked Sep 15 '25 06:09

DoctorSpruce


1 Answers

The simple solution is as follows (shape="Species" needed to be added into the original autoplot function):

library(ggplot2)
library(ggfortify)

df <- iris[c(1, 2, 3, 4)]
iris.pca<-(prcomp(df))

autoplot(iris.pca, data=iris, colour="Species", shape="Species",frame=TRUE)+
  scale_colour_manual(values=c("forestgreen","red","blue"))+
  scale_fill_manual(values=c("forestgreen","red","blue"))+
  scale_shape_manual(values=c(25,22,23))+
  theme_bw()
like image 69
DoctorSpruce Avatar answered Sep 16 '25 22:09

DoctorSpruce