Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-不同宽度的条形图叠加_R_Width_Overlay_Bar Chart - Fatal编程技术网

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

R-不同宽度的条形图叠加,r,width,overlay,bar-chart,R,Width,Overlay,Bar Chart,我知道有人问过类似的问题,但提供的答案中没有一个用R中的叠加条形图解决了我的问题 我想生成一个带有两系列y值的条形图,它们具有相同的x值 第二系列y值表示第一系列y值的子集。因此,我想将第一个系列1绘制为条形图,然后叠加系列2 我想展示的是y2系列的叠加,只有y1系列的一半大小(设置宽度=0.5)。 不幸的是,这会将第二个系列的所有条向左移动 然后,我可以围绕一个为空间创建向量(spaces=c(0.9,1.4,1.4,1.4))进行操作,但我无法将其自动化 有人能提出一个聪明的解决办法吗 事先

我知道有人问过类似的问题,但提供的答案中没有一个用R中的叠加条形图解决了我的问题

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

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

我想展示的是y2系列的叠加,只有y1系列的一半大小(设置宽度=0.5)。 不幸的是,这会将第二个系列的所有条向左移动

然后,我可以围绕一个为空间创建向量(
spaces=c(0.9,1.4,1.4,1.4)
)进行操作,但我无法将其自动化

有人能提出一个聪明的解决办法吗

事先非常感谢, 沃纳


[

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

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)

基于问题澄清的附录: 无论你制作了多少条,如果你坚持宽度为0.5的 第二个条,您可以使用
spating=space=c(0.9,rep(1.4,长度(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。 您希望第二组的每个条都位于第一组条的中心。 如果对第一个条使用
间距=0.9
,则其左边缘将位于
间距*条\宽度=0.9*0.5=0.45
。移动到条的中心将增加 0.5*0.5=0.25,因此第一根钢筋的中心将位于对齐的0.7处 第一组的第一个横杆的中心。因为我们使用 空间=1.4对于第二组中的其余钢筋,每个钢筋将移动 超过
间距*bar\u width+bar\u 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)
NumTypes=11

数字_aa Hi@G5W,非常感谢您的回复。正如我在上面写的那样,我知道
(空格=…)
选项。但是,对于宽度的每一次更改,我都必须执行一个试错序列,以便将第二个绘图与第一个绘图对齐。我的问题:有没有方法计算它(自动调整它)还是使用
width=
spaces=
测试行为的唯一方法?非常感谢@G5W的第二个详细回答!即使我已经阅读了barplot的文档(可能不是“the”,而是“a”文档),我一定是看了你贴的句子。现在,这很清楚了!!!你的信息正是我需要的。
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)
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)