2016-02-21 36 views
0

我嘗試使用barplot來繪製幾行酒吧的圖。R - 將標籤添加到帶有多個酒吧的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) 

barplot

但並不是所有的標籤出現,我的理解,因爲他們在寬度過大(可以重疊)和plot丟棄它們。我不得不爲那些不應該有標籤(左邊的,右邊的和間隙)的條形創建更多的無效標籤。如果使用axiscex.axis屬性使字體變小,則與其他元素和lloks相比,它會變得不成比例地變小。

我試圖解決方案從that question,比如增加xaxt="n"barplot,設置las=2axis(1, at=1:4, cex.axis=1, labels=b, tck=0),在barplot設置names.arg,但沒有任何幫助。

回答

0

這幾乎有: 需要與這兩個指令交換axis命令:

bplot=bplot[2,] 
axis(1, at=bplot, cex.axis=1, labels=X, tck=0) 

barplot被迫generarating太多的標籤,讓他們放在一個水平線。

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第2行)和應用標籤。

barplot-resolved

如果你想X軸交叉Y軸,您可以通過添加無形標籤使其不再:

# those two commands two continue the Xaxis to the crossing with Y axis 
bplot=cbind(-1,bplot) 
b=cbind('',b)