02.07.2013 Views

现代统计图形 - 科学网—博客

现代统计图形 - 科学网—博客

现代统计图形 - 科学网—博客

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4.1 颜色 35<br />

1 > pdf("C:/colors-bar.pdf", height = 120)<br />

2 > par(mar = c(0, 10, 3, 0) + 0.1, yaxs = "i")<br />

3 > barplot(rep(1, length(colors())), col = rev(colors()),<br />

4 + names.arg = rev(colors()), horiz = TRUE, las = 1,<br />

5 + xaxt = "n", main = expression("Bars of colors in" ~<br />

6 + italic(colors())))<br />

7 > dev.off()<br />

palette() 调色板函数;用法palette(value),这个函数用来设置调色板或<br />

者获得调色板颜色值;注意,实际上这个函数的结果可能并非“固定”<br />

颜色,但是只要设定好了调色板,它的取值就不会再改变(直到下一<br />

次重新设定调色板)。如果不写任何参数,那么该函数返回当前的调色<br />

板设置,即一个包含当前调色板中所有颜色的向量;若参数长度为1则<br />

将当前调色板重新设置为以该参数为名称的调色板,目前这种参数只<br />

有’default’这一种,即设置为默认调色板:palette(’default’);<br />

若参数为一个颜色向量,那么将当前调色板中的颜色更改为该参数表<br />

示的颜色。如下例:<br />

1 > # 默认的调色板颜色<br />

2 > palette()<br />

[1] "black" "red" "green3" "blue" "cyan" "magenta"<br />

[7] "yellow" "gray"<br />

1 > # 重新设置调色板为colors()的前10种颜色<br />

2 > palette(colors()[1:10])<br />

3 > # 更改后的调色板颜色<br />

4 > palette()<br />

[1] "white" "aliceblue" "antiquewhite"<br />

[4] "antiquewhite1" "antiquewhite2" "antiquewhite3"<br />

[7] "antiquewhite4" "aquamarine" "aquamarine"<br />

[10] "aquamarine2"<br />

1 > # 恢复默认调色板<br />

2 > palette("default")<br />

调色板的好处在于,我们可以在R中使用一个整数来表示颜色,而这<br />

个整数对应的颜色就是调色板中相应位置的颜色,比如在某作图函数<br />

中调用参数col = 2表示取调色板中第2种颜色。若整数值超过了调色<br />

板颜色向量的长度,那么R会自动取该整数除以调色板颜色向量长度<br />

的余数。

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!