zl程序教程

您现在的位置是:首页 >  其他

当前栏目

R语言中的颜色(二)

语言 颜色
2023-06-13 09:13:02 时间

在上一期☞R语言中的颜色(一)中,我给大家介绍了R中的颜色以及自带的一些配色方案。这一期我给大家介绍一下gplots这个R包中的配色方案。

gplots包里面也包含了一些颜色相关的函数

  • colorpanel(n, low, mid, high)
  • redgreen(n)
  • greenred(n)
  • bluered(n)
  • redblue(n)

我们结合一个具体的例子来看下,这里的n是要生成的颜色的个数

#如果没有安装gplots这个包,需要先去掉下一行中的#,再运行进行安装
#install.packages("gplots")
library(gplots)
#两行两列,摆放四张图,各生成20个颜色
par(mfrow = c(2, 2))
#红色到蓝色渐变
pie(rep(1, 20), col = redblue(20), main = "redblue")
#蓝色到红色渐变
pie(rep(1, 20), col = bluered(20), main = "bluered")
#红色到绿色渐变
pie(rep(1, 20), col = redgreen(20), main = "redgreen")
#绿色到红色渐变
pie(rep(1, 20), col = greenred(20), main = "greenred")

接下来我们看看colorpanel的使用,这里n是要生成的颜色的个数,low是起始颜色,mid是中间的颜色,high是最终的颜色。也就是从low-mid-high的一个渐变。

library(gplots)
#一行两列,可以摆放两张图
par(mfrow = c(1, 2))
#绿色到红色渐变,生成20个颜色
pie(rep(1, 20), col = colorpanel(20,low="green",high="red"), main = "greenred")
#绿色到黄色再到红色的渐变,生成20个颜色
pie(rep(1, 20), col = colorpanel(20,low="green",mid = "yellow", high="red"), main = "greenyellowred")

今天的分享就先到这里,敬请期待下一期内容!

参考资料:

1.R语言中的颜色(一)