天天看點

這好像是郭子的第三篇R部落格了

#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函數繪制散點圖矩陣”)