转自该网站:http://research.stowers-institute.org/efg/R/Color/Chart/

科学可视化中常用的一些颜色表:http://geog.uoregon.edu/datagraphics/color_scales.htm

Step-by-Step Procedure (to learn about "colors")

1.  The function call, colors(), or with the British spelling, colours(), returns a vector of  657 color names in R.  The color names are in alphabetical order, except forcolors()[1], which is "white".  The names "gray" and "grey" can be spelled either way -- many shades of grey/gray are provided with both spellings.

2.  Particular color names of interest can be found if their positions in the vector are known, e.g.,

> colors()[c(552,254,26)]
[1] "red" "green" "blue"

3.  grep can be used to find color names of interest, e.g.,

> grep("red",colors())
[1] 100 372 373 374 375 376 476 503 504 505 506 507 524 525 526 527 528 552 553
[20] 554 555 556 641 642 643 644 645

> colors()[grep("red",colors())]
[1] "darkred" "indianred" "indianred1" "indianred2" 
[5] "indianred3" "indianred4" "mediumvioletred" "orangered" 
[9] "orangered1" "orangered2" "orangered3" "orangered4" 
[13] "palevioletred" "palevioletred1" "palevioletred2" "palevioletred3" 
[17] "palevioletred4" "red" "red1" "red2" 
[21] "red3" "red4" "violetred" "violetred1" 
[25] "violetred2" "violetred3" "violetred4"

> colors()[grep("sky",colors())]
[1] "deepskyblue" "deepskyblue1" "deepskyblue2" "deepskyblue3" 
[5] "deepskyblue4" "lightskyblue" "lightskyblue1" "lightskyblue2"
[9] "lightskyblue3" "lightskyblue4" "skyblue" "skyblue1" 
[13] "skyblue2" "skyblue3" "skyblue4"

4.  The function col2rgb can be used to extract the RGB (red-green-blue) components of a color, e.g.,

> col2rgb("yellow")
[,1]
red 255
green 255
blue 0

Each of the three RGB color components ranges from 0 to 255, which is interpreted to be 0.0 to 1.0 in RGB colorspace.  With each of the RGB components having 256 possible discrete values, this results in 256*256*256 possible colors, or 16,777,216 colors.

While the RGB component values range from 0 to 255 in decimal, they range from hex 00 to hex FF.  Black, which is RGB = (0,0,0) can be represented in hex as #000000, and white, which is RGB = (255,255,255), can represented in hex as #FFFFFF.

5.  R provides a way to define an RGB triple with each of the color components ranging from 0.0 to 1.0 using the rgb function.  For example, yellow can be defined:

> rgb(1.0, 1.0, 0.0)
[1] "#FFFF00"

The output is in hexadecimal ranging from 00 to FF (i.e., decimal 0 to 255) for each color component.  The 0.0 to 1.0 inputs are a bit odd, but are standard in RGB color theory.  Since decimal values from 0 to 255 are common, the rgb function allows a maxColorValue parameter as an alternative:

> rgb(255, 255, 0, maxColorValue=255)
[1] "#FFFF00"

The R function, GetColorHexAndDecimal, was written to display both hex and decimal values of the color components for a given color name:

GetColorHexAndDecimal <- function(color)
{
  c <- col2rgb(color)
  sprintf("#%02X%02X%02X %3d %3d %3d", c[1],c[2],c[3], c[1], c[2], c[3])
}

Example:

> GetColorHexAndDecimal("yellow")
[1] "#FFFF00 255 255 0"

This GetColorHexAndDecimal function will be used below in Step 9.

6.  Text of a certain color when viewed against certain backgrounds can be very hard to see, e.g., never use yellow text on a white background since there isn't good contrast between the two.  One simple hueristic in defining a text color for a given background color is to pick the one that is "farthest" away from "black" or "white".  One way to do this is to compute the color intensity, defined as the mean of the RGB triple, and pick "black" (intensity 0) for text color if the background intensity is greater than 127, or "white" (intensity 255) when the background intensity is less than or equal to 127.

The R function below, SetTextContrastColor, gives a good text color for a given background color name:

SetTextContrastColor <- function(color)
{
  ifelse( mean(col2rgb(color)) > 127, "black", "white")
}

# Define this array of text contrast colors that correponds to each
# member of the colors() array.
TextContrastColor <- unlist( lapply(colors(), SetTextContrastColor) )

Examples:

> SetTextContrastColor("white")
[1] "black"
> SetTextContrastColor("black")
[1] "white"
> SetTextContrastColor("red")
[1] "white"
> SetTextContrastColor("yellow") 
[1] "black"

7.  The following R code produces the "R Colors" graphic shown at the top of this page (using TextContrastColor defined above):

# 1a. Plot matrix of R colors, in index order, 25 per row.
# This example plots each row of rectangles one at a time.
colCount <- 25 # number per row
rowCount <- 27

plot( c(1,colCount), c(0,rowCount), type="n", ylab="", xlab="",
  axes=FALSE, ylim=c(rowCount,0))
title("R colors")

for (j in 0:(rowCount-1))
{
  base <- j*colCount
  remaining <- length(colors()) - base
  RowSize <- ifelse(remaining < colCount, remaining, colCount)
  rect((1:RowSize)-0.5,j-0.5, (1:RowSize)+0.5,j+0.5,
    border="black",
    col=colors()[base + (1:RowSize)])
  text((1:RowSize), j, paste(base + (1:RowSize)), cex=0.7,
    col=TextContrastColor[base + (1:RowSize)])
}

8. Alphabetical order is not necessarily a good way to find similar colors.  The RGB values of each of the colors() was converted to hue-saturation-value (HSV) and then sorted by HSV.  This approach groups colors of the same "hue" together a bit better.  Here's the code and graphic produced:

# 1b. Plot matrix of R colors, in "hue" order, 25 per row.
# This example plots each rectangle one at a time.
RGBColors <- col2rgb(colors()[1:length(colors())])
HSVColors <- rgb2hsv( RGBColors[1,], RGBColors[2,], RGBColors[3,],
             maxColorValue=255)
HueOrder <- order( HSVColors[1,], HSVColors[2,], HSVColors[3,] )

plot(0, type="n", ylab="", xlab="",
axes=FALSE, ylim=c(rowCount,0), xlim=c(1,colCount))

title("R colors -- Sorted by Hue, Saturation, Value")

for (j in 0:(rowCount-1))
{
  for (i in 1:colCount)
  {
   k <- j*colCount + i
   if (k <= length(colors()))
   {
    rect(i-0.5,j-0.5, i+0.5,j+0.5, border="black", col=colors()[ HueOrder[k] ])
    text(i,j, paste(HueOrder[k]), cex=0.7, col=TextContrastColor[ HueOrder[k] ])
   }
  }
}

9.  While the color matrices above are useful, a more useful display would include a rectangular area showing the color, the color index, the color name, and the RGB values, both in hexadecimal, which is often used in web pages.

The code for this is a bit tedious -- see Item #2 in the ColorChart.R code for complete details. Here is the first page of the Chart of R colors.

PDF of 7-page "Chart of R colors"

10.  To create a PDF file (named ColorChart.pdf) with all the graphics shown on this page, issue this R command:

source("http://research.stowers-institute.org/efg/R/Color/Chart/ColorChart.R")

【转】R语言笔记--颜色的使用的更多相关文章

  1. R语言笔记

    R语言笔记 学习R语言对我来说有好几个地方需要注意的,我觉得这样的经验也适用于学习其他的新的语言. 语言的目标 我理解语言的目标就是这个语言是用来做什么的,为什么样的任务服务的,也就是设计这个语言的动 ...

  2. R语言笔记4--可视化

    接R语言笔记3--实例1 R语言中的可视化函数分为两大类,探索性可视化(陌生数据集,不了解,需要探索里面的信息:偏重于快速,方便的工具)和解释性可视化(完全了解数据集,里面的故事需要讲解别人:偏重全面 ...

  3. R语言笔记完整版

    [R笔记]R语言函数总结   R语言与数据挖掘:公式:数据:方法 R语言特征 对大小写敏感 通常,数字,字母,. 和 _都是允许的(在一些国家还包括重音字母).不过,一个命名必须以 . 或者字母开头, ...

  4. R语言笔记:快速入门

    1.简单会话 > x<-c(1,2,4) > x [1] 1 2 4 R语言的标准赋值运算符是<-.也可以用=,不过不建议用它,有些情况会失灵.其中c表示连接(concaten ...

  5. 初探R语言——R语言笔记

    R语言使用 <-  赋值 # 作为注释符号 c()函数用于作为向量赋值,例如age<-c(1,2,3,4,5) mean()用于求向量的平均值 sd()求向量的标准差 cor(a,b)求a ...

  6. R语言笔记5--读数据

    1.读文本文件数据 (1)先设置工作目录,把文本文件放于该目录下 备注:在记事本里写完数据后,按一下回车,负责在R语言中出现错误 (2)读剪贴板 文本或EXCEL的数据均可通过剪贴板操作 (3)读ex ...

  7. R语言笔记1--向量、数组、矩阵、数据框、列表

    注释:R语言是区分大小写的 1.向量 R语言中可以将各种向量赋值为一个变量,这种赋值操作符就是等号“=”,也可以使用“<-”. 1)产生向量 (1)函数c() 例如:x1=c(2,4,6,8,0 ...

  8. R语言笔记2--循环、R脚本

    1.循环语句 for语句 while语句 2.R脚本 source()函数 print()函数

  9. r语言笔记 jn

    get_range <- function(data_name , row_name){ library(stringr) load(data_name) data_str <- str_ ...

随机推荐

  1. CSS布局:Float布局过程与老生常谈的三栏布局

    原文见博客主站,欢迎大家去评论. 使用CSS布局网页,那是前端的基本功了,什么两栏布局,三栏布局,那也是前端面试的基本题了.一般来说,可以使用CSSposition属性进行布局,或者使用CSSfloa ...

  2. 发现新大陆-JMX

    今天接触到这个东西,觉得好有趣,可以用很多第三方的显示层jar包直接在UI界面上操作指定的java对象,网上将这个东西的也挺多的,我个人觉得这个比webServer还强大了.webserver只是公布 ...

  3. Nginx--Windows环境下Nginx+tomcat配置(包括动静分离)

    前提条件: (1)已安装好tomcat,且能成功启动 (2)已安装好Nginx,且能成功启动 接下来进行配置: (1)在Nginx的conf文件夹中新增两个文件,分别如下:(新建文件后,直接复制代码即 ...

  4. DDD:如何更好的使用值对象

    背景 大师们让我们多使用“值语义”的对象(并非一定是是值对象),我们工作中也没有少使用(int.bool.date等等),只是大多数人都没有多的自定义“值语义”的类型(我也其中之一),本文不说其它的, ...

  5. 初探KMP算法

            数据结构上老师也没讲这个,平常ACM比赛时我也没怎么理解,只是背会了代码--前天在博客园上看见了一篇介绍KMP的,不经意间就勾起了我的回忆,写下来吧,记得更牢. 一.理论准备      ...

  6. 加快MySQL逻辑恢复速度的方法和参数总结

    日常工作中经常会有需要从mysqldump导出的备份文件恢复数据库的情况,相比物理备份恢复这种方式在恢复时间上往往显得力不从心. 本文就总结了几个对于逻辑备份恢复有加速作用的参数和操作 注意:我们的大 ...

  7. Sylius – 100% 免费和开源的电子商务解决方案

    Sylius 项目提供了一个完整的电子商务解决方案.您将学习如何掌握它,帮助你在下一个项目中能够更快速的开发.Sylius 提供了一个完整的在线商店演示:demo.sylius.com. 您可能感兴趣 ...

  8. Direct2D开发:纹理混合

    转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 我们都知道Direct2D可以加载并显示图片,但是不知道你有没有想过,这个2D的图形引擎可以进行纹理混合吗?如果 ...

  9. UWP开发入门(十九)——10分钟学会在VS2015中使用Git

    写程序必然需要版本控制,哪怕是个人项目也是必须的.我们在开发UWP APP的时候,VS2015默认提供了对微软TFS和Git的支持.考虑到现在Git很火,作为微软系的程序员也不得不学一点防身,以免被开 ...

  10. NVelocity的基本用法

    NVelocity常用语法指令 默认情况下,NVelocity解析是不分大小写的,当然可以通过设置runtime.strict.math=true,采用严格解析模式. 严格区分大小写有时候还是挺有用途 ...