目录
- Generic X-Y plot定义
- 1 添加x、y标签
- 2 添加主标题,参数main
- 3 添加副标题,参数sub
- 4 添加颜色,参数col
- 5 添加xy轴显示限制,参数xlim
- 6 图状、点状、线状
- 7 平滑曲线
- 8 画函数图像
Generic X-Y plot定义
plot(x, y, ...) # ?plot 请查看帮助文档
代码及结果:
#打开数据集前6行
head(cars)
plot(x,y)
plot(x = cars$speed, y = cars$dist)
plot(x = cars$dist, y = cars$speed)
#It probably makes more sense for speed to go on the x-axis since stopping distance is a function of speed #more than the other way around
1 添加x、y标签
#添加x、y标签
plot(x = cars$speed, y = cars$dist, xlab = "Speed", ylab = "Distance")
2 添加主标题,参数main
#添加主标题,参数main
plot(x = cars$speed, y = cars$dist,xlab = "Speed", ylab = "Stopping Distance", main = "My Plot")
3 添加副标题,参数sub
#添加副标题,参数sub
plot(cars, main = "My Plot", sub ="My Plot Subtitle")
4 添加颜色,参数col
#添加颜色,参数col
plot(cars, col = 2)
5 添加xy轴显示限制,参数xlim
#添加xy轴显示限制,参数xlim
plot(cars, xlim = c(10,15))
6 图状、点状、线状
# pch & lty &type &cex
plot(1:10,pch = c("@","$","#","&","%"), type = "b", lty = 3, cex =0.5, main = "Sarge says to Beetle...")
7 平滑曲线
require(stats) # for lowess, rpois, rnorm
plot(cars)
lines(lowess(cars)) #R平滑曲线的方法之一lowess()
8 画函数图像
plot(sin, -pi, 2*pi) # see ?plot.function
# 泊松分布100个观测值,lambda = 5
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 5,
main = "rpois(100, lambda = 5)")
参考文章:
coursera R programming week4