R语言中的画图

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轴的对数标度

 

posted @ 2016-06-11 13:28  小呆蝎子  阅读(4161)  评论(0编辑  收藏  举报