plot(x,main = "Forecast Results",xlab = "Month",ylab = "Production",col=c("red","black","green"))
#plot 通用繪圖函數
#boxplot 創建箱線圖
#hist 創建直方圖
#qqnorm 創建QQ圖
#curve 繪制函數圖形
#points 添加點
#lines 添加線
#abline 添加直線
#segments 添加線段
#polygon 添加閉合多邊形
#text 添加文本
plot(cars)
plot(cars,ann=FALSE)
#ann=FALSE,它要求不繪制注釋內容,然后調用函數title添加需要的標題和標簽
title(main="The title",xlab = "speed",ylab = "dist")
plot(cars,type="n")
#調用函數plot時設置參數type=“n”,它將在不顯示數據的情況下初始化圖形框架
#調用函數grid來繪制網格
grid()
points(cars)
#在兩個向量x和y中有成對觀測值,還有一個平行因子f,可以為1,2,3,4,5,6,7,8,9.。。用來表示不同的形狀
plot(x,y,pch=as.integer(f))
with(iris,plot(Petal.Length,Petal.Width))
with(iris,plot(Petal.Length,Petal.Width,pch=as.integer(Species)))
#添加圖例
#點的圖例
legend(x,y,labels,pch=c(pointtype1,pointtype2,....))
#不同線型的線的圖例
legend(x,y,labels,lty=c(linetype1,linetype2,...))
#不同線寬的線的圖例
legend(x,y,labels,lwd=c(width1,width2,...))
#不同顏色的圖例
legend(x,y,labels,col=c(color1,color2,...))
legend(1.5,2.4,c("setosa","versicolor","virginica"),pch=1:3)
#兩種方法畫出圖例
f<-factor(iris$Species)
with(iris,plot(Petal.Length,Petal.Width,pch=as.integer(f)))
legend(1.5,2.4,as.character(levels(f)),pch=1:length(levels(f)))
#顯示了根據線型(實線,虛線或點線)的線的圖例
legend(0.5,95,c("Estimate","Lower conf lim","Upper conf lim"),lty=c("solid","dashed","dotted"))
#繪制散點圖的回歸線
install.packages("faraway")
library("faraway")
data("strongx")
m<-lm(crossx~energy,data=strongx)
summary(m)
plot(crossx~energy,data=strongx)
abline(m)
#多變量散點圖的繪制
head(iris)
plot(iris[,1:4])
#創建每個因子水平的散點圖
coplot(y~x|f)#這將產生x對應於y的散點圖,每幅圖對應f一個水平
data("Cars93",package="MASS")
coplot(hormone~MPG.city|Origins,data=Cars93)
#創建條狀圖
heights<-tapply(airquality$Temp,airquality$Month,mean)
barplot(heights)
barplot(heights,main = "Mean Temp.by Month",names.arg = c("May","Jun","Jul","Aug","Sep"))
#對條形圖添加置信區間
library(gplots)
attach(airquality)
heights<-tapply(Temp,Month,mean)
lower<-tapply(Temp, Month, function(v) t.test(v)$conf.int[1])
upper<-tapply(Temp, Month, function(v) t.test(v)$conf.int[2])
barplot2(heights,plot.ci = TRUE,ci.l = lower,ci.u = upper)#控制條形快的寬度用xpd,給條形快添加標簽用names.arg
barplot2(heights,plot.ci = TRUE,ci.l = lower,ci.u = upper,ylim = c(50,90),xpd=FALSE,main="Mean Temp.By Month",names.arg = c("May","Jun","Jul","Aug","Sep"),ylab = "Temp(deg.F)")
#給條形圖上色
barplot(heights,col=colors)
barplot(c(3,4,5),col = c("red","white","blue"))
rel.hts<-rank(heights)/length(heights)
grays<-gray(1-rel.hts)
barplot(heights,col=grays)
#完整的解決方案
rel.hts<-(heights-min(heights))/(max(heights)-min(heights))
grays<-gray(1-rel.hts)
barplot(heights,col=grays,ylim = c(50,90),xpd=FALSE,main="Mean Temp.By Month",names.arg = c("May","Jun","Jul","Aug","Sep"),ylab = "Temp(deg.F)")
#如果想用彩色的,可以使用rainbow來代替gray
#繪制過點x,y的直線
plot(x,y,type="l")
plot(dfrm,type = "l")
#pressure是內置數據集
plot(pressure)
plot(pressure,type="l")
#改變線的類型,寬度和顏色
#線的類型
#lty="solid"或者lty=1 默認
#lty="dashed"或者lty=2
#lty="dotted"或者lty=3
#lty="dotdash"或者lty=4
#lty="longdash"或者lty=5
#lty="twodash"或者lty=6
#lty="blank"或者lty=0
#畫虛線
plot(pressure,type="l",lty="dashed")
plot(pressure,type="l",lwd=2)
plot(pressure,type="l",col="red")
#函數lines將線添加到現有的圖形中並允許指定它們的類型、寬度和顏色
plot(x,y.democr,type = "l",col="blue")
lines(x,y.republ,col="red")
#繪制多個數據集
#使用高級的圖形函數如plot,curve來初始化圖形,使用低級函數如lines,points來添加額外的數據集
#先使用range來計算極限值,然后使用xlim,ylim來設置他們
x1<-c(1,2,3,4,5,6,7,8,9)
x2<-c(5,4,5,6,9,8,7,1,2)
y1<-c(9,8,7,6,5,4,3,2,1)
y2<-c(10,2,0,4,5,8,7,5,6)
xlim<-range(c(x1,x2))
ylim<-range(c(y1,y2))
plot(x1,y1,type ="l",xlim = xlim,ylim = ylim)
lines(x2,y2,lty="dashed")
#添加水平線
abline(v=x)
abline(h=y)
#畫出經過圖形原點的軸
abline(v=0)
abline(h=0)
#繪制這些點,然后繪制一條經過它們均值的實線
plot(samp)
m<-mean(samp)
abline(h=m)
#想要用圖形來說明樣本標准差,所以我們計算它們並在距離均值正負1和正負2個標准差的地方繪制虛線
stdevs<-m+c(-2,-1,+1,+2)*sd(samp)
abline(h=stdevs,lty="dotted")
#每個因子水平創建箱線圖
boxplot(x~f)#x是數值型變量,f是因子
plot(f,x)#也可以調用函數plot的兩個參數的形式,注意第一個參數是因子
library("MASS")
UScereal
boxplot(sugars~shelf,data=UScereal)
boxplot(sugars~shelf,data=UScereal,main="Sugar Content by Shelf",xlab="Shelf",ylab="Sugar(grams per portion)")
#創建直方圖hist()
data("Cars93",package="MASS")
hist(Cars93$MPG.city)
#hist中包含了第二個參數,即建議的直方塊的數量
hist(Cars93$MPG.city,20)
hist(Cars93$MPG.city,20,main="City MPG(1993)",xlab="MPG")
#對直方圖添加密度估計
hist(Cars93$MPG.city,20)
lines(density(Cars93$MPG.city))
#從伽馬分布抽取樣本
samp<-rgamma(500,2,2)
hist(samp,20,prob=T)
lines(density(samp))
#創建離散的直方圖
#調用函數table來對事件進行計數,然后調用包含參數type="h"的plot函數來將事件繪制成一個直方圖
plot(table(x),type = "h")
plot(table(x),type="h",lwd=5,ylab = "Freq")
plot(table(x)/length(x),type="h",lwd=5,ylab = "Freq")#有相對頻率的直方圖,而不是計數
#創建qq圖
qqnorm(x)
qqline(x)
data(Cars93,package="MASS")
qqnorm(Cars93$Price,main = "Q-Q Plot:Price")
qqline(Cars93$Price)#太多的點在對角線之上,表示大體向左偏的趨勢
#向左偏斜也許能夠通過對數變換糾正
data(Cars93,package="MASS")
qqnorm(log(Cars93$Price),main = "Q-Q Plot:Price")
qqline(log(Cars93$Price))
#假設數據y有自由度為5的學生t分布,學生t分布的分位數函數是qt,第二個參數是自由度
plot(qt(ppoints(y),5),sort(y))
abline(a=0,b=1)
#從均值為10(或比率為1/10)的指數分布中隨機抽取
RATE<-1/10
Y<-rexp(N,rate=RATE)
#對於指數分布的分位數函數是qexp,有一個參數rate
plot(qexp(ppoints(y),rate = RATE),sort(y),main = "QQ plot",xlab = "Theoretical Quantiles",ylab = "Sample Quantiles")
abline(a=0,b=1)
#用各種顏色繪制變量
plot(x,col="blue")
plot(x,type="h",lwd=3)
#對圖形添加陰影
colors<-ifelse(x>=0,"black","gray")
plot(x,type="h",lwd=3,col=colors)
colors<-ifelse(x>=0,"green","red")
plot(x,type="l",col=c("blue","green"))
#繪制函數
curve(sin,-3,+3)
curve(dnorm,-3.5,+3.5,main="Std. Normal Density")#顯示了標准正太密度函數的圖
f<-function(x){
exp(-abs(x))*sin(2*pi*x)
}
curve(f,-5,+5,main="Dampened Sine Wave")
#圖形間暫停
par(ask=TRUE)
#當ask設定為TRUE時,R在開始一個新圖形前會馬上輸出一下信息
#Waiting to confirm page change...
#當你准備好繪制新圖形時,單擊圖形窗口,R會開始下一個圖形
#在一頁中顯示多個圖形
par(mfrow=c(N,M))#N行M列
par(mfrow=c(2,2))
Quantile<-seq(from=0,to=1,length.out = 30)
plot(Quantile,dbeta(Quantile,2,4),type = "l",main="First")
plot(Quantile,dbeta(Quantile,4,2),type = "l",main="Second")
plot(Quantile,dbeta(Quantile,1,1),type = "l",main="Third")
plot(Quantile,dbeta(Quantile,0.5,0.5),type = "l",main="Fourth")
#grid和lattice包含額外的顯示多圖形的工具
#打開另一個圖形窗口
win.graph()
dev.set()#此函數進行窗口切換
#可以設置窗口大小
win.graph(width = 7.0,height = 5.5)
#在文檔中繪制圖形
#當需要把圖形保存在一個文件中,例如PNG,JPEG或PostScript
savePlot(filename = "filename.ext",type = "type")
#調用這個函數,該方案會如下執行並創建圖形文件myPlot.png
png("myPlot.png",width = 648,height = 432)
plot(x,y,main = "Scatterplot of x, y")
dev.off()
#改變圖形參數
#需要改變圖形軟件的一個全局參數,例如線型,背景顏色,或者字體大小
par(lwd=2)
#par的參數
#1、ask=logical 如果為TRUE,在每個新圖前
#2、bg="color" 背景色
#3、cex=number 文字或者繪圖點的高度,表示為標准尺寸的倍數
#4、col="color" 默認繪圖顏色
#5、fg="color" 前景色
#6、lty="linetype" 線型:實線,虛線等
#7、lwd=number 線寬:1:正常,2:寬,3:更寬
#8、mfcol=c(nr,nc)或者mfrow=c(nr,nc) 建立多圖畫布矩陣,nr行,nc列
#9、new=logical 在一張圖上繪制另外一張圖形
#10、pch=pointtype 默認點類型
#11、xlog=logical 采用x軸的對數標度
#12、ylog=logical 采用y軸的對數標度
