R 语言中的颜色

我们可以通过给图表着色来视觉上改善它们。这通常通过 `col` 图形参数完成。

我们可以将所需颜色的名称指定为字符串。例如,如果我们要将图表着色为红色,我们传入 `col = "red"`。


在 R 语言中为图表添加颜色

在本节中,我们将使用以下 `temp` 向量来创建 条形图

# create a vector named temp
temp <- c(5,7,6,4,8)

# barplot of temp without coloring
barplot(temp, main="By default")

# barplot of temp with coloring
barplot(temp, col="coral", main="With coloring")

输出

Add Color to Plot Output
为图表添加颜色

在这里,我们在 `barplot()` 函数中传入了 `col = "coral"`,将条形图着色为珊瑚色。

尝试将其替换为“绿色”、“蓝色”、“紫色”等,看看有什么不同。


在 R 语言中使用颜色名称更改图表颜色

R 语言编程有 **657** 种颜色的名称。我们可以使用 `colors()` 函数查看所有这些颜色,或者直接查看此 R 颜色 PDF

# display all color names
colors()

输出

[1] "white"           "aliceblue"        "antiquewhite"        
[4] "antiquewhite1"   "antiquewhite2"    "antiquewhite3"       
[7] "antiquewhite4"   "aquamarine"       "aquamarine1"         
...
... 
[649] "wheat3"        "wheat4"           "whitesmoke"          
[652] "yellow"        "yellow1"          "yellow2"             
[655] "yellow3"       "yellow4"          "yellowgreen"

在这里,`colors()` 函数返回一个按字母顺序排列的所有颜色名称的向量,其中第一个元素是 `"white"`。

我们可以通过索引此向量来为图表着色。例如,`col=colors()[655]` 与 `col="yellow3"` 相同。


在 R 语言中使用十六进制值作为颜色

在 R 语言中,除了使用颜色名称,颜色也可以用十六进制值定义。

我们将颜色定义为 ` #RRGGBB ` 形式的 **6** 位十六进制数字。其中 `RR` 代表**红色**,`GG` 代表**绿色**,`BB` 代表**蓝色**,值范围从 **00** 到 **FF**。

例如,`#FF0000` 将是**红色**,`#00FF00` 将是**绿色**,同样,`#FFFFFF` 将是**白色**,`#000000` 将是**黑色**。

让我们看看如何在 R 语言中将十六进制值作为颜色实现:

# create a vector named temp
temp <- c(5,7,6,4,8)

# using hex value #c00000
barplot(temp, col="#c00000", main="#c00000")

# using hex value #AE4371
barplot(temp, col="#AE4371", main="#AE4371")

输出

Using Hex Values as Colors in R Output
使用十六进制值作为颜色

在上面的例子中,我们在 `barplot()` 函数中为 `col` 参数传递了十六进制值。

这里,

  • `#c00000` - 这个十六进制值由 **75.3%** 红色、**0%** 绿色和 **0%** 蓝色组成
  • `#AE4371` - 这个十六进制值由 **68.24%** 红色、**26.27%** 绿色和 **44.31%** 蓝色组成

在 R 语言中使用 RGB 值着色图表

R 语言中的 `rgb()` 函数允许我们指定红、绿、蓝分量,其值在 **0** 到 **1** 之间。

此函数返回上面讨论的相应十六进制代码。例如,

rgb(0, 1, 0) # prints "#00FF00"

rgb(0.3, 0.7, 0.9) # prints "#4DB3E6"

我们可以直接将 `rgb()` 传递给 `col` 参数,如下所示:

# create a vector named temp
temp <- c(5,7,6,4,8)

# using rgb() to color barplot
barplot(temp, col = rgb(0.3, 0.7, 0.9), main="Using RGB Values")

输出

Using RGB Values as Colors in R Output
使用 RGB 值作为颜色

在这里,我们已将 `rgb()` 传递给 `barplot()` 中的 `col` 参数。

因此,图表根据 rgb 值着色。


R 语言中的颜色循环

我们可以通过提供一个颜色向量来为条形图的每个条形着色不同的颜色。

如果提供的颜色数量少于条形图的数量,则颜色向量会循环使用。例如,

# create a vector named temp
temp <- c(5,7,6,4,8)

# color with 5 different colors
barplot(temp, col=c("red", "coral", "blue", "yellow", "pink"), main="With 5 Colors")

# color with 3 different color, last two bars will be recycled
barplot(temp, col=c("red", "coral", "blue"), main="With 3 Color")

输出

Color Cycle Output
颜色循环

在上面的示例中,我们首先通过为 **5** 个不同的条形提供 **5** 种颜色的向量来为条形图的每个条形着色。

对于第二个条形图,我们提供了 **3** 种不同颜色的向量,因此颜色在最后 **2** 个条形中循环使用。


在 R 语言中使用调色板

R 语言编程提供了 4 种内置调色板,可用于快速生成所需长度的颜色向量。

它们是:`rainbow()`、`heat.colors()`、`terrain.colors()` 和 `topo.colors()`。我们传入我们想要的颜色数量。

让我们看一下示例:

# use rainbow() to generate color palette
rainbow(5)

# Output: "#FF0000FF" "#CCFF00FF" "#00FF66FF" "#0066FFFF" "#CC00FFFF"

请注意,这里的十六进制数字有 **8** 位长。最后两位数字是透明度级别,其中 `FF` 是不透明的,`00` 是完全透明的。

示例:在 R 语言中使用调色板

# create a vector named temp
temp <- c(5,7,6,4,8)

# using rainbow()
barplot(temp, col=rainbow(5), main="rainbow")

# using heat.colors()
barplot(temp, col=heat.colors(5), main="heat.colors")

# using terrain.colors()
barplot(temp, col=terrain.colors(5), main="terrain.colors")

# using topo.colors()
barplot(temp, col=topo.colors(5), main="topo.colors")

输出

Color Palette
调色板

在这里,我们使用了 **4** 种内置调色板,可用于快速生成所需长度的颜色向量。

你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战