码迷,mamicode.com
首页 > 编程语言 > 详细

R语言-图的要素颜色

时间:2018-11-30 13:55:29      阅读:914      评论:0      收藏:0      [点我收藏+]

标签:color   span   orb   head   mat   yellow   范围   com   背景   

1.设置图形要素的颜色

Plot函数中,使用col=参数来决定要素的颜色 
如果不指定plot type,颜色加在散点上,如果指定了plot type,例如line,则颜色加在线上 
其它函数,例如barplot()和histogram()也使用col=参数影响颜色

2.颜色的表示

①颜色名

> colors()                             #查看所有颜色名
> plot(rnorm(1000),col="yellow") 

技术分享图片

②数值

> palette()                                     #查看当前调色板 (缺省调色板下,1表示黑色,2表示红色,0表示背景色)
> plot(rnorm(1000),col="2")                     #数值为当前调色板上的颜色值 
> palette(c("red","blue","green","orange"))     #改变缺省调色板 
> palette()
> plot(rnorm(1000),col="2") 

技术分享图片                                                   技术分享图片

③十六进制

> rgb(0.5,0.5,0.5,0.8)                 #设置红色,绿色,蓝色,透明度的数值(范围为0-1)返回16进制表示
> plot(rnorm(1000),col="#808080CC")    #col参数为上一步的结果 

技术分享图片

3.多种颜色画图

①使用调色板控制包

> library(RColorBrewer)                #加载调色板控制包
> display.brewer.all()                 #查看所有色板
> brewer.pal(7,"YlOrRd")               #替换缺省调色板
> display.brewer.pal(7,"YlOrRd")       #画图

技术分享图片                            技术分享图片

②使用颜色名

> sales<-read.csv("citysales.csv",header=TRUE) 
> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City,                                  #增加图例
+ col=c("red","blue","green","orange","pink"),        #颜色数和样本数相等
+ border="white") 

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=c("red","blue","green","orange"),              #颜色数和样本数不相等
+ border="white") 

技术分享图片                                 技术分享图片

③使用函数

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=heat.colors(length(sales$City)), 
+ border="white") 

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=rainbow(length(sales$City)), 
+ border="white") 

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=terrain.colors(length(sales$City)), 
+ border="white") 

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=cm.colors(length(sales$City)), 
+ border="white") 

> barplot(as.matrix(sales[,2:4]), beside=T, 
+ legend=sales$City, 
+ col=topo.colors(length(sales$City)), 
+ border="white") 

技术分享图片技术分享图片技术分享图片技术分享图片技术分享图片

 

R语言-图的要素颜色

标签:color   span   orb   head   mat   yellow   范围   com   背景   

原文地址:https://www.cnblogs.com/all1008/p/10043104.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!