图形初阶
3.1 使用图形
在交互对话中,通过逐条语句构建图形,例如:
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
通过图形用户界面保存图形,或者通过代码保存图形到当前工作目录,例如:
#保存绘制的图形到当前工作目录
#attach()将数据框添加到搜索路径中;detach()将数据框从搜索路径中移除
pdf("mygraph.pdf")
attach(mtcars)
plot(wt, mpg)
abline(lm(mpg~wt))
title("Regression of MPG on Weight")
detach(mtcars)
dev.off()
创建多个图形并随时查看每一个图形,方法有三:1)创建新图形之前打开一个新的图形窗口.
2)通过图形界面查看多个图形(Mac中是command+⬅️/command+➡️)
3)使用函数dev.new()、dev.next()、dev.prev()、dev.set()和dev.off()同时打开多个图形窗口,并选择将哪个输出发送到哪个窗口。(详情参考help(dev.cur)
3.2 图形参数
使用par()函数。
example:
# Input data for drug example
dose <- c(20, 30, 40, 45, 60)
drugA <- c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
plot(dose, drugA, type="b")
#图形参数
opar <- par(no.readonly=TRUE)
# make a copy of current settings
par(lty=2, pch=17)
# change line type and symbol
plot(dose, drugA, type="b")
# generate a plot
par(opar)
# restore the original settings