Search code examples
rggplot2colorscolor-schemeggbiplot

How to add/assign color to many groups automatically


I need some help to modify my R script that could assign different color to each group. I always need to specify color name/code manually [e.g., scale_fill_manual(values = c("grey", "red", "yellow", "green"))].

Complete R script is here

     library(ggbiplot)
iris<- read.csv("data_umar.csv")
log.ir <- iris[, 1:11]
ir.species <- iris[, 12]

ir.pca <- prcomp(log.ir, center = TRUE, scale. = TRUE)

##for PC3 and PC4 choices = c(1,2)

ggbiplot(ir.pca, choices = c(1,2), obs.scale = 1, var.scale = 1, groups = ir.species, alpha = 0) +
  theme(legend.direction = 'vertical', legend.position = 'right') + 
  scale_color_manual(values=c("Black", "Black", "Black", "Black")) +
  scale_fill_manual(values = c("grey", "red", "yellow", "green")) + # just offset by one to show
  geom_point(size = 1.2, shape = 21, aes(fill = groups, color = groups))

Many thanks for the help.


Solution

  • scale_color_manual(values=ir.species)

    scale_color_manual(values=1:20) # assumes 20 colours