1. 绘图函数(graphics 包)
- plot / hist / boxplot / points / lines / text / title / axis
- 调用函数会启用一个图形设备并在设备上绘图
· 基本绘图系统 + 屏幕设备
2. plot()
- plot(x, y, ...)
- 重要参数:xlab / ylab / lwd / lty / pch / col
- 查看其他参数:?par
· par() 用于设置全局参数(作用于R中所有plot绘图)
- bg / mar / las / mfrow / mfcol
· 这些参数可以在每次plot之前进行修改
3. 实践
hist(airquality$Wind, xlab="Wind") # 直方图,风速值的频率 boxplot(airquality$Wind, xlab="Wind", ylab="Speed(mph)") # 风速的箱图 boxplot(Wind~Month, airquality, xlab="Month", ylab="Speed(mph)") # 按月份的风速箱图 plot(airquality$Wind, airquality$Temp) # 风速和对应温度的散点图 with(airquality, plot(Wind, Temp, main="Wind and Temp in NYC")) # with函数 title(main="Wind and Temp in NYC") # 添加标题 with(airquality, plot(Wind, Temp, main="Wind and Temp in NYC", type="n")) # 图的框架 with(subset(airquality, Month==9), points(Wind,Temp,col="red")) # 9月红点 with(subset(airquality, Month==5), points(Wind,Temp,col="blue")) # 5月蓝点 with(subset(airquality, Month %in%c(6,7,8)), points(Wind,Temp,col="black")) # 其他月黑点 fit <- lm(Temp~Wind, airquality) # lm函数:拟合线性模型 abline(fit, lwd=2) # 添加直线,线宽为2 legend("topright", pch=1, col=c("red","blue","black"),legend=c("Sep","May","Other")) # 添加图例说明
4. 全局参数
- 通过命令 ?par 查看帮助文档
par("bg") par("col") par("mar") # (bottom, left, top, right) par("mfrow") # 几行几列的图,按行排列 par("mfcol") # 几行几列的图,按列排列 par(mfrow=c(1,2)) # 一行两列的图 hist(airquality$Temp) hist(airquality$Wind) par(mfrow=c(1,1)) # 一行一列的图 boxplot(airquality$Temp) par(mfcol=c(2,1)) # 两行一列的图 hist(airquality$Temp) hist(airquality$Wind)