基本绘图函数:plot的使用

 

注意:“##”后面是程序输出结果

例如:

    par("bg") # 命令

## [1] "white" # 结果
基本绘图函数:
      plot:散点图
      hist:直方图
      boxplot:箱线图
      points:添加线
      lines:添加线
      text:添加文字
      title:标题(main)、坐标(xlab、ylab)、字幕(sub)等的设置
      axis:坐标轴设置
  1. plot

  • 常用参数:xlab/ylab/lwd/lty/pch/col
    lwd-线宽
    lty-线型
    pch:点类型
  1. par:类型设置(用于全局的设定,会影响后期图形)
  • 常用参数: bg
    mar(边距)
    las(标签设置,水平or竖直)

 # 0:always parallel(平行) to the axis [default]
 # 1:always horizontal
 # 2:always perpendicular(垂直) to the axis
 # 3:always vertical

mfrow(子图,行填充)/mfclo(子图,列填充)

  • ?par 可查看帮助
  • 查看默认值:
    par("bg")

## [1] "white"

    par("mar")


## [1] 5.1 4.1 4.1 2.1

 

  1. plot练习
with( airquality,
      plot( Wind,Temp,main="wind and Temp in NYC",type="n") # type="n"画出除点之外的部分  
)

#
-按月份画点-# with( airquality[airquality$Month==9, ], points( Wind,Temp,col="red" ) ) with( subset(airquality,Month==8), # subset 取子集,对比上面 points( Wind,Temp,col="blue" ) ) with( subset(airquality,Month %in% c(5,6,7) ), # %in% points( Wind,Temp,col="yellow" ) )
#
--拟合--# fit<-lm( Temp~Wind,airquality ) # 因变量~自变量 abline( fit,lwd=2 ) # abline:加入拟合线
#
--添加图例--# legend( "topright", # 位置:右上角 pch=1, # 1-表示圆圈,2-三角形。。。<?pch> col=c( "red","blue","yellow" ), legend=c( "九月","八月","其他" ) )

dev.off() ##
null device ## 1

 

  1. par练习
default.par<-par() # 保存默认设置
par(bg = "lightgrey") # 背景颜色设置
with(airquality,
     hist(Wind)
     )
 

dev.off() # 清除绘图

## null device
##           1

par( mfrow = c(2,2) )
par(las = "0")
with(
  airquality,
  plot(Wind,Temp,main="las=\"0\"")
)

#-注意with的使用-#
with(
  airquality,{
    par(las = "1")
    plot(Wind,Temp,main="las=\"1\"")
   
    par(las = "2")
    plot(Wind,Temp,main="las=\"2\"")
   
    par(las = "3")
    plot(Wind,Temp,main="las=\"3\"")
  }
)

 

小结 1. par设置可利用帮助学习(全局设置)。
 - las设定坐标轴标签方向<0-平行坐标轴,1-水平,2-垂直坐标轴,3-竖直的,个人比较喜欢“1”>
2. with构建数据集
 with(data,
 {
            exp1
            exp2
 }
 )
3. with与attach的区别:
         attach存在隐患,当数据集与内存中已存在的变量重名时会出现错误。with则是封闭在数据框的环境中进行运算,不会与外在环境中的对象相冲突,因此,没有以上的隐患。
4. 拟合 lm( 因变量 ~ 自变量, data) abline( 拟合返回对象 )

posted @ 2016-04-17 14:33  ^-馒头-^  阅读(1771)  评论(0编辑  收藏  举报