rggplot2ggfortify

R: Set data point size of PCA autoplot according to a value


I am using the autoplot function to make a PCA plot. This is the code:

library(ggfortify)
df <- iris[c(1, 2, 3)]
autoplot(prcomp(df), data=iris, colour = "Species")

I would like to use the iris[c(4)] to be used in a way that the size of each data point in the PCA plot corresponds to the value of iris[c(4)]. An example from the iris dataset: the data point 132 should have a bigger size than data point 1. Is this possible in ggforitfy?


Solution

  • Just pass in the column name you want to size by as a string.

    autoplot(prcomp(df), data=iris, colour = "Species", size = "Petal.Width")
    

    enter image description here

    You can then adjust sizing like ggplot2 normal:

    + scale_size_continuous(range = c(1,3))