@Macux
2015-12-01T06:45:23.000000Z
字数 4245
阅读 1455
R语言_学习笔记
> par(mfrow=c(1,2)) #创建一张1×2的画布
> plot(pressure)
> abline(v=200) #在x=200处添加一条垂直线
> abline(h=200) #在y=200处添加一条水平线
> plot(pressure,type="l") #type="l",表示用plot()函数绘制线图
> abline(v=200)
> abline(h=200)
> attach(iris)
#将数据框iris添加到R的搜索路径,方便在引用数据框的某些列时,单用列名就可完成引用,而不需要使用$。但每次只能添加一个数据框到R的搜索路径。
> f <- factor(Species)
> col_f <- ifelse(Species=="setosa","light blue",
ifelse(Species=="versicolor","orange","green"))
#利用两个ifelse()语句,对属于不同因子的数据配上不同的颜色。
> lwd_f <- ifelse(Species=="setosa",5,ifelse(Species=="versicolor",3,1))
#利用两个ifelse()语句,对属于不同因子的数据选用不同形状的散点。
> plot(xlim=c(0,7),ylim=c(0,3),Petal.Length,Petal.Width,
pch=as.integer(f),
col=col_f,
lwd=lwd_f)
> legend(0.0780599,2.968162, #定位如此精确,就要用到locator()函数。
as.character(levels(f)),
pch=1:length(levels(f)),
col=c("blue","red","green"),
lwd=c(5,3,1),
xpd=TRUE)) #允许在区域外画图。
> library(MASS)
> attach(Cars93)
> coplot(Horsepower~MPG.city | Origin,
#每幅图对应Origin的一个因子水平。
pch=19, #pch=19,使得散点全为实心点。
col=rainbow(12))
#用rainbow()函数控制颜色的变化,是一件很有意思的事!
> library(MASS)
> library(car)
> attach(Cars93)
> sla <- Cars93[,c("Price","Horsepower","RPM","MPG.highway",
"EngineSize","Rev.per.mile")]
> scatterplotMatrix(sla,spread=FALSE,lwd=1.4,
col=c("darkorange1","mediumpurple3","olivedrab3"))
#spread=FALSE,表示不添加展示分散度和对称信息的直线。lty.smooth=2,设定拟合曲线用虚线表示。
#当只生成散点图矩阵时,由于上对角线和下对角线的图完全一样,可以设置参数upper.panel=NULL,就只显示下三角的图形。
#绘制有阴影的密度曲线
> x <- seq(from=-3,to=3,length.out=100)
> y <-dnorm(x)
> par(col.main="red")
> plot(x,y,main="Standard Normal Distribution",type='l',
ylab="Density",xlab="Quantile",col=c("pink"),lwd=3)
> abline(h=0)
#The body of the polygon follows the density curve where 1 <= z <=2
> region.x <- x[1 <= x & x <=2]
> region.y <- y[1 <= x & x <=2]
#We add initial and final segments, which drop down to the Y axis
> region.x <- c(region.x[1],region.x,tail(region.x,1))
> region.y <- c(0,region.y,0)
#polygon(region.x,region.y,density=50)
> polygon(region.x,region.y,density=5,col=c("blue"),lwd=3)
> set.seed(888)
#将种子设置为888,这样每次运行该脚本,只要有这一行,生成的伽马随机数都是同一个序列。
> samp <- rgamma(1000,2,3)
> hist(samp,20,
prob=T, #prob=T,表示添加密度曲线
col=rainbow(20))
> lines(density(samp))
> par(fg="pink",bty="l") #把边框颜色设为粉色,把边框设为L型。
> Redondo <- c(1,2,3,3,3,3,3,3,3,3,5,6,1,2,3,1,1,3,2,2,2,2,1,1,1,1,6,7,7,7,8,9,5,4,1,13,13)
> plot(table(Redondo),type="h",lwd=6,col=rainbow(7))
> 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,
ylim=c(50,90),
col=c("bisque1","cyan2","darkgoldenrod","darkorange1","darkolivegreen2"),
xpd=FALSE)) #不允许在区域外作图。
> library(vcd)
> library(MASS)
> attach(Cars93)
> counts <- table(Origin,Type)
> spine(counts,main="Spinegram Of Cars93")
> library(MASS)
> attach(Cars93)
> boxplot(Price~DriveTrain,main="Price by DriveTrain",
xlab="Price",ylab="DriveTrain",
col=c("light blue","light green","pink"),
horizontal=TRUE, #将箱线图水平放置。其实竖着放更易观察。
notch=TRUE, #创建有凹槽的箱线图。没必要设置。
varwidth=TRUE) #使箱线图的宽度与其样本大小的平方根成正比。
> library(MASS)
> attach(Cars93)
> boxplot(Price~DriveTrain*Origin,
ylab="Price",
main="Price by DriveTrain & Origin",
col=c("light blue","pink"),
varwidth=TRUE)
> library(vioplot)
> library(MASS)
> attach(Cars93)
> vioplot(Min.Price,Price,Max.Price,
names=c("Min.Price","Price","Max.Price"),
col="tan1")
> library(MASS)
> attach(Cars93)
> koni <- table(Type)
> pie(koni,main="Pie Chart Of Type",col=rainbow(6))
> par(mfrow=c(2,2))
> library(MASS)
> attach(Cars93)
> par(mfrow=c(2,2))
> col1 <- ifelse(Horsepower>144,"blue","red")
> qqnorm(log(Horsepower),col=col1,pch=1)
> qqline(log(Horsepower))
> col2 <- ifelse(Price>20,"blue","red")
> qqnorm(log(Price),col=col2,pch=6)
> qqline(log(Price))
> col3 <- ifelse(RPM>5280,"blue","red")
> qqnorm(log(RPM),col=col3,pch=8)
> qqline(log(RPM))
> col4 <- ifelse(Rev.per.mile>2333,"blue","red")
> qqnorm(log(Rev.per.mile),col=col4,pch=18)
> qqline(log(Rev.per.mile))
#利用ifelse()函数,用颜色对属于不同数据集的数据进行分类。
有关各种绘图符号的具体样式,参见《R语言实践》P45,图3-4