R语言绘制坐标 保存图片

绘制坐标

有时候我们绘制坐标的时候,需要改变坐标轴的定义,我们以日期为横坐标绘制时间序列为例讲解

先列举简单的例子:


rnorm(n, mean = 0, sd = 1)

n 为产生随机值个数(长度),mean 是平均数, sd 是标准差 。
> rnorm(10,1,sd = 2)
 [1]  1.98984356 -1.93403191 -1.15324772  0.84623524 -0.73123833 -2.77682328 -0.74316683 -0.02913632 -0.80063421  3.23976243

用随机数绘制曲线:

require(stats) # for rnorm
plot(x=1:10, y=rnorm(10,1,sd = 2), type = 'l')

type 参数:
what type of plot should be drawn. Possible types are
"p" for points,
"l" for lines,
"b" for both,
"c" for the lines part alone of "b",
"o" for both ‘overplotted’,
"h" for ‘histogram’ like (or ‘high-density’) vertical lines,
"s" for stair steps,
"S" for other steps, see ‘Details’ below,

把上述内容绘制成时间序列

> plot(x=1:10, y=rnorm(10,1,sd = 2), type = 'l',xaxt='n') # 原坐标X 不绘制,否则会重叠
> axis(side = 1, at = c(2,4,6,8,10), labels = c("2016-05-24", "2016-05-26", "2016-05-30", "2016-06-01", "2016-06-03"))

自动保存成图片文件

png(filename = "Rplot%03d.png",
    width = 480, height = 480, units = "px", pointsize = 12,
    bg = "white", res = NA, family = "", restoreConsole = TRUE,
    type = c("windows", "cairo", "cairo-png"), antialias)
在 plot 的上方和下方加入 png function 和 devoff():
require(stats) # for rnorm
png(file='draw.png', bg="white", width = 300, height = 200)
plot(x=1:10, y=rnorm(10,1,sd = 2), type = 'l',xaxt='n', main = 'draw') # 增加坐标的 名称 draw
axis(side = 1, at = c(2,4,6,8,10), labels = c("2016-05-24", "2016-05-26", "2016-05-30", "2016-06-01", "2016-06-03"))
dev.off()



创建 png 的时候 可以指定分辨率, 可以看出这个图片比之前的小一些


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值