如何在x轴上对直方图的条形对齐或居中?

时间:2017-05-28 16:35:48

标签: r ggplot2

qplot(carat,data=diamonds,geom="histogram",binwidth=1,xlim=c(0,3))

我使用此代码绘制直方图。但结果与书的结果不同。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:2)

有两种可能性,使用center参数作为suggested by Richard Telfordboundary参数。

以下两个代码都会创建相同的图表:

library(ggplot2)  # CRAN version 2.2.1 used
qplot(carat, data=diamonds, geom = "histogram", binwidth = 1, xlim = c(0,3),
      center = 0.5)
qplot(carat, data=diamonds, geom = "histogram", binwidth = 1, xlim = c(0,3),
      boundary = 0)

enter image description here

有关详细信息,请参阅?geom_histogram

相关问题