R - 条形图覆盖不同宽度的条形图

R - barplot overlay with bars of different width

我知道有人问过类似的问题,但所提供的答案都没有解决我在 R 中叠加条形图的问题。

我想用两个系列的 y 值生成一个条形图,它们具有相同的 x 值。

第二组 y 值表示第一组 y 值的子集。因此,我想将第一个系列 1 绘制为条形图,然后叠加系列 2。

我想展示的是 y2 系列的叠加层,其条形​​尺寸仅为 y1 系列的一半(设置宽度 = 0.5)。 不幸的是,这会将第二个系列的所有条形图向左移动。

然后我可以尝试为空间创建向量 (spaces = c(0.9, 1.4, 1.4, 1.4)),但我无法将其自动化。

任何人都可以提出一个聪明的解决方案吗?

非常感谢您, 维尔纳

# x-values
number_aa <-  c(6, 7, 8, 9, 10)
# y1-values
peptide_total <- c(62040, 57755, 50053, 45077, 39011)
# y2-values
peptide_unique <- c(56791, 54978, 47943, 43248, 37658)

# determine ymax (from y1-values)
ymax <- peptide_total[which.max(peptide_total)]
ymax

# determine xmax (from x-values)
xmax <- number_aa[which.max(number_aa)]
xmax

# assign vector of midpoint-values of first barplot (y1) to x 
x <- barplot(peptide_total, names.arg = number_aa,
         xlim = c(0, xmax), ylim = c(0, ymax),
         width = 1)

# set plotting to overlay (no new plot)
par(new = TRUE)

barplot(peptide_unique, col = 'black', names.arg=x,
    xlim = c(0, xmax), ylim = c(0, ymax),
    width = 0.5,
    axisnames = FALSE)

# ----> second bar plot is not aligned on first barplot

============================================= ===========================

===>这是在@G5W

回复后添加的

我想看到的是以下内容,了解为条形图设置 widthspace 选项的系统。

对条形图 2 使用以下代码:

# creating second barplot
space_vector = c(0.9, rep(1.4, 4))

barplot(peptide_unique, col = 'black', names.arg=x,
    xlim = c(0, xmax), ylim = c(0, ymax),
    width = 0.5, space = space_vector,
    axisnames = FALSE)

[2

对于第二个条形图,增加间距。

barplot(peptide_unique, col = 'black', names.arg=x,
    xlim = c(0, xmax), ylim = c(0, ymax),
    width = 0.5, space=c(0.4,rep(1.4,4)),
    axisnames = FALSE)

基于问题澄清的附录:

无论你做了多少条,如果你坚持 width=0.5 第二栏,您可以使用 spacing = space=c(0.9,rep(1.4,length(number_aa)-1)) 将第二个栏放在第一个栏的中心。 (做第一个数 0.4 让它在左边,1.4 让它在右边。)

您可以计算出这应该有效。条形图的文档说:

space

the amount of space (as a fraction of the average bar width) left before each bar.

第一个条形图使用默认值 width = 1space = 0.2。 第一个条从 0.2 开始。中心将位于 0.2 + 0.5 = 0.7。 每个后续条移动 1.2(0.2 间距 + 1.0 条宽度)。

由于第二组的所有条形宽度均为 0.5,因此平均值也是 0.5。 您希望第二组的每个条都以第一组的条为中心。 如果您对第一个柱使用 spacing = 0.9,则其左边缘将位于
spacing * bar_width = 0.9 * 0.5 = 0.45。移动到栏的中心将添加 0.5*0.5 = 0.25 所以第一个条形的中心将在 0.7 对齐 与第一组的第一条的中心。由于我们正在使用 space = 1.4 对于第二组中的其余柱,每个柱都会移动 spacing * bar_width + bar_width = 1.4*0.5 + 0.5 = 1.2 同意 第一组中每个柱移动的量。

为了证实这个计算,你可以生成很多垃圾数据集 使用不同数量的条并确认它们排成一行。这是代码 像你的,但这些价值观没有真正的意义,它们只是说明性的。 尝试将 NumTypes = 10 的值更改为各种值以说服自己 这是正确的。

NumTypes = 11
number_aa <-  6:(5+NumTypes)
peptide_total  <- sort(rnorm(NumTypes, 51000,9000), decreasing=TRUE)
peptide_unique <- sort(rnorm(NumTypes, 47000,8000), decreasing=TRUE)

# determine ymax (from y1-values)
ymax <- peptide_total[which.max(peptide_total)]
ymax

xmax <- number_aa[which.max(number_aa)]
xmax

# assign vector of midpoint-values of first barplot (y1) to x 
x <- barplot(peptide_total, names.arg = number_aa,
         xlim = c(0, xmax), ylim = c(0, ymax),
         width = 1)

# set plotting to overlay (no new plot)
par(new = TRUE)

barplot(peptide_unique, col = 'black', names.arg=x,
    xlim = c(0, xmax), ylim = c(0, ymax),
    width = 0.5, space=c(0.9,rep(1.4,length(number_aa)-1)),
    axisnames = FALSE)