赞
踩
首先下载包,绘制一张图:
#install.packages(c("Hmisc", "RColorBrewer")) #install.packages(c("vcd", "plotrix", "sm", "vioplot")) library(ggplot2, lib.loc=""~/R/lib"") #生存一个可以修改的当前图形参数列表 par(ask=TRUE) opar <- par(no.readonly=TRUE) #载入数据集 attach(mtcars) # mtcars是R中自带的数据集,包含多种汽车的数据 plot(wt, mpg) abline(lm(mpg~wt)) #abline是画一条或多条直线,lm是线性模型 title("Regression of MPG on Weight") #不用了一定要detach detach(mtcars)
字体、颜色、坐标轴、标签等通过**函数par()**来指定选项。以这种方式设定的参数值除非被再次修改,否则将在
会话结束前一直有效。其调用格式为:
par(optionname=value,optionname=name,...)
不加参数地执行par()将生成一个含有当前图形参数设置的列表。添加参数no.readonly=TRUE可以生成一个可以修改的当前图形参数列表
例如一个药品的例子:
# 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) #很重要
对上面的例子,现修改一些参数:
par(lty=2, pch=17) # change line type and symbol
plot(dose, drugA, type="b") # generate a plot
par(opar) # restore the original settings,恢复原设置
plot(dose, drugA, type="b", lty=3, lwd=3, pch=17, cex=2)
# choosing colors
library(RColorBrewer)
n <- 7
mycolors <- brewer.pal(n, "Set1")
barplot(rep(1,n), col=mycolors)
n <- 10
mycolors <- rainbow(n)
pie(rep(1, n), labels=mycolors, col=mycolors)
把饼图改成灰色:
mygrays <- gray(0:n/n)
pie(rep(1, n), labels=mygrays, col=mygrays)
# View font families
opar <- par(no.readonly=TRUE)
par(cex=1.5)
plot(1:7,1:7,type="n")
text(3,3,"Example of default text")
text(4,4,family="mono","Example of mono-spaced text")
text(5,5,family="serif","Example of serif text")
par(opar)
仍用上方药物例子:
par(lwd=2, cex=1.5)
par(cex.axis=.75, font.axis=3)
plot(dose, drugA, type="b", pch=19, lty=2, col="red")
plot(dose, drugB, type="b", pch=23, lty=6, col="blue", bg="green")
par(opar)
par(pin=c(2, 3))#图形大小
title(main="maintitle", sub="subtitle", xlab="x-axis label", ylab="y-axis label")
plot(dose, drugA, type="b",
col="red", lty=2, pch=2, lwd=2,
main="Clinical Trials for Drug A",
sub="This is hypothetical data",
xlab="Dosage", ylab="Drug Response",
xlim=c(18, 60), ylim=c(0, 70)) #x轴刻度大小
你可以使用函数axis()来创建自定义的坐标轴,而非使用R中的默认坐标轴,其格式为:
axis(side, at=, labels=, pos=, lty=, col=, las=, tck=, ...) #例子在下面
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。