R語言實戰學習筆記-R語言繪圖基礎


 

本文將從以下幾個方面介紹R語言繪圖基礎:1.簡單實例 2.圖形參數 3.添加文本、自定義坐標軸和圖例 4.圖形的組合

1.簡單實例 

#繪制第一個圖形
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60
plot(dose,drugA,type="b")

2.圖形參數

圖形中常用的參數:

以下為運用以上參數繪制圖形的實例:

#繪制圖形
dose<-c(20,30,40,45,60)

drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
plot(dose,drugA,pch=5,type="b",cex=1.5,lty=1,lwd=2,col="red", #設置線型
col.axis="blue",col.lab="green",col.main="pink",col.sub="orange",fg="linen",bg="SeaGreen1", #設置顏色
cex.axis=1.5,cex.lab=2,cex.main=2,cex.sub=1.5, #設置標簽縮放倍數
font.axis=2,font.lab=1,font.main=4,font.sub=2,ps=2,family="serif", #設置標簽的字體樣式
pin=c(2,1),mai=c(1,0.5,2,0.2)) #設置圖形的尺寸

3.圖形的標題、坐標軸信息添加

#圖形實例
dose<-c(20,30,40,45,60)
drugA<-c(16,20,24,27,45)
plot(dose,drugA,type="b",
col="red",lty=2,pch=2,lwd=2,
main="Clinical Trials for DrugA",
sub="This is hypothetical data",
xlab="Dosage",ylab="Drug Response",
xlim=c(10,60),ylim=c(0,70))

3.1自定義標題和坐標軸

#添加自定義標題和坐標軸
title(main="main title",sub="subtitle",xlab="x-axis",ylab="y-axis label"
axis(side,at=,labels=,pos=,lty=,col=,las=,tck=,……)

#自定義坐標軸
x<-c(1:10) y<-x z<-10/x opar<-par(no.readonly=TRUE) par(mar=c(5,4,4,8)+0.1) #增加邊界大小 plot(x,y,type="b",pch=21,yaxt="n",lty=3,ann=FALSE) #繪制x對y的圖形 lines(x,z,type="b",pch=22,col="blue",lty=2) #繪制x對1/x的線 axis(2,at=x,labels=x,col.axis="red",las=2) #繪制針對y=x自定義坐標軸 axis(4,at=z,labels=round(z,digits=2),col.axis="blue",las=2,cex.axis=0.7,tck=-0.01) #繪制針對y=1/x的坐標軸 mtext("y=1/x",side=4,line=3,cex.lab=1,las=2,col="blue") #添加標題 title("An Example of Creative Axes", xlab="X Value", ylab="Y=X") par(opar)

3.2添加參考線、圖例

#繪制圖例
dose<-c(20,30,40,45,60) drugA<-c(16,20,27,40,60) drugB<-c(15,20,27,31,40) opar<-par(no.readonly=TRUE) par(lwd=2,cex=1.5,font.lab=2) plot(dose,drugA,type="b",pch=15,lty=1,col="red",ylim=c(0,60),main="Drug A VS Drug B",xlab="Drug Dosage",ylab="Drug Response") lines(dose,drugB,type="b",pch=17,lty=2,col="blue") abline(h=c(30),lwd=1.5,lty=2,col="gray") library(Hmisc) minor.tick(nx=3,ny=3,tick.ratio=0.5) legend("topleft",cex=0.5,inset=0.05,title="Drug Type",c("A","B"),lty=c(1,2),pch=c(15,17),col=c("red","blue"))#添加圖例 par(opar)

3.2添加文本

#添加文本
attach(mtcars) plot(wt,mpg,main="Mileage vs Car Weight", xlab="Weight",ylab="Mileage", pch=18,col="blue") text(wt,mpg,row.names(mtcars),cex=0.6,pos=4,col="red")#添加標簽 detach(mtcars)

4.圖形的組合

#圖形組合,
attach(mtcars) opar<-par(no.readonly=TRUE) par(mfrow=c(2,2))#繪制2*2四個圖形區域 plot(wt,mpg,main="Scatterplot of wt vs mpg") plot(wt,disp,main="Scatterplot of wt vs disp") hist(wt,main="Histogram of wt") boxplot(wt,main="Boxplot of wt") par(opar) detach(mtcars)

#創建3*1的圖形組合
attach(mtcars) opar<-par(no.readonly=TRUE) par(mfrow=c(3,1)) hist(wt) hist(mpg) hist(disp) par(opar) detach(mtcars)

#精確控制圖形布局大小
attach(mtcars) layout(matrix(c(1,1,2,3),2,2,byrow=TRUE),widths=c(1.5,1),heights=c(1,2))#第二行中左邊圖形的寬度是右邊的1.5倍,第二行的圖形高度是第一行的2倍 hist(wt) hist(mpg) hist(disp) detach(mtcars)

以上為R語言中圖形中的基本信息,后續針對學習基本圖形的畫法。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM