7

我正在尝试使用 R 生成一个条形图,其中条形的宽度不同,它们之间的空间也不同。例如我有一个矩阵

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() 允许您指定宽度,但条形之间的空间只能表示为平均条形宽度的一小部分。但是,我想为条形之间的空格指定特定的(整数)数字。我会很感激任何提示/想法!

4

2 回答 2

4

获得所需内容的一种方法是创建虚拟的空条。例如,

##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)

在此处输入图像描述

于 2013-05-10T12:38:09.177 回答
3

如果你将space论点除以mean(Width)你也可以到达那里:

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)

在此处输入图像描述

于 2014-01-29T02:50:25.260 回答