代码之家  ›  专栏  ›  技术社区  ›  Honigdachs

不同宽度条的R-条形图叠加

  •  2
  • Honigdachs  · 技术社区  · 8 年前

    我知道有人问过类似的问题,但提供的答案中没有一个用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
    

    enter image description here

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

    ==>这是在@G5W的响应之后添加的

    我想看到的是,了解设置的系统 width space 条形图选项。

    对于条形图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)
    

    [ enter image description here 2

    1 回复  |  直到 8 年前
        1
  •  3
  •   Community Mohan Dere    6 年前

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

    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)
    

    Spaced Barplot

    基于问题澄清的补遗:

    无论你做了多少条,如果你坚持宽度为0.5 第二条,你可以使用 spacing = space=c(0.9,rep(1.4,length(number_aa)-1)) 使第二根杆位于第一根杆的中心。(填写第一个数字 0.4在左边,1.4在右边。)

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

    空间

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

    第一个条形图使用默认值 width = 1 space = 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,对齐 以第一组的第一根杆为中心。因为我们正在使用 空间=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)
    

    Center bars