#if else
score<-79
if(score>=80){
print(“A”)
}else if(score>=60){
print(“B”)
}else{
print(“C”)
}
#for
num<-1:10
for(i in num){
if(i%%2==0){
print(i)
}
}
#while
num<-5
while(num>0){
print(num)
num<-num-1
}
#定义函数
pow <- function(x,y){
result<-x^y
print(paste(x,"^",y,"=",result))
}
pow(2,3)
num<-1:10
my.fun<-function(x){
x+1
}
sapply(num,my.fun)
#求10以内整数平方
pow <- function(x){
result<-x^2
print(paste(x,"^",2,"=",result))
}
num<-1:10
for(i in num){
if(i%%2==0){
# sapply(i,pow)
pow(i)
}
}
data()
data(package=.packages(all.available = TRUE))
head(airquality)
?airquality
#颜色
head(women)
nrow(women)
str(women)
?plot
plot(women,col=“red”)#点图
plot(women,col=554)#颜色下标
plot(women, col="#FF0000")#颜色16进制
mycolor<-rgb(red=255,green=0,blue=0,max=255)
plot(women,col=mycolor)#通过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) )”)
#字体
plot(0:4,type=“n”,axes=F,xlab = NA,ylab = NA)
plot(0:4,type=“n”,axes=T,xlab = ‘x’,ylab = ‘y’)
type <-c(“正常字体(默认)”,“粗体字体”,“斜体字体”,“粗斜体字体”)
for(i in 1:4){text (2,5-i, labels =paste0(“font=”,i,":",type [i]),font =i)}
type <-c(“cex=0.5:放大0.5倍”,“cex=0.8:放大0.8倍”,“cex=1(默认):正常大小”,“cex=1.5:放大1.5倍”,“cex=2:放大2倍”)
cex<-c(0.5,0.8,1,1.5,2)
plot(0:5,type=“n”,axes=F,xlab = NA,ylab = NA)
for(i in 1:5){
text (2,5-i, labels =paste0(type[i]),cex=cex[i])}
iris
attach(iris)#加载到内存
par(mfrow=c(1, 2))
for(i in c(FALSE,TRUE)){
barplot (VADeaths,horiz = i,beside = T,col= rainbow(5))}
par(mfrow=c (1, 1))
data()
attach(OrchardSprays)
dev.new()
OrchardSprays
chickwts
head(chickwts)
attach(chickwts)
dev.new()
boxplot(weight~feed,col=heat.colors(3),
main=list(“不同饮食种类对小鸡生长速度的影响”,
font=4,col=“green”,cex=1.5),
sub=list(“数据来源:chickwts数据集”,font=3,
col=“yellow”,cex=0.8),
xlab=“treatment”,ylab=“decrease”)
barplot(VADeaths,beside = T,col=cm.colors (5))
legend (“top”,legend-rownames(VADeaths),ncol-5, fil1=cm.colors (5),bty=“n”)
op <-par(mfcol=1:2)
a<-as.matrix(chickwts)
str(a)
barplot(a,beside = TRUE,col=cm.colors (6),
main=“plot VADeaths with grid()”)
grid()
barplot(VADeaths,beside = TRUE, col=cm.colors(5),main="plot VADeaths with grid (NA, 7, 1ty=2, 1wd=1.5, col=‘green’) ")
grid(NA,7,lty=2,lwd=1.5,col=“green”)
par(mfcol=c (1,1) )
dev.new()
set.seed(1234)
data <-c(rnorm (200, mean=0, sd=1),rnorm (3, mean=4, sd=1))
head(data)
length(data)
str(data)
summary(data)
boxplot(data,col=“violet”, ylim=c(-4,5),outline=F)
points (rep (1,3),data [201:203],pch=21,bg=c(“blue”, “red”, “black”),cex=1.2)
data[201:203]
text(rep(1,3),data [201:203] ,pos=4,label=paste0 (“异常值”,round (data [201:203],3)))
#install.packages(“ggplot2”)
data (economics, package =“ggplot2”)
attach(economics)
plot(date,psavert,type=“l”, ylab="", ylim=c (0,26))
lines (date, uempmed, type=“l”, col=“blue”)
detach(economics)#移除
attach(iris)
plot (Petal.Length~Petal.Width)
abline (h=mean (Petal.Length) ,col=“red”)#加水平线
abline (v=mean (Petal.Width) ,col=“red”)#加水平线
abline(lm(Petal.Length~Petal.Width),col=“blue”, lwd=2, lty=3)#拟合的线
detach (iris)
#高级绘图 散点
par(mfrow=c(1,2))
plot(x=rnorm(10))
plot(women)
par(mfrow=c(1,1))
plot(iris[1:4], main=“利用plot函数绘制散点图矩阵”)
pairs(iris[1:4], main=“利用pairs画数绘制散点图矩阵”)
pairs(~Sepal.Length+Sepal.Width+Petal.Length+Petal.Width,data=iris, main=“利用pairs函数绘制散点图矩阵”)