2013-05-10 26 views
4

我正嘗試用不同寬度的條和它們之間的不同空格生成帶有R的barplot。比如我有一個矩陣指定barplot中條間的空格

data <- matrix(c(1,2,2,4,7,1,11,12,3), ncol = 3, byrow = T) 
colnames(data) <- c("Start", "Stop", "Height") 

而且我想產生這樣一個數字(遺憾的草圖):

|         __ 
| __       | | 
| | |  ________   | | 
| | |  |  |   | | 
------------------- ------------------ 
0 1 2 3 4 5 6 7 8 9 10 11 12 

據我瞭解,barplot()允許您指定寬度但條之間的空間只能表示爲平均條寬的一部分。但是,我想爲條之間的空格指定特定的(整數)數字。 我會欣賞任何提示/想法!

+0

+1的真棒情節! :) – Arun 2013-05-10 12:55:11

回答

3

獲得您想要的一種方法是創建虛擬空欄。例如,

##h specifies the heights 
##Dummy bars have zero heights 
h = c(0, 2, 0, 1, 0, 3) 
w = c(1, 1, 2, 3, 4, 1) 

然後繪製使用barplot

##For the dummy bars, remove the border 
##Also set the space=0 to get the correct axis 
barplot(h, width=w, border=c(NA, "black"), space=0) 
axis(1, 0:14) 

enter image description here

+0

這解決了它!謝謝 – roschu 2013-05-10 12:43:42

1

如果通過mean(Width)劃分space參數,你還可以得到有:

data <- as.data.frame(data) 
data$Width <- with(data, Stop - Start) 
data$Space <- with(data, Start - c(0,head(Stop,-1))) 
with(data, barplot(Height, Width, space=Space/mean(Width), xlim=c(0,13))) 
axis(1,0:14) 

enter image description here