用R一页绘制多图以及绘制组合图(一幅图上添加新图)

在同一页面上绘制多张图用layout()函数,在一幅图上添加新图,得到组合图用par()函数,下面举个例子。

  1. data <- read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=',', header=T)
  2. attach(data)
  3.  
  4. par(mfrow=c(2,2))
  5. plot(read,write,main="ScatterPlot of read vs write")
  6. hist(read,main="Histogram of read")
  7. boxplot(read,main="Boxplot of read")
  8. barplot(read,main="Boxplot of read")

用R一页绘制多图以及绘制组合图(一幅图上添加新图)-图片1

  1. data <- read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=',', header=T)
  2. attach(data)
  3. par(mfrow=c(3,1))
  4. plot(read,write,main="ScatterPlot of read vs write")
  5. hist(read,main="Histogram of read")
  6. boxplot(read,main="Boxplot of read")

用R一页绘制多图以及绘制组合图(一幅图上添加新图)-图片2

  1. data <- read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=',', header=T)
  2. attach(data)
  3. layout(matrix(c(1,1,2,3), 2, 2, byrow = TRUE))
  4. hist(read)
  5. hist(write)
  6. hist(math)

用R一页绘制多图以及绘制组合图(一幅图上添加新图)-图片3

  1. data <- read.table("http://www.ats.ucla.edu/stat/R/notes/hsb2.csv", sep=',', header=T)
  2. attach(data)
  3.  
  4. par(fig=c(0,0.85,0,0.85),new=FALSE)
  5. plot(read,write,ylab="write",xlab="read",col="orange")
  6. par(fig=c(0,0.85,0.45,1),new=TRUE)
  7. boxplot(read,horizontal=TRUE,axes=FALSE,col=rainbow(1))
  8. par(fig=c(0.55,1,0,0.85),new=TRUE)
  9. boxplot(write,horizontal=FALSE,axes=FALSE,col=rainbow(1))
  10. mtext("Enhanced Scatterplot", side=3, outer=TRUE, line=-3)

用R一页绘制多图以及绘制组合图(一幅图上添加新图)-图片4

上面的例子可能举得不恰当,但我发表此博客主要是希望能为R爱好者提供绵薄之力,希望各位提出宝贵意见。

发表评论

匿名网友

拖动滑块以完成验证
加载中...