我如何绘制附加到箱形图的条形图

how can I plot the bar plot attached to box plot

我的一小部分数据如下所示

dt<- structure(c(79L, 54L, 37L, 41L, 42L, 121L, 134L, 169L, 23L, 19L, 
22L, 19L, 25L), .Names = c("Experi_1", "Experi_2", "Experi_3", 
"Experi_4", "Experi_5", "Experi_6", "Experi_7", "Experi_8", "Experi_9", 
"Experi_10", "Experi_11", "Experi_12", "Experi_13"))

我尝试做的是将条形图分配给下面的箱线图 我已经阅读了这条评论并尝试使用它但没有成功

Align barplot with boxplot in R

条形图可以简单地画成这样。但是,我无法控制我的 x 轴。例如,如果我想以 5 的距离绘制它,我不能。让我们说 1、5、10 和 13 作为 x 轴标签。无论如何,这不是什么大问题。问题是为这个条形图分配一个箱线图!

barplot(dt, xlab="Number of S in each experiment")

我用下面的方法添加了箱线图,但它似乎没有这样做

xlim <- c(-0.5, 0.5) + range(dt)
par(mar=c(3.1, 3.1, 1.1, 2.1))
boxplot(dt, horizontal=TRUE,  outline=TRUE, ylim=xlim, frame=F, width = 10)

我认为问题在于您没有拆分 window。使用 layout() 来执行此操作(根据 post 的建议)。

layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(3,1))
par(mar=c(3.1, 3.1, 1.1, 2.1))
barplot(dt, main="Distribution", xlab="Number of each experiment")
boxplot(dt, horizontal=TRUE,  outline=TRUE, frame=F, width = 10)

您忘记使用 'layout' 行。 以下应该有效。

    nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(3,1))
    par(mar=c(3.1, 3.1, 1.1, 2.1))
    barplot(dt, main="Distribution", xlab="Number of each experiment")
    boxplot(dt, horizontal=TRUE,  outline=TRUE, ylim=xlim, frame=F, width = 10)