R语言绘图
文章目录
一、常用图形参数
1.颜色
代码如下(示例):
#3.1.1颜色
#对women数据集绘制散点图,并用红色表示散点。
head (women)
nrow (women)
str (women)
?plot
plot (women,col="red")#通过颜色名称
plot (women,col=554)#通过颜色下标
plot(women,col="#FFO000")#通过十六进制的颜色值
mycolor <- rgb(red= 255,green=0,blue=0,max=255)
plot (women,col=maycolor)#通过RGB值
#对其他图形参数颜色进行设置
plot (women,main="身高vS体重散点图" , sub="数据来源:women数据集",
col="red" ,col.main="green" , col.sub="blue",
col.axis="grey" ,col.lab="yellow")
colors()
#主题顾色
par(mfrow=c(3, 2))
?barplot
barplot(rep(1,7),col=rainbow(7),main="barplot(rep(1,7),col=rainbow(7))")
barplot (rep(1,7),col=heat.colors (7),main="barplot(rep(1,7), col=heat.colors(7))")
barplot(rep(1,7),col=terrain.colors(7),main="barplot(rep(1,7),col=terrain.colors(7))")
barplot(rep(1,7),col=topo.colors(7),main="barplot (rep(1,7),col=topo.colors (7))")
barplot(rep(1,7),col=cm.colors (7),main="barplot(rep(1,7),col=cm.colors(7))")
#par (mfrow=c(1,1))
2.文字元素
代码如下(示例):
#字体
plot(0: 4,type="n",axes =E,xlab=NA,ylab = NA)
plot(0:4,type="n" ,axes = T,xlab= 'x',ylab= 'y')
type <- c("正常字体(默认)","粗体字体","斜体字i","粗斜体字体")
?text
for(i in i:4){
text(2,5-i,labels = paste0("font=",i,": ",type[i]),font = i)
}
#大小
plot(0: 5,type="n",axes = F,xlab=NA,ylab = NA)
text(2,5,labels="cex=0.5:放太0.5倍",cex=0.5)
text(2,4,labels="cex=0.8:放大o.8倍",cex=0.8)
text(2,3,labels="cex=1(默认):正常大小",cex=1)
text(2,2,labels="cex=1.5:放大1.5倍",cex=1.5)
text(2,1,labels="cex=2:放大2倍", cex=2)
3.点元素
代码如下(示例):
plot (1,col="white",xlim=c(1,7),ylim=c(1,5),
main="点样式 pch=", xlab=NA,ylab=NA)
?points
for(i in c(0:25)){
x<-(i%/%5)*1+1
y<-6-(i%%5)-1
#if(length (which (c(21:25)==i)>=1)){
if(i>21){
points(x,y,pch=i,col="red",bg="yellow",cex