对齐barplot与盒形图在研发barplot、盒形图

2023-09-11 07:53:14 作者:凉了时光疼了心

我想用绘制在R中barplot函数计算的分布,并用箱线图下垫它包括位数的信息,四分位数,和异常值。这样做的一个不太完美的解决方案已经发现了直方图和箱线图:的 http://rgraphgallery.blogspot.com/2013/04/rg-plotting-boxplot-and-histogram.html 。

I would like to plot a distribution of counts using the barplot function in R, and underlay it with a boxplot to include information on median, quartiles, and outliers. A not-too-elegant solution for this has been found for histogram and boxplots: http://rgraphgallery.blogspot.com/2013/04/rg-plotting-boxplot-and-histogram.html.

有许多地方网上,人们可以找到正在取得的论点,数字数据应绘出直方图,而分类数据应与巴图绘制。我的数据是数字,而事实上在比规模(因为他们是计数),而是因为他们的离散的,我想有间隙列,那一抹不列,这似乎是唯一的选择对于直方图()。

There are many places online where one can find the argument being made that numerical data should be plotted with histograms while categorical data should be plotted with bar plots. My data are numerical, and in fact on a ratio scale (as they are counts), but because they are discrete, I want columns with gaps, not columns that touch, which seems to be the only option for histogram().

目前,我有以下,但酒吧,和箱线不对齐十分完美的:

I currently have the following, but bar- and boxplot do not align quite perfectly:

set.seed(476372)
counts1 <- rpois(10000,3)
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(prop.table(table(counts1)))
boxplot(counts1, horizontal=TRUE,  outline=TRUE,ylim=c(0,12), frame=F, width = 10)

下面我的问题:我怎样才能让他们调整

Here my question: How can I make them align?

推荐答案

另一个选项是类似的,但更多一点的工作。这preserves的选项酒吧之间的差距:

Another option that's similar but a little more work. This preserves the option for gaps between the bars:

tbl <- prop.table(table(counts1))
left <- -0.4 + do.call('seq', as.list(range(counts1)))
right <- left + (2 * 0.4)
bottom <- rep(0, length(left))
top <- tbl
xlim <- c(-0.5, 0.5) + range(counts1)

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))
plot(NA, xlim=xlim, ylim=c(0, max(tbl)))
rect(left, bottom, right, top, col='gray')
boxplot(counts1, horizontal=TRUE,  outline=TRUE, ylim=xlim, frame=F, width = 10)

相关推荐