R Color Palettes and Point Shapes - meyermicrobiolab/Meyer_Lab_Resources GitHub Wiki
There are a variety of ways to customize the colors in your R figures which will probably all be generated with ggplot2, as described in the Cookbook for R (ggplot2 colors). FYI, changing the colors in a base R plot would use different syntax. You can choose to apply an established color palette or you can manually assign colors to each individual group in your data.
If you are going to bother to make a custom color palette, you should be creating something that is colorblind friendly. See the lists of colorblind friendly colors on the Cookbook for R link above. If you only have 2 or 3 categories to color, black and white (and gray) color schemes add the most contrast and will likely be the most appropriate choice. Don't forget you can also use open/closed points in both b/w and color palettes. Below is an example of how you would make your own palette manually. Basically you name a palette with a list of hexadecimal color codes as shown here or using words like "blue" and "black" (which is less precise, but handy if you only need a few colors). Then call the palette when you are plotting the graph. By default, ggplot2 will use the colors in the order they are listed and apply them to the groups alphabetically.
my20colors<-c("#c26162","#d689c2","#d64142","#6db643","#9c58cb","#cdab39","#626fdf","#5cba78","#bd3fa0","#497535","#de76dd","#949345","#6e58a2","#cd6529","#46aed7","#d94681","#49b39c","#9e4e77","#bd814c","#798fd5")
So maybe you don't have a ton of colors OR you just want more control. Sometimes you want to precisely color the samples for easier (more intuitive) interpretation. For example, I like water samples to be blue, soil samples to be brown, leaves to be green, etc. Keep in mind that your goal in making figures is to COMMUNICATE. All of these seemingly minor adjustments can make a big difference in how much information you can easily convey.
Here is an example of a palette where I want to assign exact colors to specific groups:
cols<-c("Dichocoenia stokesii"="#009E73","Diploria labyrinthiformis"="#e79f00","Montastraea cavernosa"="#56B4E9","Orbicella faveolata"="#0072B2")
An example of how I would "call" this palette when plotting a PCA:
p<-ggplot(df_out,aes(x=PC1,y=PC2,fill=samples$Coral,shape=samples$Condition))
p<-p+geom_point(size=3)+theme(axis.title = element_text(size=14))+theme(axis.text=element_text(size=12))+
theme(legend.title = element_text(size=14))+theme(legend.text = element_text(size=12))+
scale_fill_manual(values=cols)+
scale_shape_manual(values=c(21,22,24))+
guides(fill = guide_legend(override.aes=list(shape=21)))
p + labs(x=xlab, y=ylab, fill="Coral", shape="Condition") + coord_fixed()
Notice that I had to tell it where to get the values for the fill (on the first line) and separately tell it to use the manual fill values "scale_fill_manual(values=cols)". I have also manually applied 3 different shapes for the health/disease condition "scale_shape_manual(values=c(21,22,24))" but let ggplot2 to assign them alphabetically. I could easily reorder the shape codes to customize. See Cookbook for R shape codes for adjusting open/closed points and changing shapes.
For example, in stacked bar graphs depicting relative abundance of ASVs/OTUs, you may need a lot of colors. Decide how many colors you need (you can use less than the defined colors, but not more) and use the following package to generate the most unique/distinct set of colors for the palette;
library(randomcoloR)
n <- 99
palette <- distinctColorPalette(n)
You can rerun the last line to get a new selection of colors. When you have found one you like, you can save the palette like this:
sink("palette.txt")
print(palette)
sink()
It saves a list - but it isn't comma separated. If you open the text file it will look something like this:
[1] "#D7B490" "#7692C0" "#DE463F" "#E0D9CF" "#5DE9CE" "#E69EDA" "#6875DA" "#5DA0ED" "#C2E6A4" "#59735F" "#6DE2E6" [12] "#D48B3F" "#98AFA2" "#B2CAE6" "#E3B1B2" "#6446E1" "#AB67E0" "#E6CF85" "#9FF044" "#55EFA0" "#7EDE9F" "#8AAA58" [23] "#E34AD1" "#E14682" "#D9E74A" "#E7D8EA" "#A2E982" "#E5E7C1" "#61C4E6" "#B7E8E6" "#9B8399" "#A6E7C7" "#DCE67E" [34] "#5BB49F" "#AF90DE" "#E5C042" "#DDBFED" "#DF8870" "#96476E" "#E38399" "#C636EF" "#E47AD5" "#4FD75D"
But you can take that list and set it as a new palette for future use (by removing the line numbers and adding commas):
palette<-c("#D7B490","#7692C0","#DE463F","#E0D9CF","#5DE9CE","#E69EDA","#6875DA","#5DA0ED","#C2E6A4","#59735F","#6DE2E6","#D48B3F","#98AFA2",
"#B2CAE6","#E3B1B2","#6446E1","#AB67E0","#E6CF85","#9FF044","#55EFA0","#7EDE9F","#8AAA58","#E34AD1","#E14682","#D9E74A","#E7D8EA",
"#A2E982","#E5E7C1","#61C4E6","#B7E8E6","#9B8399","#A6E7C7","#DCE67E","#5BB49F","#AF90DE","#E5C042","#DDBFED","#DF8870","#96476E",
"#E38399","#C636EF","#E47AD5","#4FD75D")