R語言繪製座標 儲存圖片
阿新 • • 發佈:2019-02-03
繪製座標
有時候我們繪製座標的時候,需要改變座標軸的定義,我們以日期為橫座標繪製時間序列為例講解 先列舉簡單的例子: 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"))
自動儲存成圖片檔案
在 plot 的上方和下方加入 png function 和 devoff():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)
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 的時候 可以指定解析度, 可以看出這個圖片比之前的小一些