直方图与使用连续数据的空间(宽度= ...不工作)(Histogram with spaces us

2019-10-21 15:41发布

我试图绘制使用ggplot其中有条之间的一些空间上的直方图。

这是离散的数据没有问题:

b= data.frame(x=sample(LETTERS[1:3],size=50, replace=T))
ggplot(b, aes(x=x)) + geom_bar(width=.3)

然而,使用连续的数据, width似乎没有任何效果。

a= data.frame(x=rnorm(100))
ggplot(a, aes(x=x, width=.5)) +
geom_bar(width=.3, binwidth=1)

如何与间隔条的直方图进行存档连续数据?

Answer 1:

我觉得这样做,这是一个非常糟糕的主意(和GGPLOT2不支持它)。

这是一种可能性:

breaks <- pretty(range(a$x), n = 6, min.n = 1)
mids <- 0.5 * (breaks[-1L] + breaks[-length(breaks)])

ggplot(a, aes(x = cut(x, breaks = breaks, labels = mids))) + 
    geom_bar(width=.3)


文章来源: Histogram with spaces using continuous data (width=… doesn't work)
标签: r plot ggplot2