我想在条形图上方显示的条形图上显示正数,在条形图下方显示负数。无论如何在R中这样做?感谢
par(mfrow=c(2,1))
HOLD <- matrix(c(0.711993046435892, 0.732294839190538,
0.393938967564015, -0.456254046041675,
-0.284020839130226, 0.544765413396821,
0.349792656944892, 0.395227638605604,
0.55130390493582, -0.513905184144522,
0.0989034381192891, 0.0908636716369119), ncol=6, nrow=2)
for (i in 1:2) {
BP <- barplot(HOLD[,i], main=M[[i]], ylab="%", ylim=c(-1, 1), col=SEQUENTIAL)
text(BP, HOLD[,i], labels=HOLD[,i], pos=3, offset=0.8)
}
答案 0 :(得分:1)
HOLD <- matrix(c(0.711993046435892, 0.732294839190538,
0.393938967564015, -0.456254046041675,
-0.284020839130226, 0.544765413396821,
0.349792656944892, 0.395227638605604,
0.55130390493582, -0.513905184144522,
0.0989034381192891, 0.0908636716369119), ncol=6, nrow=2)
HOLD <- data.frame(HOLD)
HOLD <- stack(HOLD)
HOLD$grp2 <- factor(1:2)
offset <- 0.05
library('ggplot2')
ggplot(data = HOLD, mapping = aes( x = ind, y = values, fill = grp2 ) ) +
geom_bar(stat = 'identity', position = 'dodge' ) +
geom_text( aes( x = ind,
y = ifelse( sign( values ) > 0, values + offset, values - offset ),
label = round( values, 2 ) ),
position = position_dodge( width = 1 ),
size = 3)
答案 1 :(得分:0)
使用基础图可以将向量传递给pos
参数:
for (i in 1:2) {
BP <- barplot(HOLD[,i], main=M[[i]], ylab="%", ylim=c(-1, 1), col=SEQUENTIAL)
text(BP, HOLD[,i], labels=HOLD[,i], pos=ifelse(HOLD[i,] >= 0, 3, 1),
offset=0.8)
}