#方法一:用par参数进行修改
#par()将生成一个含有当前图像参数设置的列表
#par(no.readonly = T)可以生成一个可以修改的当前参数列表
#输入运行数据
dose = c(20, 30, 40, 45, 60)
drugA = c(16, 20, 27, 40, 60)
drugB <- c(15, 18, 25, 31, 40)
drugc <- c(14, 16, 24, 27, 36)
#用par修改参数
opar = par(no.readonly = TRUE) #为后面恢复原始参数设置
par(lty=2, pch=17) #修改线性和点的形状
plot(dose, drugA, type = "b")
par(opar) #恢复成原始设置
#方法二:为高级函数提供optionname = value的键值对,制定的选项仅对这幅图像有效####
plot(dose, drugA, type = "b", lty=2, pch=17, main="dose vs. drug")
#见
png("难点二.png")
opar <- par(no.readonly = T)
par(lwd=2, cex=1.5, font.lab=2) #分别改变字体的大小和字体的样式
plot(dose, drugA, type = "b", pch=15, lty=1, col="red", ylim=c(0,60), main = "DrugA VS. DrugB", xlab = "Drug Dosage", ylab = "Drug Response")
lines(dose, drugB, type = "b", pch=17, lty=2, col="blue")
lines(dose, drugc, type = "b", pch=16, lty=3, col="yellow") #用line可以添加很多附加线
abline(h=c(30), lwd=1.5, lty=2, col="gray") #添加参考线
#添加次要刻度线
library(Hmisc)
minor.tick(nx=3, ny=3, tick.ratio = 0.5)
#添加图例
legend("topleft", inset = 0.05, border="red", title="Drug type", c("A","B","C"), lty = c(1,2,3), pch = c(15,17,16),col = c("red", "blue","yellow"))
par(opar)
dev.off()
#难点三:如何给点添加文本标注####
png("难点三.png")
attach(mtcars)
plot(wt, mpg,
main = "Mileage vs. Car Weight",
xlab="weight", ylab="Mileage",
pch=18, col="blue")
#用text函数给点添加文本
text(wt, mpg,
row.names(mtcars),
cex = 0.6, pos=4, col="red")
detach(mtcars)
dev.off()
#本实例展示输出为png格式,还可以输出png(),jpeg(),bmp()等格式
png("****.png")
plot(dose, drugA, type = "b", lty=2, pch=17, main = "dose vs. drug")
dev.off()
未完待续,敬请期待…
参考文档:
R in Action