在条形图中指定条形之间的间距 [英] Specify spaces between bars in barplot

查看:74
本文介绍了在条形图中指定条形之间的间距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 R 生成一个条形图,条形图的宽度和它们之间的间距不同.例如我有一个矩阵

I am trying to generate a barplot with R with different widths of the bars and different spaces between them. For example I have a matrix

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

我想生成这样的图形(对不起草图):

And I would like to generate a figure like this (sorry for the sketch):

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

据我所知,barplot() 允许您指定宽度,但条形之间的空间只能表示为平均条形宽度的一小部分.但是,我想为条形之间的空格指定特定的(整数)数字.我将不胜感激任何提示/想法!

As far as I understand, barplot() allows you to specify the width but the space between the bars can only be expressed as a fraction of the average bar width. However, I would like to specify specific (integer) numbers for the spaces between the bars. I'll appreciate any hints/ideas!

推荐答案

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

One way of getting what you want is to create dummy, empty bars. For example,

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

这篇关于在条形图中指定条形之间的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆