方法1: 算好方差,通过箭头模拟方差区间:
数据:PhD_fig22D.csv,内容如下:
group,CSpluslatency,CSminuslatency,CSplusSEM,CSminusSEM
sham,3.884335347,4.212530713,0.224840383,0.259312234
ACCX,4.672486108,3.61943375,0.347628945,0.490227261
fig22d
"D:/path/to/PhD_fig22D.csv",
header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE
) #数据导入(后两列是你要画的方差值)
attach(fig22d)
superpose.eb
arrows(x, y + ebu, x, y - ebl, angle = 90, code = 3,
length = length, ...) # 函数:画方差箭头
# our data begin as:
# group CSpluslatency CSminuslatency CSplusSEM CSminusSEM
# 1 sham 3.884335 4.212531 0.2248404 0.2593122
# 2 ACCX 4.672486 3.619434 0.3476289 0.4902273
#
# step 1: reorganize
latencies = t(matrix( c(CSpluslatency, CSminuslatency), 2, 2)) # create a 2-by-2 matrix, then transpose it
colnames(latencies) = group
rownames(latencies) = c("CS+","CS-")
sems = t(matrix(c(CSplusSEM, CSminusSEM), 2, 2)) # do the same for the SEMs
colnames(sems) = group
rownames(sems) = c("CS+","CS-")
# latencies, for example, is now:
# sham ACCX
# CS+ 3.884335 4.672486
# CS- 4.212531 3.619434
fillcolours = c("gray","white")
x.abscis
latencies, beside=TRUE,
col=fillcolours,
space=c(0.4,2), # spacing between bars in the same group, and then between groups 设置柱间距为0.4,组间距为2
ylim=c(0,5.5),
ylab="Latency to approach (s)",
main="Autoshaping approach latencies"
)
box(bty="L") #画底线
superpose.eb(x.abscis, latencies, ebl=0, ebu=sems) # +1 SEM, no descending error bar(只有上限没有下限,要下限的话 设置 ebl=sems即可)
# Note that the X coordinates were stored in x.abscis
legend(x=5, y=5.5, box.lty=0, legend=rownames(latencies), fill=fillcolours)
# Axis breaks, not reproduced here, are discussed at:
# http://tolstoy.newcastle.edu.au/R/help/05/11/15877.html
# https://stat.ethz.ch/pipermail/r-help/2005-May/072058.html
# Most use axis.break from the plotrix library for the break drawing itself.
# Others use gap.plot, gap.barplot, gap.boxplot from the same package.
# However, these options need a bit of careful massaging to get right. A simple example is shown below.
detach(fig22d) # clean up
rm(fig22d, latencies, sems, fillcolours, x.abscis)
方法2: 使用error bar函数,其实和方法1一样
error.bar
if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))
stop("vectors must be same length")
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)
}
y
y
y.means
y.sd
y1
y1
y1.means
y1.sd
yy
ee
barx
error.bar(barx,yy,ee)