#设置图形要素的颜色(回顾) 

 #一、用颜色名 

 #1)Plot函数中,使用col=参数来决定要素的颜色 

 #2)如果指不定plot type,颜色加在散点上,如果指定了plot type,例如line,则颜色加在线上 

 #3)其它函数,例如柱形图:barplot()和直方图:histogram()也使用col=参数影响颜色 


 #二、使用数值表达颜色 

 #n为当前调色板上的颜色值 

 #缺省调色板下,1:红色;2:绿色;0:背景颜色 

 #palette()函数观看当前调色板 

 palette() 

 col=n 

 #改变缺省调色板 

 palette(c("red","blue","green","orange")) 

 palette() 

 #恢复成原来缺省的样子 

 palette("default") 

 palette() 


 #三、十六进制表达的颜色 

 #"#"-表示十六进制数;“AC"--前两组表示红色;”55“--表示绿色;”00“--表示蓝色;”BB“--alpha(透明度) 

 plot(rnorm(1000),col="#AC5500BB") 


 #函数rgb()可以根据输入颜色的亮度,透明度帮组转换成对应的十六进制颜色字符串 

 #颜色值从0-1;0--最暗的;1--最亮的 

 rgb(0.5,0.5,0.5,0.5) 


 #自动产生颜色函数heat.colors 

 heat.colors(5) 



 #直观的调色板控制宝RColorBrewer 

 #加载RColorBrewer包 

 library(RColorBrewer) 

 #显示所有的颜色 

 display.brewer.all() 

 #用包里的调色板来代替当前的调色板 

 #YlOrRd黄色画红色--红黄色系;7--指定调色板有7种颜色 

 brewer.pal(7,"YlOrRd") 

 display.brewer.pal(7,"YlOrRd") 


 sales<-read.csv("G:\\R_workspace\\sales.csv") 

 #这里用颜色表示不同的样本,(要求颜色数和样本数要相等,我自己验证好像不一样也可以,可以重复利用有定义的颜色) 

 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]),legend=sales$City, 

         col=c("red","blue","green"),border="white") 


 barplot(as.matrix(sales[,2:4]),legend=sales$City, 

         col=c("red","blue","green","orange","pink"),border="white") 

 #利用heat.colors()自动生成颜色 

 barplot(as.matrix(sales[,2:4]),beside=T,legend=sales$City, 

         col=heat.colors(length(sales$City)),border="white") 


 #除了heat.colors()以外,还有可以产生颜色的 

 #---rainbow() 

 barplot(as.matrix(sales[,2:4]),beside=T,legend=sales$City, 

         col=rainbow(length(sales$City)),border="white") 


 #--terrain.colors()--也可以产生颜色(这种方式产生的颜色比较素净,表现不强烈) 

 barplot(as.matrix(sales[,2:4]),beside=T,legend=sales$City, 

         col=terrain.colors(length(sales$City)),border="white") 


 #--cm.colors()--(这种方式产生的颜色比较素净,表现不强烈) 

 barplot(as.matrix(sales[,2:4]),beside=T,legend=sales$City, 

         col=cm.colors(length(sales$City)),border="white") 

 #--topo.colors()--(这种方式产生的颜色比较鲜明) 

 barplot(as.matrix(sales[,2:4]),beside=T,legend=sales$City, 

         col=topo.colors(length(sales$City)),border="white") 


 #设置背景颜色 

 #par()的作用知道画板被关闭 

 #par--parameter--设置画图参数的语句--当前画板的控制语句 

 #par中指定的颜色是全局的,但是如果plot中也指定了,就以plot中指定的颜色为准 

 #par的有效使用范围是当前画板上,如果想使这个设置失效有两种办法 

 #---1:用新的par覆盖它 

 #---2:直接关掉画板 

 par(bg="gray") 

 plot(rnorm(100)) 



 #只设置坐标系内的背景颜色 

 plot(rnorm(100),type="n") 

 #x的值就是图中大矩形的四个顶点的坐标 

 x<-par("usr") 

 #对四个顶点坐标所表示的区域进行背景颜色的设置 

 rect(x[1],x[3],x[2],x[4],col="lightgray") 

 #再在背景上标出散点 

 points(rnorm(1000)) 


 #设置标题、坐标轴标号等颜色 

 #col.axis="blue"---设置坐标轴刻度颜色 

 #col.lab="red"---设置横纵坐标轴标题颜色 

 #col.main="darkblue"--设置标题颜色 

 plot(rnorm(100),main="Plot Title",col.axis="blue",col.lab="red",col.main="darkblue") 


 #使用par()设置 

 par(col.axis="black",col.lab="#444444",col.main="darkblue") 

 plot(rnorm(100),main="Plot") 


 #使用title()函数 

 #---可以覆盖缺省的标题设置 

 #可以用于设置图画参数 

 title("Sales Figures for 2010",col.main="blue") 

 title(xlab="Month",ylab="Sales",col.lab="red") 

 title(xlab="X axis",col.lab="green") 

 title(ylab="Y axis",col.lab="blue")