【问题标题】:R - barplot overlay with bars of different widthR - 具有不同宽度条形的条形图叠加
【发布时间】:2017-10-12 23:13:35
【问题描述】:

我知道有人问过类似的问题,但提供的答案都没有解决我在 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 的回复之后添加的

我想看的是以下,了解为barplot设置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

【问题讨论】:

    标签: r width overlay bar-chart


    【解决方案1】:

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

    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 获取右侧。)

    您可以计算出这应该可行。 barplot 的文档说:

    空格

    每个条之前剩余的空间量(作为平均条宽度的一部分)。

    第一个条形图使用默认值,因此 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)
    

    【讨论】:

    • 嗨@G5W,非常感谢您的回复。正如我在上面写的,我知道(spaces = ...) 选项。但是,对于宽度的每一次变化,我都必须执行一个试错系列,以便将第二个图与第一个图对齐。我的问题:有没有办法计算它(自动化它)或者是尝试使用width =spaces = 测试行为的唯一方法?
    • 非常感谢@G5W 的第二次详细回答!即使我已经阅读了 barplot 的文档(可能不是“the”而是“a” doc),我也必须监督您发布的句子。现在,这很清楚!而你的信息正是我所需要的。
    猜你喜欢
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 2019-08-04
    • 2013-03-22
    • 1970-01-01
    • 1970-01-01
    • 2016-02-06
    • 2021-05-17
    相关资源
    最近更新 更多