1. R语言四大作图系统
- 基础绘图系统
- lattice包
- ggplot2包
- grid包
可以通过ls()命令查看graphics包中的一些绘图函数
ls("package:graphics")
可以通过demo函数查看一些示例绘图函数
demo(graphics())
绘图也可以包含高级绘图和低级绘图函数:
1、高级绘图
高级绘图是一步到位,可以直接绘制出图
2、低级绘图
而低级绘图,不能单独使用,必须在高级绘图产生图形的基础上,对图形进行调整,比如加一条线,加上标题文字等。
2.绘图函数的数据输入
2.1 plot绘图函数
plot函数可以接受单独的一个数值向量,按序号绘制出图像;也可以接受两个数值向量,两个数值分别代表横纵坐标。
2.1.1 散点图
散点图只需要向量类型变量
plot(women$height)
plot(women$height,women$weight)

2.1.2 直方图
直方图需要因子类型的数据才可以绘制出来
plot(as.factor(mtcars$cyl))

2.1.3 箱线图
箱线图需要第一个是因子类型数据,第二个是向量类型数据
plot(as.factor(mtcars$cyl),mtcars$carb)

2.1.4 散列图
散列图需要第一个是向量类型数据,第二个是因子类型数据
plot(mtcars$carb,as.factor(mtcars$cyl))

2.1.5 脊柱图
脊柱图需要第一个是因子类型数据,第二个是因子类型数据
plot(as.factor(mtcars$carb),as.factor(mtcars$cyl))

2.1.6 借助公式绘图
用波浪号连接变量,可以绘制出两变量之间的关系图
plot(women$height~women$weight)

2.1.7 对线性回归分析结果进行绘图
会生成四张图
f<-lm(height~weight,data=women)
plot(f)

S3系统是R中面向对象的一个编程概念,R中每个对象可以添加很多属性。
3指属性,泛型函数,方法。S3系统使得每个函数都有很多子集,像plot就像一个家族一样,家族里面有很多人,每个人都代表着一种方法。方法多了处理问题的能力自然就更强,整体表现出来这个家族神秘而又强大,就像plot函数。
2.2 par绘图函数
用来对绘图参数进行设置,par函数有许多参数。
直接输入
par()
可以查看par绘图参数的默认设置,par函数中的参数设置完成后会在全局的绘图函数中起作用。也可以在绘图函数中直接添加对应的线条形状,颜色等指标。
例如在直方图中改变颜色
plot(as.factor(mtcars$cyl),col=c("red","green","blue"))

2.2.1 修改图片显示格式
par(mfrow=c(2,2))
将图片显示格式改为两行两列的图
网友评论