[英]R - add labels to the middle of barplot with multiple bars
我嘗試使用barplot
繪制帶有多排條形圖的barplot
。
x11()
X=c('May','July','September','November')
Y1=c(1,1,3,5)
Y2=c(5,5,6,7)
Y3=c(9,8,0,2)
C=rbind(Y1,Y2,Y3)
l <- c()
a=b=l
i <- 1
while(i<=length(X)) {
a[[i]] <- ''
b[[i]] <- X[i]
i <- i + 1
}
l=rbind(a,b,a,a)
plot(0, xlim=c(1,(length(Y1)*4)), ylim=c(0,10), )
bplot<-barplot(C,xaxt="n",beside=T, xlim=c(1,(length(Y1)*4)), ylim=c(0,10), lty=1, las=1)
axis(1, at=1:(length(Y1)*4), cex.axis=1, labels=l, tck=0)
Sys.sleep(40)
但是,據我所知,並不是所有的標簽都會出現,因為它們的寬度太大(可能會重疊),並且plot
會掉落它們。 我必須為那些不應該帶有標簽(左側,右側和間隙)的條創建額外的空白標簽。 如果我使用axis
cex.axis
屬性使字體變小,則與其他元素和丑陋的字體相比,它會成比例地變小。
我已經嘗試過解決該問題的解決方案,例如將xaxt="n"
添加到barplot
,設置las=2
, axis(1, at=1:4, cex.axis=1, labels=b, tck=0)
,設置names.arg
中的barplot
,但無濟於事。
快要到了:需要用以下兩個命令交換axis
命令:
bplot=bplot[2,]
axis(1, at=bplot, cex.axis=1, labels=X, tck=0)
barplot
被迫生成太多標簽,無法將它們放置在一條水平線上。
bplot
[,1] [,2] [,3] [,4]
[1,] 1.5 5.5 9.5 13.5
[2,] 2.5 6.5 10.5 14.5
[3,] 3.5 7.5 11.5 15.5
因此,僅剪切所需的中心標簽位置( bplot
第二行)並粘貼標簽。
如果要使X軸與Y軸交叉,可以通過添加不可見標簽來使其更長:
# those two commands two continue the Xaxis to the crossing with Y axis
bplot=cbind(-1,bplot)
b=cbind('',b)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.