手動(dòng)輸入數(shù)據(jù)
mydata<-data.frame(age=numeric(0),gender=numeric(0),weight=numeric(0))
mydata<-edit(mydata)
#使用edit()可以手動(dòng)輸入數(shù)據(jù)芽丹,并且還可以改變列名什么的
#值標(biāo)簽
patientdata$gender<-factor(patientdata$gender,levels=c(1,2),labels=c("males","female"))
#使用levels和labels相互對應(yīng)婿牍,創(chuàng)建字符型向量的值標(biāo)簽```
#創(chuàng)建圖形
weight<-c(32,43,23,43)
mpg<-c(12,21,9,22)
mtcars<-data.frame(weight,mpg)
將繪圖語句夾在開啟圖形設(shè)備和關(guān)閉圖形設(shè)備之間即可輸出圖形
pdf("mygraph.pdf")
attach(mtcars)
plot(weight,mpg)
abline(lm(mpg~weight))
title("regression of mpg on weight")
detach(mtcars)
dev.off()```
簡單的繪圖例子
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
#"p" for points,"l" for lines,"b" for both
dev.new()
plot(dose,drugA,type="b")
#dev.new()必須在設(shè)置參數(shù)之前吸奴,要不然就會使用默認(rèn)參數(shù)重新繪圖
dev.new()
opar<-par(no.readonly=TRUE)
#這種是指定全局參數(shù)
par(lty=2,pch=17)
#指定坐標(biāo)軸標(biāo)簽字體為斜體,標(biāo)題為粗斜體
par(font.lab=3,cex.lab=1.5,font.main=4,cex.main=2)
plot(dose,drugA,type="b")
title("the influence of the drugA")
par(opar)
#為高級繪圖函數(shù)指定參數(shù)东抹,只對這幅圖有效
dev.new()
plot(dose,drugA,type="b",lty=2,lwd=2,pch=17,cex=1.5)
#lwd指定線寬秩冈,相對大小本缠,cex指定符號的大小,具體代表值查書45頁
dev.new()
plot(dose,drugA,type="b",lty=3,lwd=3,pch=15,cex=2)```
#指定顏色
n<-10
mycolors<-rainbow(n)
pie() draw a pie chart,labels are the names of the colors
dev.new()
pie(rep(1,n),labels=mycolors,col=mycolors)
rep(1,n) replicates the values in x for n times and generates a vector as [1,1,...,1]
mygrays<-gray(0:n/n)
dev.new()
pie(rep(1,n),labels=mygrays,col=mygrays)
first par in pie as a vector of non-negative numerical quantities.
and the values in x are displayed as the areas of pie slices.```
設(shè)置圖形尺寸pin,mai,mar
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,31,40)
#返回一個(gè)可以修改的當(dāng)前圖形參數(shù)的列表
opar<-par(no.readonly = TRUE)
#pin返回圖形大小
par(pin=c(2,3))
par(lwd=2,cex=1.5)
par(cex.axis=.75,font.axis=3)
plot(dose,drugA,type="b",pch=19,lty=2,col="red")
plot(dose,drugB,type = "b",pch=23,lty=6,col="blue",bg="green")
#bg是來設(shè)置繪制符號的背景色的
par(opar)```
#使用高級繪圖函數(shù)內(nèi)置參數(shù)設(shè)置標(biāo)題丹锹,坐標(biāo)軸等
plot(dose,drugA,type = "b",col="red",lty=2,pch=2,lwd=2,
main="Clinical Trials for Drug A",
sub="This is hypothetical data",
xlab = "Dosage",ylab = "Drug Response",
xlim = c(0, 60),ylim = c(0,70))
plot(dose,drugB,type="b",col="blue",lty=2,pch=2,lwd=2,
main="Clinical Trials for Drug B",
sub="This is hypothetical data",
xlab = "Dosage",ylab = "Drug Response",
xlim=c(0,60),ylim = c(0,70))
設(shè)置參數(shù)分別是顏色,線型芬失,繪圖標(biāo)志楣黍,線寬,主標(biāo)題棱烂,副標(biāo)題,x軸標(biāo)簽哩治,y軸標(biāo)簽衬鱼,x軸的坐標(biāo)范圍,y坐標(biāo)范圍```
使用title()函數(shù),運(yùn)行位置是在繪圖之后進(jìn)行添加的
#高級函數(shù)包含了標(biāo)題或者標(biāo)簽蒜胖,使用par(ann=FALSE)或者在繪圖函數(shù)中使用ann=FLASE來移除
#par(ann=FALSE)
plot(dose,drugA,ann=FALSE)
title(main="my title",col.main="red",
sub="my sub title",col.sub="blue",
xlab="my x label",ylab="my ylabel",col.lab="green",
cex.lab=.75)
par(opar)```
#自定義坐標(biāo)軸axis(),
side表示在圖形哪邊繪制坐標(biāo)軸,1234台谢,下左上右,
at繪制刻度線的位置岁经,輸入是向量
labels表示刻度線旁邊的標(biāo)簽
las表示標(biāo)簽垂直2還是平行0于坐標(biāo)軸,tck圖形內(nèi)側(cè)正值還是外側(cè)負(fù)值默認(rèn)-0.01
pdf("mydata.pdf")
x<-c(1:20)
y<-x
z<-20/x
opar<-par(no.readonly = TRUE)
par(mar=c(5,4,4,8)+0.1)
繪制一條直線,yaxt表示禁用y軸刻度,ann禁用默認(rèn)標(biāo)簽
plot(x,y,type = "b",pch=21,col="red",yaxt="n",lty=3,ann=FALSE)
繪制20/x線,lines()為一幅現(xiàn)有的圖添加新的圖形元素
lines(x,z,type="b",pch=22,col="blue",lty=2)
axis(2,at=x,labels=x,col.axis="red",las=2)
slices<-z[c(1,11,13,15,17,19)]
axis(4,at=z,labels = round(z,digits = 2),col.axis="blue",las=2,cex.axis=0.7,tck=-0.01)
line是控制文字內(nèi)移還是外移蒿偎,數(shù)字越大怀读,越往外
mtext("y=1/x",side=4,line=2,cex.lab=1,las=2,col="blue")
title("An example of creative axes",xlab = "x values",ylab="y=x")
library(Hmisc)
使用次要刻度線骑脱,nx=2表示將2個(gè)刻度線之間分為兩個(gè)區(qū)間,也就是加一條次要刻度線
minor.tick(nx=2,ny=3,tick.ratio = 0.5)
par(opar)
dev.off()```
參考線和圖例叁丧,abline(),legend()
#這里是指定x與x軸垂直的參考線拥娄,abline(h=yvalues,v=xvalues)
abline(x=seq(1,10,2),lty=2,col="blue")
#在x為1,3,5,7,9的位置添加了垂直的藍(lán)色虛線
pdf("mylegend.pdf")
dose<-c(20,30,40,45,60)
drugA<-c(16,20,27,40,60)
drugB<-c(15,18,25,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")
#增加第二個(gè)線條
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)
#inset表示向內(nèi)側(cè)移動(dòng)的大小,按照全圖比例來說稚瘾,legend參數(shù)表示圖例標(biāo)簽
legend("topleft",inset=0.05,title="Drug Type",legend=c("A","B"),
lty=c(1,2),pch=c(15,17),col=c("red","blue"))
par(opar)
dev.off()```
#文本標(biāo)注摊欠,繪圖區(qū)域內(nèi)部text()和mtext()圖形的四個(gè)邊界添加文本
text(wt,mpg,row.names(mtcars),cex=0.6,pos=4,col="red")
使用wt,mpg作為標(biāo)注的x,y坐標(biāo),row.names作為標(biāo)記文本內(nèi)容些椒,cex作為調(diào)整字號,pos作為文本位置赢乓,1234下左上右石窑,col為顏色
pdf("mytext.pdf")
opar<-par(no.readonly = TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,4,"example of default text")
text(4,5,family="mono","example of default text")
text(6,6,"say you love me",cex=0.6,pos=1,col="blue")
par(opar)
dev.off()```
圖形的組合,par(mfrow=c(2,2)),按行填充,mfcol按列填充
par(mfrow=c(2,2))
#后面是畫的圖试吁,plot或者h(yuǎn)ist等函數(shù),就會自動(dòng)的填充到這個(gè)組合圖形中
par(mfcol=c(3,1))
pdf("mylayout.pdf")
wt<-c(12,32,43,54,64)
mpg<-c(23,43,34,53,23)
disp<-c(1,2,3,4,5)
#layout(matrix(c(1,1,2,3),2,2,byrow=TRUE))
#表示寬兩個(gè)圖第一個(gè)占3份烛恤,第二個(gè)占1份余耽,高度第一個(gè)占1份,第二個(gè)占2份
layout(matrix(c(1,1,2,3),2,2,byrow=TRUE),widths = c(3,1),heights = c(1,2))
#按行填充3行一列
#par(mfrow=c(3,1))
#par(mfrow=c(2,2))
hist(wt)
hist(mpg)
hist(disp)
dev.off()```
#圖形的精細(xì)控制
使用par的fig參數(shù)進(jìn)行控制
pdf("myfig.pdf")
wt<-c(12,32,43,54,64)
mpg<-c(23,43,34,53,23)
disp<-c(1,2,3,4,5)
opar<-par(no.readonly = TRUE)
par(fig=(c(0,0.8,0,0.8)))
plot(wt,mpg,xlab = "miles per gallon",ylab = "car weight")
par(fig=c(0,0.8,0.55,1),new=TRUE)
boxplot(wt,horizontal=TRUE,axes=FALSE)
fig=默認(rèn)會新建一幅新的圖形币喧,所以在添加一幅圖到現(xiàn)有的圖形的時(shí)候,設(shè)定參數(shù)new=TRUE
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(mpg,axes=FALSE)
outer表示use outer margins if available,line:on which margin line,starting at 0 counting out wards
mtext("enhanced scatterplot",side=3,outer=TRUE,line=-3)
par(opar)
dev.off()```
創(chuàng)建圖形
#創(chuàng)建圖形最基本的函數(shù)plot()
#plot是一個(gè)泛型函數(shù)干发,真正被調(diào)用的函數(shù)依賴于對象所屬的類
plot(c(1,2,3),c(4,2,5))
#plot是分若干階段執(zhí)行的史翘,這樣就可以用一系列命令分若干步來構(gòu)建一幅完整的圖形
#首先可以繪制空白圖形,向其中添加坐標(biāo)軸
#type="n"表示不要給圖中添加任何元素
#繪制坐標(biāo)軸琼讽,這樣確定了坐標(biāo)軸的范圍
#其實(shí)這樣是隨著數(shù)據(jù)點(diǎn)而變化的
plot(c(-5,3),c(-1,6),type="n",xlab="x",ylab="y")
x <- c(1,2,3)
y <- c(1,3,8)
#只是添加了3個(gè)點(diǎn)
plot(x,y)
#abline()函數(shù)添加線
#計(jì)算線性回歸
lmout <- lm(y~x)
abline(lmout)```
#abline函數(shù)
參數(shù)是斜率和截距钻蹬,lmout里面的lmout$coefficients中就包含了斜率與截距
abline(c(2,1))```
lines函數(shù)
#使用lines函數(shù)先向圖形添加點(diǎn),然后用直線將其連起來
lines(c(1.5,2.5),c(2,3))
#只畫出線條不需要點(diǎn),使用參數(shù)type="l"
plot(x,y,type="l")```
#指定線型
help("par"),中l(wèi)ty參數(shù)脉让,0是空白,1是實(shí)線术唬,2是虛線等等
plot(x,y,type="l",lty=2)```
在現(xiàn)有圖形基礎(chǔ)上新增繪圖窗口
#Linux下X11(),windows下windows()
hist(x)
windows()
hist(y)```
#points函數(shù)
向現(xiàn)有的圖形中添加一系列的點(diǎn)對x滚澜,y,輸入可以是向量借浊,繪圖將一一對應(yīng)
points(x,y,pch="+")```
legend()函數(shù)
#為擁有多條曲線的圖中添加圖例```
#text()函數(shù)
在圖形的任意位置加上一些文字
text(2.5,4,"abc")
字符串的中心萝招,b剛好位于點(diǎn)2.5,4
text()函數(shù)和locator函數(shù)結(jié)合使用
text(locator(1),"i love you")```
locator()函數(shù)
#精確定位
locator(1)```
#定制圖形
cex選項(xiàng)
用于放大或者縮小圖形中的字符
text(locator(1),"love",cex=1.5)```
R圖形設(shè)備
pdf("12.pdf")
#顯示圖形設(shè)備列表
dev.list()
#RStudioGD圖形設(shè)備編號是2,pdf編號是3
#顯示活動(dòng)設(shè)備
dev.cur()
#保存已經(jīng)顯示的圖形
#方法是將當(dāng)前屏幕設(shè)置為活動(dòng)設(shè)備曙蒸,然后將內(nèi)容復(fù)制到PDF設(shè)備上
dev.set(2)
dev.copy(which=4)
#關(guān)閉R圖形設(shè)備
#創(chuàng)建的pdf文件必須在關(guān)閉設(shè)備之后才能正常使用
dev.off()```