来源:http://www.sthda.com/english/wiki/ggplot2-colors-how-to-change-colors-automatically-and-manually
The goal of this article is to describe how to change the color of a graph generated using R software and ggplot2 package. A color can be specified either by name (e.g.: “red”) or by hexadecimal code (e.g. : “#FF1234”). The different color systems available in R are described at this link : colors in R.
In this R tutorial, you will learn how to :
change colors by groups (automatically and manually)
use RColorBrewer and Wes Anderson color palettes
use gradient colors
ggplot2 color, graph, R software
Simple plots
library(ggplot2)
Box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) +geom_boxplot()
scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Use a single color
box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(fill=’#A4A4A4’, color=“darkred”)
scatter plot
ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(color=‘darkblue’)
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Change colors by groups
Default colors
The following R code changes the color of the graph by the levels of dose :
Box plot
bp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_boxplot()
bp
Scatter plot
sp<-ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) + geom_point()
sp
ggplot2 color, graph, R softwareggplot2 color, graph, R software
The lightness (l) and the chroma (c, intensity of color) of the default (hue) colors can be modified using the functions scale_hue as follow :
Box plot
bp + scale_fill_hue(l=40, c=35)
Scatter plot
sp + scale_color_hue(l=40, c=35)
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Note that, the default values for l and c are : l = 65, c = 100.
Change colors manually
A custom color palettes can be specified using the functions :
scale_fill_manual() for box plot, bar plot, violin plot, etc
scale_color_manual() for lines and points
Box plot
bp + scale_fill_manual(values=c("#999999", “#E69F00”, “#56B4E9”))
Scatter plot
sp + scale_color_manual(values=c("#999999", “#E69F00”, “#56B4E9”))
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Note that, the argument breaks can be used to control the appearance of the legend. This holds true also for the other scale_xx() functions.
Box plot
bp + scale_fill_manual(breaks = c(“2”, “1”, “0.5”),
values=c(“red”, “blue”, “green”))
Scatter plot
sp + scale_color_manual(breaks = c(“8”, “6”, “4”),
values=c(“red”, “blue”, “green”))
ggplot2 color, graph, R softwareggplot2 color, graph, R software
The built-in color names and a color code chart are described here : color in R.
Use RColorBrewer palettes
The color palettes available in the RColorBrewer package are described here : color in R.
Box plot
bp + scale_fill_brewer(palette=“Dark2”)
Scatter plot
sp + scale_color_brewer(palette=“Dark2”)
ggplot2 color, graph, R softwareggplot2 color, graph, R software
The available color palettes in the RColorBrewer package are :
RColorBrewer palettes
Use Wes Anderson color palettes
Install and load the color palettes as follow :
Install
install.packages(“wesanderson”)
Load
library(wesanderson)
The available color palettes are :
wesanderson-color palettes
library(wesanderson)
Box plot
bp+scale_fill_manual(values=wes_palette(n=3, name=“GrandBudapest”))
Scatter plot
sp+scale_color_manual(values=wes_palette(n=3, name=“GrandBudapest”))
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Use gray colors
The functions to use are :
scale_colour_grey() for points, lines, etc
scale_fill_grey() for box plot, bar plot, violin plot, etc
Box plot
bp + scale_fill_grey() + theme_classic()
Scatter plot
sp + scale_color_grey() + theme_classic()
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Change the gray value at the low and the high ends of the palette :
Box plot
bp + scale_fill_grey(start=0.8, end=0.2) + theme_classic()
Scatter plot
sp + scale_color_grey(start=0.8, end=0.2) + theme_classic()
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Note that, the default value for the arguments start and end are : start = 0.2, end = 0.8
Continuous colors
The graph can be colored according to the values of a continuous variable using the functions :
scale_color_gradient(), scale_fill_gradient() for sequential gradients between two colors
scale_color_gradient2(), scale_fill_gradient2() for diverging gradients
scale_color_gradientn(), scale_fill_gradientn() for gradient between n colors
Gradient colors for scatter plots
The graphs are colored using the qsec continuous variable :
Color by qsec values
sp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()
sp2
Change the low and high colors
Sequential color scheme
sp2+scale_color_gradient(low=“blue”, high=“red”)
Diverging color scheme
mid<-mean(mtcars$qsec)
sp2+scale_color_gradient2(midpoint=mid, low=“blue”, mid=“white”,
high=“red”, space =“Lab” )
ggplot2 color, graph, R softwareggplot2 color, graph, R softwareggplot2 color, graph, R software
Gradient colors for histogram plots
set.seed(1234)
x <- rnorm(200)
Histogram
hp<-qplot(x =x, fill=…count…, geom=“histogram”)
hp
Sequential color scheme
hp+scale_fill_gradient(low=“blue”, high=“red”)
ggplot2 color, graph, R softwareggplot2 color, graph, R software
Note that, the functions scale_color_continuous() and scale_fill_continuous() can be used also to set gradient colors.
Gradient between n colors
Scatter plot
Color points by the mpg variable
sp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()
sp3
Gradient between n colors
sp3+scale_color_gradientn(colours = rainbow(5))
ggplot2 color, graph, R softwareggplot2 color, graph, R software