##创建图形
#最基本函数plot()
plot(c(1,2,3),c(1,2,4))
plot(c(-3,3),c(-1,5),type="n",xlab="x",ylab="y") #no point or line
#更改type参数值
plot(c(-3,3),c(-1,5),type="p",xlab="x",ylab="y") #points(默认值)
plot(c(-3,3),c(-1,5),type="l",xlab="x",ylab="y")
plot(c(-3,1,3),c(-1,2,5),type="b",xlab="x",ylab="y") #both points and lines
plot(c(-3,1,3),c(-1,2,5),type="o",xlab="x",ylab="y") #overplotted points and lines
plot(c(-3,0,3),c(-1,3,5),type="h",xlab="x",ylab="y") #histogram-like vertical lines
#添加线条 abline() /lines()
x <- c(1,2,3)
y <- c(1,3,8)
plot(x,y)
lmout <- lm(y~x)
abline(lmout)
abline(-4,5) #截距和斜率
abline(-2,6)
abline(a=1,b=2,col=4) #截距为1,斜率为4的一条蓝色的线
abline(h=1:5,v=2,col="pink",lty=2) #h 水平线 v垂直线 #lty线的类型
abline(h=1:5,v=1:3,col="pink",lty=4)
lines(x,y,col=2) #线穿点
plot(x,y,type="o")
plot(x,y,type="l")
plot(x,y,type="l",lty=3,lwd=4) #lwd 宽度 #lty指定线条的类型
#新增一个绘图窗口
#windows()
windows()
par(bg="lightblue")
plot(x,y,type="l")
points(x,y,col=2)
windows()
par(bg="lightblue")
plot(x,y,type="o")
points(x,y,col="yellow")
#在一张图中绘制两条密度曲线
test <- matrix(0,8,2)
test[,1]=c(88,97,76,60,83,70,94,80)
test[,2]=c(55,49,59,66,82,92,62,71)
d1=density(test[,1],from=0,to=100)
d2=density(test[,2],from=0,to=100)
plot(d1,main="",xlab="")
lines(d2) #添加第二条曲线
#添加点 points()
points(20,0.02,pch="@")
points(40,0.01,pch=9)
points(20,0.004,pch=15,col="blue")
test <- matrix(0,8,3)
test[,1]=c(88,97,76,60,83,70,94,80)
test[,2]=c(55,49,59,66,82,92,62,71)
test[,3]=rep(0.01,8)
points(test[,1],test[,3],pch="+",col="red")
points(100,0.02,pch=15,col="red")
#添加图例
example(legend)
plot(d1,main="",xlab="",ylab="",col=2,lty=2)
lines(d2,col=4,lty=1)
legend(5,0.025,c("Examl","Exam1"),col=c(2,4),lty=c(2,1),text.col = "green4")
#pch=c(1,4)
#添加文字text()
plot(c(1,2,3),c(1,2,4))
text(2.5,3,"abc",col="pink")
plot(d1,main="",xlab="",ylab="",col=2,lty=2)
lines(d2,col=4,lty=1)
text(35,0.015,"Exam2",col="blue")
text(95,0.025,"Exam1",col="red")
#改变字符大小,cex选项
plot(c(1,2,3),c(1,2,4))
text(2,3,"abc")
text(1,3,"abc",cex=3) #正常字号的3倍
#改变坐标轴的范围,xlim和ylim选项
plot(c(0,100),c(0,0.03),type="n",xlab="score",ylab="density")
lines(d2)
lines(d1)
plot(c(1:8),c(88,97,76,60,83,70,94,80))
plot(c(1:8),c(88,97,76,60,83,70,94,80),xlim=c(0,10),ylim=c(0,100))
dose <- c(10,20,30,40,50,60)
drugA <- c(12,15,20,28,50,60)
drugB <- c(10,12,17,22,32,40)
plot(dose,drugA,type="b",pch=15,lty=1,col="#df536b",lwd=2,xlim = c(0,60),ylim = c(0,70),xlab="Dosage",ylab="Drug Response")
abline(h=30,lty=2,col="gray") #添加水平线
lines(dose,drugB,type="b",pch=17,lty=2,col="#2497e5",lwd=2)
#添加图例
legend(5,70,c("A","B"),col=c("#df536b","#2497e5"),title="drug type",lty=c(1,2),pch=c(15,17))
#添加标题
title("Clinical Trails for Drug X")
网友评论