因子
sex<-c(1,2);sex
sex.f <- factor(sex);sex.f
str(sex.f)
stu.sex <- sample(1:2,10,replace= T);stu.sex
stu.sex.f<-factor(stu.sex);stu.sex.f
有序因子
stu.sex.ord <- ordered(stu.sex, levels = c(1,2));stu.sex.ord
stu.sex.ord<- ordered (stu.sex, levels = c(2,1));stu.sex.ord
cut函数
stu.r.score <- c(60,sample(60:100,8,replace = T),100);stu.r.score
stu.r.score.level <- cut(stu.r.score,breaks = 4);stu.r.score.level
tapply()函数
tapply(stu.r.score,stu.sex, mean)
#R内置数据集
data(package = .packages(all.available = TRUE))
head(airquality)
颜色
plot(women,col=“blue”) #名称
plot(women,col=554)#下标
plot(women,col="#FF0000") #十六进制的颜色值
mycolor <- rgb(red=0,green=0,blue=255,max=255)
plot(women,col=mycolor)#RGB值
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))”)
文字元素
plot(0:4,type=“n”,axes = F ,xlab = NA ,ylab = NA)
plot(0:4,type=“n”,axes = T ,xlab = ‘x’ ,ylab = ‘y’)
type <- c(“正常字体(默认)”,“粗体字体”,“斜体字体”,“粗斜体字体”)
?text
for (i in 1: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,3,labels=“cex=1(默认):正常大小”,cex=1)
text(2,2,labels=“cex=1.5:放大1.5倍”,cex=1.5)
点元素
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=2)
}else{
points(x,y,pch=i,cex=2)
}
text(x+0.2,y,labels = i)
}
网格线
op <- par(mfcol=1:2)
barplot(VADeaths,beside =TRUE,col=cm.colors(5),
main=“plot vADeaths with grid()”)
grid()
barplot(VADeaths,beside =TRUE,col=cm.colors(5),
main=“plot vADeaths with grid(NA,7,lty=2,lwd=1.5,col=‘green’)”)
grid(NA,7,lty=2,lwd=1.5,col=“green”)
par(mfcol=c(1,1))
点
set.seed(1234)
stackloss <- c(rnorm( 70,mean=0,sd=1),rnorm(3,mean=4,sd=1))
head(stackloss)
length(stackloss)
str(stackloss)
summary(stackloss)
boxplot(stackloss,col=“purple”,ylim=c(-4,5),outline=F)
points (rep(1,3),stackloss[70:72],pch=21,bg=c(“blue”,“yellow”,“green”),cex=1.2)
stackloss[70:72]
text(rep(1,3),stackloss[70:72],pos=3,labels = paste0(“异常值”,round(data[70:72],3)))