Footsteps on my way !
perl/linux/测序分析

带方差的柱状图

方法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 <-read.table(
  "D:/path/to/PhD_fig22D.csv",
  header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE
) #数据导入(后两列是你要画的方差值)
attach(fig22d)
superpose.eb <- function (x, y, ebl, ebu = ebl, length = 0.08, ...)
  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 <- barplot(
  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 <- function(x, y, upper, lower=upper, length=0.1,...){
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 <- rnorm(50000, mean=1)
y <- matrix(y,10000,5)
y.means <- apply(y,2,mean)
y.sd <- apply(y,2,sd)
y1 <- rnorm(50000, mean=1.1)
y1 <- matrix(y1,10000,5)
y1.means <- apply(y1,2,mean)
y1.sd <- apply(y1,2,sd)
yy <- matrix(c(y.means,y1.means),2,5,byrow=TRUE)
ee <- matrix(c(y.sd,y1.sd),2,5,byrow=TRUE)*1.96/sqrt(10000)
barx <- barplot(yy, beside=TRUE,col=c("blue","magenta"), ylim=c(0,1.5), names.arg=1:5, axis.lty=1, xlab="Replicates", ylab="Value (arbitrary units)")
error.bar(barx,yy,ee)

来源:
http://egret.psychol.cam.ac.uk/statistics/R/graphs1.html
http://monkeysuncle.stanford.edu/?p=485

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址