【发布时间】: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设置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)
[2
【问题讨论】: