Search code examples
rggplot2colorspca

How to get rid of inner dot filled with deeper colour in geom_point in R?


I don't want that smaller, inner, deeper coloured points in the geom point in the graph. How do I get rid of it? I tried playing around with shapes but nothing happened.

library(stats)
library(ggfortify)
sp1 <- rnorm(72, mean = 4, 1)
sp2 <- rnorm(72, mean = 2, 1)
sp3 <- rnorm(72, mean = 3, 1)
sp4 <- rnorm(72, mean = 9, 1)
temp <- rnorm(72, mean = 20, 5)
season <- rep(c("Summer", "Autumn","Winter", "Spring"), each = 18)

sp.abd <- data.frame(sp1, sp2, sp3, sp4, season, temp)

pca_res <- prcomp(sp.abd[,-c(5,6)], scale. = TRUE)

autoplot(pca_res,
         loadings = TRUE, loadings.colour = 'blue',
         loadings.label = TRUE, loadings.label.size = 3) + 
  theme_classic() + geom_point(aes(color = sp.abd$season), size = 6, alpha = 0.5) + 
  scale_color_manual(values = rainbow(12,s = 0.6, start = 0, end = 0.7, rev = TRUE))

enter image description here

Following Randy's answer below I try to replicate it for colouring by temp (a gradient here)

autoplot(pca_res, sp.abd, color  = temp, 
         loadings = TRUE, loadings.colour = 'blue',
         loadings.label = TRUE, loadings.label.size = 3)  + 
  scale_colour_gradient2(low = "white", mid = "cyan", high = "dodgerblue4", na.value = NA)

It generates just a gray plot. Only if I call colour outside the autoplot command does it use the scale_colour_gradient2 option. But this is still with the dots

enter image description here

autoplot(pca_res, size = 6, alpha. = 0.5,
         loadings = TRUE, loadings.colour = 'blue',
         loadings.label = TRUE, loadings.label.size = 3)  + geom_point(aes(colour = sp.abd$temp)) +
  scale_colour_gradient2(low = "white", mid = "cyan", high = "dodgerblue4", na.value = NA)

enter image description here


Solution

  • It looks like autoplot.prcomp (the autoplot method for prcomp objects as specified by ggfortify) already handles making the points without a geom_point() call, so you can put the color mapping, point size, and point opacity inside autoplot:

    library(stats)
    library(ggfortify)
    sp1 <- rnorm(72, mean = 4, 1)
    sp2 <- rnorm(72, mean = 2, 1)
    sp3 <- rnorm(72, mean = 3, 1)
    sp4 <- rnorm(72, mean = 9, 1)
    season <- rep(c("Summer", "Autumn","Winter", "Spring"), each = 18)
    sp.abd <- data.frame(sp1, sp2, sp3, sp4, season)
    pca_res <- prcomp(sp.abd[,-5], scale. = TRUE)
    
    autoplot(pca_res,
             ################
             # Add this stuff
             data = sp.abd, 
             color = 'season',
             size=6, alpha=.5,
             ################
             loadings = TRUE, loadings.colour = 'blue',
             loadings.label = TRUE, loadings.label.size = 3) + 
      theme_classic() + 
      scale_color_manual(values = rainbow(12,s = 0.6, start = 0, end = 0.7, rev = TRUE))