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.
本文的目标是描述如何更改使用 r 软件和 ggplot2包生成的图的颜色。 可以通过名称(例如: “ red”)或十六进制代码(例如: “ # ff1234”)指定颜色。 在 r 中可用的不同颜色系统描述在这个链接: r 中的颜色。
In this R tutorial, you will learn how to :
在这个 r 教程中,你将学习如何:
ToothGrowth and mtcars data sets are used in the examples below.
下面的例子使用了 ToothGrowth 和 mtcars 数据集。
# Convert dose and cyl columns from numeric to factor variablesToothGrowth$dose <- as.factor(ToothGrowth$dose)mtcars$cyl <- as.factor(mtcars$cyl)head(ToothGrowth)
## len supp dose## 1 4.2 VC 0.5## 2 11.5 VC 0.5## 3 7.3 VC 0.5## 4 5.8 VC 0.5## 5 6.4 VC 0.5## 6 10.0 VC 0.5
head(mtcars)
## mpg cyl disp hp drat wt qsec vs am gear carb## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1## Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1## Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2## Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
Make sure that the columns dose and cyl are converted as factor variables using the R script above.
确保使用上面的 r 脚本将列剂量和 cyl 转换为因子变量。
library(ggplot2)# Box plotggplot(ToothGrowth, aes(x=dose, y=len)) +geom_boxplot()# scatter plotggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# box plotggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot(fill='#A4A4A4', color="darkred")# scatter plotggplot(mtcars, aes(x=wt, y=mpg)) + geom_point(color='darkblue')
The following R code changes the color of the graph by the levels of dose :
下面的 r 代码根据剂量水平改变图表的颜色:
# Box plotbp<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) + geom_boxplot()bp# Scatter plotsp<-ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) + geom_point()sp
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 :
默认颜色(色调)的亮度(l)和彩度(c,颜色的强度)可以通过以下函数调整色调:
# Box plotbp + scale_fill_hue(l=40, c=35)# Scatter plotsp + scale_color_hue(l=40, c=35)
Note that, the default values for l and c are : l = 65, c = 100.
注意,l 和 c 的默认值是: l 65,c 100。
A custom color palettes can be specified using the functions :
可以使用以下函数指定自定义调色板:
# Box plotbp + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))# Scatter plotsp + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
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.
注意,可以使用参数中断来控制图例的外观。 对于其他的比例尺 xx ()函数也是如此。
# Box plotbp + scale_fill_manual(breaks = c("2", "1", "0.5"), values=c("red", "blue", "green"))# Scatter plotsp + scale_color_manual(breaks = c("8", "6", "4"), values=c("red", "blue", "green"))
The built-in color names and a color code chart are described here : color in R.
描述了内置的颜色名称和颜色代码图: r 中的颜色。
The color palettes available in the RColorBrewer package are described here : color in R.
在 RColorBrewer 包中可用的调色板描述如下:。
# Box plotbp + scale_fill_brewer(palette="Dark2")# Scatter plotsp + scale_color_brewer(palette="Dark2")
The available color palettes in the RColorBrewer package are :
在 RColorBrewer 包中可用的调色板有:
Install and load the color palettes as follow :
安装并加载调色板,如下所示:
# Installinstall.packages("wesanderson")# Loadlibrary(wesanderson)
The available color palettes are :
现有的调色板有:
library(wesanderson)# Box plotbp+scale_fill_manual(values=wes_palette(n=3, name="GrandBudapest"))# Scatter plotsp+scale_color_manual(values=wes_palette(n=3, name="GrandBudapest"))
The functions to use are :
使用的功能如下:
# Box plotbp + scale_fill_grey() + theme_classic()# Scatter plotsp + scale_color_grey() + theme_classic()
Change the gray value at the low and the high ends of the palette :
改变调色板高低两端的灰度值:
# Box plotbp + scale_fill_grey(start=0.8, end=0.2) + theme_classic()# Scatter plotsp + scale_color_grey(start=0.8, end=0.2) + theme_classic()
Note that, the default value for the arguments start and end are : start = 0.2, end = 0.8
请注意,start 和 end 参数的默认值是: start 0.2,end 0.8
The graph can be colored according to the values of a continuous variable using the functions :
可以根据连续变量的值使用以下函数对图形进行着色:
The graphs are colored using the qsec continuous variable :
这些图使用 qsec 连续变量着色:
# Color by qsec valuessp2<-ggplot(mtcars, aes(x=wt, y=mpg, color=qsec)) + geom_point()sp2# Change the low and high colors# Sequential color schemesp2+scale_color_gradient(low="blue", high="red")# Diverging color schememid<-mean(mtcars$qsec)sp2+scale_color_gradient2(midpoint=mid, low="blue", mid="white", high="red", space ="Lab" )
set.seed(1234)x <- rnorm(200)# Histogramhp<-qplot(x =x, fill=..count.., geom="histogram") hp# Sequential color schemehp+scale_fill_gradient(low="blue", high="red")
Note that, the functions scale_color_continuous() and scale_fill_continuous() can be used also to set gradient colors.
注意,缩放函数的颜色连续()和缩放填充连续()也可以用来设置渐变颜色。
# Scatter plot# Color points by the mpg variablesp3<-ggplot(mtcars, aes(x=wt, y=mpg, color=mpg)) + geom_point()sp3# Gradient between n colorssp3+scale_color_gradientn(colours = rainbow(5))
联系客服