R barplot + sapply shuffles bars(虽然它不应该)
R barplot + sapply shuffles bars (although it shouldn't)
我需要绘制按 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"。
有什么问题?我想,它必须是带有 barplot 函数或 with sapply 的东西。
P.S.: 请不要向我推荐 ggplot 和其他软件包,我确信有一个具有标准功能的简单解决方案。
您的数据分组不正确。您需要在这部分代码中将 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
我需要绘制按 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"。
有什么问题?我想,它必须是带有 barplot 函数或 with sapply 的东西。
P.S.: 请不要向我推荐 ggplot 和其他软件包,我确信有一个具有标准功能的简单解决方案。
您的数据分组不正确。您需要在这部分代码中将 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