如何在ggplot2中为R更改bin的位置

Ben*_*Ben 3 r histogram ggplot2

下面绘制了一个直方图,其中最左边的点为0.

myplot = ggplot(df,aes(x = myvar)) +
         geom_histogram(aes(y = ..density..), binwidth = .3)  
Run Code Online (Sandbox Code Playgroud)

我希望直方图有一个以0为中心的bin.(如果你想知道我为什么要做这么古怪的事情 - 这是为了说明直方图的一些弱点.)

mne*_*nel 7

你可以传递一个breaks参数stat_bin...,(geom_histogram电话stat_bin)

myplot <-  ggplot(df,aes(x = myvar))+
 geom_histogram(aes(y = ..density..), breaks = seq(0,5,by=1))
Run Code Online (Sandbox Code Playgroud)

这覆盖bindwidthorigin

有关详细信息,请参阅stat_bin的帮助.

您也可以找到origin一个有用的参数,(origin = 0也许是设置),但不能与之结合使用breaks!