R语言plot作图

plot函数:在活动图形窗口中绘制带有轴和标题等装饰的散点图。

plot(x, y = NULL, type = "p",  xlim = NULL, ylim = NULL,
     log = "", main = NULL, sub = NULL, xlab = NULL, ylab = NULL,
     ann = par("ann"), axes = TRUE, frame.plot = axes,
     panel.first = NULL, panel.last = NULL, asp = NA,
     xgap.axis = NA, ygap.axis = NA,
     ...)

1.  基本用法

plot(100:125)  # x 为1:26
plot(rnorm(1000)) # x 为1:1000

x<-rnorm(100)
y<-2*x+rnorm(100)
plot(x,y,main="Linearly correlated random     numbers",xlab="x",ylab="2*x+noise",xlim=c(-4,4),ylim=c(-4,4))

2. 画多个子图

par(mfrow=c(1,2)) # 一行,每行两个图

par(mfrow=c(2,3)) # 两行,每行三个图

3. 点上加文字

plot(1:10,1:10,xlim=c(0,12),ylim=c(0,12))
text(1:10+1,1:10,paste("point",c(1:10)))

4.  设置点形状

pch 参数控制点形状, 0:25 数字表示,形状如下。

plot(0:25,pch=0:25,main="pch character",xlim=c(0,30),xlab="pch code")
text(0:25+3,0:25,0:25)

plot(0:25,pch="$") # 指定形状

5. 设置点颜色

plot(0:10,0:10,col='red')

colors() # 查看颜色

除了名称外,同样可以用下标,十六进制颜色值,RGB值和HSV值来指定颜色。例子:col=1、col="white"、col="#FFFFFF"、col=rgb(1,1,1)和col=hsv(0,0,1)。

另外,R中还有许多生成颜色变量的函数。有rainbow()、heat.colors()、terrain.colors()、topo.colors()、cm.colors()方法,gray()方法生成多阶灰度色。

6. 设置点大小

par(mfrow=c(1,2))
plot(0:10,0:10) # 默认cex=1
plot(0:10,0:10,cex=2)

7. 设置图形类型

type参数:

“p”点图
“l”线图
“b”点线图,线不穿过点
“c”虚线图
“o”点线图,线穿过点
“h”直方图
“s”阶梯图
“S”步骤图
“n”无图

x<-c(1:10)
par(mfcol=c(3,3))
plot(x,type="p",main="p")
plot(x,type="l",main="l")
plot(x,type="b",main="b")
plot(x,type="c",main="c")
plot(x,type="o",main="o")
plot(x,type="h",main="h")
plot(x,type="s",main="s")
plot(x,type="S",main="S")
plot(x,type="n",main="n")

8.  画多个多个颜色,形状

x <- 1:10
y <-1:10
plot(x,y,pch=ifelse(x*y>25,16,1),main="scatter plot with conditional point styles")
plot(x,y,col=ifelse(x*y>25,'red','blue'),main="scatter plot with conditional colors")

9. 参考线

plot(1:10)
abline(h = c(1, 5))  #则在y=1和5处各有一条水平线
abline(v = c(1, 5))  #则在x=1和5处各有一条垂直线

10. 其他参数

cex.axis:坐标轴刻度文字的缩放倍数
cex.lab:坐标轴标签(名称)的缩放倍数
cex.main:标题的缩放倍数
cex.sub:副标题的缩放倍数
font:整数。用于指定字体样式。1常规、2粗体、3斜体、4粗斜体

xlab

a label for the x axis, defaults to a description of x.

ylab

a label for the y axis, defaults to a description of y.

xlim

the x limits (x1, x2) of the plot. Note that x1 > x2 is allowed and leads to a ‘reversed axis’.

The default value, NULL, indicates that the range of the finite values to be plotted should be used.

ylim

the y limits of the plot.

  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值