我需要绘制按2(左手与右手)分组的4个变量的均值和标准误差。 数据如下:
left_start_mydata = read.table(text="condition force
right_small 1.80523635404968
right_small 2.6420765093878
right_small -0.814658993753841
right_small -2.60104096307957
right_small -1.98589533137477
right_small 3.40251831946075
right_small -0.320129242153803
right_small -2.98033170716285
right_small 1.89317065279704
right_small -3.84882524848594
right_small -3.98968367934259
right_small 1.10427581334271
right_large -1.75347355221301
right_large 0.791286271808679
right_large -2.0073148173165
right_large -5.03908061365724
right_large -3.21618785397385
right_large 3.15958835997412
right_large -0.728320450803572
right_large -0.754841068944837
right_large 1.26489177600709
right_large -1.25150854925629
right_large 2.91927950249639
right_large 0.343070062995591
left_small 2.76611178207954
left_small 1.98555350876524
left_small 1.90443573003935
left_small 0.939363367617274
left_small 1.47248738494375
left_small -1.04761679029031
left_small -0.824572467883381
left_small -1.54423800803017
left_small 1.5187848305815
left_small 1.0956007263072
left_small 3.89244539291397
left_small 1.72801660622873
left_large 0.902501901614639
left_large 2.89567274148723
left_large -0.503732000967399
left_large -2.87429518370343
left_large -1.85785327815289
left_large -4.73265776308004
left_large -0.752958593136438
left_large 2.47010977406911
left_large -1.19149141260447
left_large -0.396960252581726
left_large 1.54175722591051
left_large 2.05533917545533
",header=TRUE)
下一步,我将为每个条件计算描述性统计数据:
attach(left_start_mydata)
left_start_mean_force = tapply(force, INDEX=condition, mean) #means
left_start_sem_force = tapply(force,INDEX=condition,sd)/ sqrt(tapply(force,condition, length) ) #stand_errors
现在我绘图:
barcols = c("red","blue")
sapply(2,
function(x) {
mids = barplot(matrix(left_start_mean_force,
nrow=2,
byrow=TRUE),
ylim=c(-2,3),
beside=TRUE,
col=barcols)
axis(1,at=colMeans(mids),
c("left hand","right hand"),lwd=0,lwd.tick=0)
abline(h=0)
arrows(mids, left_start_mean_force - left_start_sem_force,
mids, left_start_mean_force + left_start_sem_force,
code = 3,
angle = 90,
length = 0.1,
lwd = 2)
}
)
我几乎得到了我所需要的(请参见下图)。
但是!如果查看右边的条,您会看到红色的条(应该代表条件“ right_large”)实际上比旁边的蓝色条(“ right_small”)低,而实际值则更高(即接近零):
> left_start_mean_force
left_large left_small right_large right_small
1.2812381 -0.6430682 -0.5242566 -0.6063786
似乎这两列都以某种方式被改组了。该问题仅针对平均值出现。标准错误可以正确表示,即,左侧显示“ right_large”,右侧显示“ right_small”。
出什么问题了?我认为,必须是带有sapply的barplot函数。
P.S .:请不要建议我ggplot和其他软件包,我敢肯定有一个具有标准功能的简单解决方案。
答案 0 :(得分:2)
您的数据分组不正确。您需要在代码的这一部分中将byrow
设置为FALSE
:
mids = barplot(matrix(left_start_mean_force,
nrow=2,
byrow=FALSE), # <<<<<< HERE
ylim=c(-2,3),
beside=TRUE,
col=barcols)
将矩阵传递给barplot
时,它将按列而不是行对值进行分组。
m = matrix(1:4, nrow=2)
barplot(m, beside=T)
# m is:
# 1 3
# 2 4
另一方面,您正在做的事情与此类似:
m = matrix(1:4, nrow=2, byrow=T)
barplot(m, beside=T)
# m is:
# 1 2
# 3 4