R- scatter plots

目录

  • 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)
cars数据集
plot(x,y)
plot(x = cars$speed, y = cars$dist)
speed为x,dist为y
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
dist为x,speed为y
1 添加x、y标签
#添加x、y标签
plot(x = cars$speed, y = cars$dist, xlab = "Speed", ylab = "Distance")
添加x、y标签
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))
添加xy轴显示限制
6 图状、点状、线状
type 图形状9种

pch 点形状25种

lty 线形状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()
R平滑曲线
8 画函数图像
plot(sin, -pi, 2*pi) # see ?plot.function
sin(x), x [-pi, 2*pi]
# 泊松分布100个观测值,lambda = 5
plot(table(rpois(100, 5)), type = "h", col = "red", lwd = 5,
     main = "rpois(100, lambda = 5)")

泊松分布

参考文章:
coursera R programming week4

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容