2013-08-16 221 views
1

我的R中的數據是這樣的:如何合併R中的barplot中的列標籤(堆疊barplot)?

1-12 1-12 1-15 1-15 1-20 1-20 2-6 2-6 3-1-1 3-1-1 3-1 3-1 3-2 3-2 3-3 3-3 
N 0 0 14 0 17 0 9 0 27  0 9 0 13 0 33 0 
P 0 0 0 12 0 12 0 5  0 13 0 6 0 0 0 9 
F 0 0 0 0 0 0 0 2  0 14 0 0 0 6 0 20 

通過dput(數據)的結果R是:

structure(c(0L, 0L, 0L, 0L, 0L, 0L, 14L, 0L, 0L, 0L, 12L, 0L, 
17L, 0L, 0L, 0L, 12L, 0L, 9L, 0L, 0L, 0L, 5L, 2L, 27L, 0L, 0L, 
0L, 13L, 14L, 9L, 0L, 0L, 0L, 6L, 0L, 13L, 0L, 0L, 0L, 0L, 6L, 
33L, 0L, 0L, 0L, 9L, 20L), .Dim = c(3L, 16L), .Dimnames = list(
c("N", "P", "F"), c("1-12", "1-12", "1-15", "1-15", "1-20", 
"1-20", "2-6", "2-6", "3-1-1", "3-1-1", "3-1", "3-1", "3-2", 
"3-2", "3-3", "3-3"))) 

,我的代碼是:

barplot(data,space=c(1,0.25),legend=rownames(data),col=c('white','black','grey'),las=2) 

它看起來像一個正常的條形圖,每一列在底部都有一個標籤...

但是,那裏在x軸上有太多的標籤,我想將兩列的名稱合併爲一個(因爲它們具有相同的名稱),即在前兩列的中間,只有一個標籤「1-12 「在底部,總共會有八個標籤。我怎樣才能做出這個改變?

非常感謝!

+0

我不想猜測這可能是什麼結構,然後嘗試將它拼接在一起,矩陣或故障rmed data.frame或其他什麼,那麼爲什麼你不要在這個對象上發佈dput(。)? –

+0

@Dwin - 同意。如果barplot已經在工作,它必須是一個矩陣。 – thelatemail

+0

是的,因爲我仍然不能把圖像的問題,我張貼dput() – user2687500

回答

2

如果你只是希望讓每個組下的居中標籤,你可以這樣做:

# suppress the x-axis and save your original plot's bar locations 
bp <- barplot(data,space=c(1,0.25),legend=rownames(data), 
      col=c('white','black','grey'),las=2, 
      xaxt="n") 

# draw a new axis using these values 
axis(1,at=rowMeans(matrix(bp,ncol=2,byrow=TRUE)), 
    labels=unique(colnames(data)),lty=0) 

enter image description here

忽略過程的重疊傳說......這可以通過固定在原始barplot中使用legend.args將其更好使用

+0

感謝您對dput的建議!我把dput的結果放在問題中,所以你可以看看它......實際上我使用data = rbind(F,P,N),然後使用colnames(data)= c(「1-12」,「 15「,」1-20「)來重命名列。 但是,我不想生產一個將P,F,N堆疊在一起的列,我的柱狀圖就像所有的「N」在一個列中,「F」「N」在一個列中組合在一起。這與barplot無關,我只是想改變x軸的標籤。 – user2687500

+0

@ user2687500 - 我已編輯,希望能更好地解決您的問題。 – thelatemail