helixcn
下面是几个实例,将下面所有代码拷贝到R-console中,即可得到相应的图形。
第一
#直方图添加误差线函数
error.bars<-function(yv,z,nn){
xv<-
barplot(yv,ylim=c(0,(max(yv)+max(z))),names=nn,ylab=deparse(substitute(yv)
))
g=(max(xv)-min(xv))/50
for (i in 1:length(xv)) {
lines(c(xv,xv),c(yv+z,yv-z))
lines(c(xv-g,xv+g),c(yv+z, yv+z))
lines(c(xv-g,xv+g),c(yv-z, yv-z))
}}
#举例:
#数据输入
biomass<-c(551, 457, 450, 731, 499, 632, 595, 580, 508, 583, 633, 517, 639, 615, 511, 573, 648, 677, 417, 449, 517, 438, 415, 555, 563, 631, 522, 613, 656, 679)
clipping<-as.factor(c("n25", "n25", "n25", "n25", "n25", "n25", "n50", "n50", "n50", "n50", "n50", "n50", "r5", "r5", "r5", "r5", "r5", "r5", "control","control","control","control","control","control","r10", "r10", "r10", "r10", "r10", "r10"))
comp<-data.frame(biomass,clipping)
attach(comp)
names(comp)
se<-rep(28.75,5)
labels<-as.character(levels(clipping))
ybar<-as.vector(tapply(biomass,clipping,mean))
error.bars(ybar,se,labels)
结果如下
第二
# 数据点添加误差线
xy.error.bars<-function (x,y,xbar,ybar){
plot(x, y, pch=16, ylim=c(min(y-ybar),max(y+ybar)),
xlim=c(min(x-xbar),max(x+xbar)))
arrows(x, y-ybar, x, y+ybar, code=3, angle=90, length=0.1)
arrows(x-xbar, y, x+xbar, y, code=3, angle=90, length=0.1) }
# 数据输入
x <- rnorm(10,25,5)
y <- rnorm(10,100,20)
xb <- runif(10)*5
yb <- runif(10)*20
xy.error.bars(x,y,xb,yb)
第三
利用sciplot包中的bargraph.CI(), lineplot.CI()函数
library(sciplot)
#直方图添加误差线
bargraph.CI(x.factor = clipping, response =biomass, data = comp)
#折线图添加误差线
lineplot.CI(x.factor = clipping, response =biomass, data = comp)