2014-06-13 79 views
4

我試圖對分組的陰謀和切割y軸做一個陰謀。不過,我似乎無法得到兩者。使用此數據:y軸切割分組的陰謀陰影

d = t(matrix(c(7,3,2,3,2,2,852,268,128,150, 
       127,74,5140,1681,860,963,866, 
       470,26419,8795,4521,5375,4514,2487), 
      nrow=6, ncol=4)) 
colnames(d)=c("A", "B", "C", "D", "E", "F") 

我可以得到分組barplots這樣的:

barplot(d, beside = TRUE) 

Example

我可以用再拿到切y軸:

# install.packages('plotrix', dependencies = TRUE) 
require(plotrix) 
gap.barplot(as.matrix(d), 
      beside = TRUE, 
      gap=c(9600,23400), 
      ytics=c(0,3000,6000,9000,24000,25200,26400)) 

enter image description here

但是,我放棄了分組和A,B,C ...標籤。我怎麼能得到這兩個?

回答

5

您可以手動執行此操作。與barplot類似,?gap.barplot返回條形的中心位置使用這些添加標籤。

使用space作爲常規barplot組之間的間隔似乎不起作用。我們可以使用一排NAs來破解一個空間。

d = t(matrix(c(7,3,2,3,2,2,852,268,128,150, 
           127,74,5140,1681,860,963,866, 
           470,26419,8795,4521,5375,4514,2487), 
          nrow=6, ncol=4)) 
colnames(d)=c("A", "B", "C", "D", "E", "F") 

# add row of NAs for spacing 
d=rbind(NA,d) 

# install.packages('plotrix', dependencies = TRUE) 
require(plotrix) 

# create barplot and store returned value in 'a' 
a = gap.barplot(as.matrix(d), 
       gap=c(9600,23400), 
       ytics=c(0,3000,6000,9000,24000,25200,26400), 
       xaxt='n') # disable the default x-axis 

# calculate mean x-position for each group, omitting the first row 
# first row (NAs) is only there for spacing between groups 
aa = matrix(a, nrow=nrow(d)) 
xticks = colMeans(aa[2:nrow(d),]) 

# add axis labels at mean position 
axis(1, at=xticks, lab=LETTERS[1:6]) 
+0

非常好,可以在我們的空間羣體之間黑客以某種方式嗎? – jonalv

+0

是的,原來NA被繪製爲一個空間 – jonalv

2

與koekenbakker答案的幫助下,我想出了這個最後:

# install.packages('plotrix', dependencies = TRUE) 
require(plotrix) 

d = t(matrix(c(7,3,2,3,2,2,852,268,128,150, 
       127,74,5140,1681,860,963,866, 
       470,26419,8795,4521,5375,4514,2487), 
       nrow=6, ncol=4)) 

# Hack for grouping (leaves the extra space at the end) 
e = as.vector(rbind(d, rep(NA, 6)))[1:29] 

a = gap.barplot(ceiling(as.matrix(e/60)), 
       gap=c(160,390), 
       col=rep(c(grey.colors(4), 1), 6), 
       #space=rep(c(rep(0,3), 1), 6), 
       ytics=c(0,50,100,150,400,420,440), 
       xaxt='n') # disable the default x-axis 

xticks=c(2.5, 7.5, 12.5, 17.5, 22.5, 27.5) 

# add axis labels at mean position 
axis(1, at=xticks, LETTERS[1:6]) 

legend("topright", LETTERS[7:10], 
     bty="n", 
     fill=grey.colors(4)) 

enter image description here