[关闭]
@Macux 2015-12-01T06:45:23.000000Z 字数 4245 阅读 1455

R语言_初级绘图

R语言_学习笔记



Part 1、用plot()绘制散点图和线图

  1. > par(mfrow=c(1,2)) #创建一张1×2的画布
  2. > plot(pressure)
  3. > abline(v=200) #在x=200处添加一条垂直线
  4. > abline(h=200) #在y=200处添加一条水平线
  5. > plot(pressure,type="l") #type="l",表示用plot()函数绘制线图
  6. > abline(v=200)
  7. > abline(h=200)

此处输入图片的描述


Part 2、创建多组散点图

  1. > attach(iris)
  2. #将数据框iris添加到R的搜索路径,方便在引用数据框的某些列时,单用列名就可完成引用,而不需要使用$。但每次只能添加一个数据框到R的搜索路径。
  3. > f <- factor(Species)
  4. > col_f <- ifelse(Species=="setosa","light blue",
  5. ifelse(Species=="versicolor","orange","green"))
  6. #利用两个ifelse()语句,对属于不同因子的数据配上不同的颜色。
  7. > lwd_f <- ifelse(Species=="setosa",5,ifelse(Species=="versicolor",3,1))
  8. #利用两个ifelse()语句,对属于不同因子的数据选用不同形状的散点。
  9. > plot(xlim=c(0,7),ylim=c(0,3),Petal.Length,Petal.Width,
  10. pch=as.integer(f),
  11. col=col_f,
  12. lwd=lwd_f)
  13. > legend(0.0780599,2.968162, #定位如此精确,就要用到locator()函数。
  14. as.character(levels(f)),
  15. pch=1:length(levels(f)),
  16. col=c("blue","red","green"),
  17. lwd=c(5,3,1),
  18. xpd=TRUE)) #允许在区域外画图。

此处输入图片的描述


Part 3、呈现不同因子水平的散点图,即“条件化图”(conditioning plot)

  1. > library(MASS)
  2. > attach(Cars93)
  3. > coplot(Horsepower~MPG.city | Origin,
  4. #每幅图对应Origin的一个因子水平。
  5. pch=19, #pch=19,使得散点全为实心点。
  6. col=rainbow(12))
  7. #用rainbow()函数控制颜色的变化,是一件很有意思的事!

此处输入图片的描述


Part 4、生成散点图矩阵查看二元关系

  1. > library(MASS)
  2. > library(car)
  3. > attach(Cars93)
  4. > sla <- Cars93[,c("Price","Horsepower","RPM","MPG.highway",
  5. "EngineSize","Rev.per.mile")]
  6. > scatterplotMatrix(sla,spread=FALSE,lwd=1.4,
  7. col=c("darkorange1","mediumpurple3","olivedrab3"))
  8. #spread=FALSE,表示不添加展示分散度和对称信息的直线。lty.smooth=2,设定拟合曲线用虚线表示。
  9. #当只生成散点图矩阵时,由于上对角线和下对角线的图完全一样,可以设置参数upper.panel=NULL,就只显示下三角的图形。

此处输入图片的描述


Part 5、带有阴影的密度曲线

  1. #绘制有阴影的密度曲线
  2. > x <- seq(from=-3,to=3,length.out=100)
  3. > y <-dnorm(x)
  4. > par(col.main="red")
  5. > plot(x,y,main="Standard Normal Distribution",type='l',
  6. ylab="Density",xlab="Quantile",col=c("pink"),lwd=3)
  7. > abline(h=0)
  8. #The body of the polygon follows the density curve where 1 <= z <=2
  9. > region.x <- x[1 <= x & x <=2]
  10. > region.y <- y[1 <= x & x <=2]
  11. #We add initial and final segments, which drop down to the Y axis
  12. > region.x <- c(region.x[1],region.x,tail(region.x,1))
  13. > region.y <- c(0,region.y,0)
  14. #polygon(region.x,region.y,density=50)
  15. > polygon(region.x,region.y,density=5,col=c("blue"),lwd=3)

此处输入图片的描述


Part 6、对直方图添加密度估计

  1. > set.seed(888)
  2. #将种子设置为888,这样每次运行该脚本,只要有这一行,生成的伽马随机数都是同一个序列。
  3. > samp <- rgamma(1000,2,3)
  4. > hist(samp,20,
  5. prob=T, #prob=T,表示添加密度曲线
  6. col=rainbow(20))
  7. > lines(density(samp))

此处输入图片的描述


Part 7、创建离散直方图

par()函数的所有参数设置

  1. > par(fg="pink",bty="l") #把边框颜色设为粉色,把边框设为L型。
  2. > 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)
  3. > plot(table(Redondo),type="h",lwd=6,col=rainbow(7))

此处输入图片的描述


Part 8、带有置信区间的条形图

  1. > library(gplots)
  2. > attach(airquality)
  3. > heights <- tapply(Temp,Month,mean)
  4. > lower <- tapply(Temp,Month,function(v) t.test(v)$conf.int[1])
  5. > upper <- tapply(Temp,Month,function(v) t.test(v)$conf.int[2])
  6. > barplot2(heights,plot.ci=TRUE,ci.l=lower,ci.u=upper,
  7. ylim=c(50,90),
  8. col=c("bisque1","cyan2","darkgoldenrod","darkorange1","darkolivegreen2"),
  9. xpd=FALSE)) #不允许在区域外作图。

此处输入图片的描述
R中所有的颜色


Part 9、棘状图

  1. > library(vcd)
  2. > library(MASS)
  3. > attach(Cars93)
  4. > counts <- table(Origin,Type)
  5. > spine(counts,main="Spinegram Of Cars93")

此处输入图片的描述


Part 10、箱线图

  1. > library(MASS)
  2. > attach(Cars93)
  3. > boxplot(Price~DriveTrain,main="Price by DriveTrain",
  4. xlab="Price",ylab="DriveTrain",
  5. col=c("light blue","light green","pink"),
  6. horizontal=TRUE, #将箱线图水平放置。其实竖着放更易观察。
  7. notch=TRUE, #创建有凹槽的箱线图。没必要设置。
  8. varwidth=TRUE) #使箱线图的宽度与其样本大小的平方根成正比。

此处输入图片的描述

  1. > library(MASS)
  2. > attach(Cars93)
  3. > boxplot(Price~DriveTrain*Origin,
  4. ylab="Price",
  5. main="Price by DriveTrain & Origin",
  6. col=c("light blue","pink"),
  7. varwidth=TRUE)

此处输入图片的描述


Part 11、小提琴图

  1. > library(vioplot)
  2. > library(MASS)
  3. > attach(Cars93)
  4. > vioplot(Min.Price,Price,Max.Price,
  5. names=c("Min.Price","Price","Max.Price"),
  6. col="tan1")

此处输入图片的描述


Part 12、饼图

  1. > library(MASS)
  2. > attach(Cars93)
  3. > koni <- table(Type)
  4. > pie(koni,main="Pie Chart Of Type",col=rainbow(6))

此处输入图片的描述


Part 13、Q-Q图

  1. > par(mfrow=c(2,2))
  2. > library(MASS)
  3. > attach(Cars93)
  4. > par(mfrow=c(2,2))
  5. > col1 <- ifelse(Horsepower>144,"blue","red")
  6. > qqnorm(log(Horsepower),col=col1,pch=1)
  7. > qqline(log(Horsepower))
  8. > col2 <- ifelse(Price>20,"blue","red")
  9. > qqnorm(log(Price),col=col2,pch=6)
  10. > qqline(log(Price))
  11. > col3 <- ifelse(RPM>5280,"blue","red")
  12. > qqnorm(log(RPM),col=col3,pch=8)
  13. > qqline(log(RPM))
  14. > col4 <- ifelse(Rev.per.mile>2333,"blue","red")
  15. > qqnorm(log(Rev.per.mile),col=col4,pch=18)
  16. > qqline(log(Rev.per.mile))
  17. #利用ifelse()函数,用颜色对属于不同数据集的数据进行分类。

此处输入图片的描述
有关各种绘图符号的具体样式,参见《R语言实践》P45,图3-4

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注