如何在R中使标签与bin边缘对齐的可变宽度直方图? [英] How to make variable width histogram in R with labels aligned to bin edges?

查看:61
本文介绍了如何在R中使标签与bin边缘对齐的可变宽度直方图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 ggplot2 ,默认情况下,它会创建具有固定bin宽度的直方图,并且其bin标签会绘制在每个bin的中心.

I'm using ggplot2, which by default creates histograms with fixed bin widths and whose bin labels are plotted in the center of each bin.

我想要的是一个可变宽度的直方图,其bin标签代表每个bin的端点,如下图所示:

What I want instead is a variable-width histogram whose bin labels are representative of the end points of each bin, like this plot:

要生成此示例图,我手动输入了bin参数并移动了bin以使其与端点对齐:

To produce this example plot, I manually entered the bin parameters and shifted the bins to align them with their end points:

income=data.frame(lx=c(0,10,25,50,100),rx=c(10,25,50,100,150),y=c(20,28,27,18,7))
income$width = income$rx-income$lx


ggplot(income, aes(lx+width/2,y/width)) + geom_bar(aes(width=rx-lx), color='black', stat='identity') + 
  scale_x_continuous(breaks=unique(c(income$lx,income$rx))) + labs(x='Income (thousands of $)', y='% per thousand $')

但是我想根据原始数据自动执行此操作.(原始数据可以使用以下代码来近似):

But I want to do this automatically, from the original data. (The original data can be approximated using the following code):

incomes=unlist(sapply(1:nrow(income), function(i) sample(income$lx[i]:(income$rx[i]-1),income$y[i],replace=TRUE)))
widths=unlist(sapply(1:nrow(income), function(i) rep(income$rx[i]-income$lx[i],income$y[i])))
incomes=data.frame(incomes, widths)

推荐答案

您可以通过在 geom_histogram 中指定所需的 breaks 来生成可变宽度的直方图.使用 y = .. density .. (而不是基于计数的默认值),这样条形将被标准化为它们在整个条形区域中所占的比例.

You can produce a variable width histogram by specifying the desired breaks in geom_histogram. Use y=..density.. (rather than the default, which is based on counts), so that the bars will be normalized to their proportion of the total bar area.

breaks = c(0,10,25,50,100,150)

ggplot(incomes, aes(incomes)) +
  geom_histogram(aes(y=..density..),
                 color="black", fill="grey40", breaks=breaks) +
  scale_x_continuous(breaks=breaks)

这篇关于如何在R中使标签与bin边缘对齐的可变宽度直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆