Mir*_*atz 4 plot r alignment bar-chart boxplot
我想使用R中的barplot函数绘制计数分布,并使用boxplot将其包含在内,以包含有关中位数,四分位数和异常值的信息.对于直方图和箱形图,我们发现了一个不太优雅的解决方案:http: //rgraphgallery.blogspot.com/2013/04/rg-plotting-boxplot-and-histogram.html .
网上有很多地方可以找到这样的论点:数字数据应该用直方图绘制,而分类数据应该用条形图绘制.我的数据是数字的,实际上是比例尺度(因为它们是计数),但因为它们是离散的,我想要有间隙的列,而不是要触摸的列,这似乎是histogram()的唯一选项.
我目前有以下内容,但是条形图和箱形图并不完美对齐:
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)
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何使它们对齐?
另一个类似但更多工作的选择.这样可以保留条形间隙的选项:
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)
Run Code Online (Sandbox Code Playgroud)
