使用 ggplots 绘制直方图类似于 R base 中的 hist()

Plot histogram using ggplots to be similar as hist() in R base

我是 ggplot2 的新手。我正在用它来绘制直方图。 这是代码

   d=c(14.7926078,29.7932923,0.7912389,23.0034223,6.6913073,6.6940452,12.6954141,3.6933607,33.6947296,15.6960986,13.6974675
,8.6981520,27.6988364,14.7104723,38.7104723,15.7097878,19.7097878,1.8480493,49.7138946,12.7200548,23.7125257,6.7132101
,13.7084189,10.9431896,10.7186858,18.7186858,24.7200548,4.7200548,6.7296372,21.7303217,29.7357974,19.7316906,34.7323751
,17.7330595,10.7323751,13.7330595,5.4811773,9.7357974,0.7529090,15.8083504,5.7713895,12.7720739,10.7789185,15.7700205,25.7713895,12.7748118,8.7748118,51.7782341,64.7912389,8.7885010,7.7864476,14.7871321,10.7898700,4.9089665,23.7946612
,1.7905544,18.7926078,21.7905544,12.7912389,7.7973990,2.7926078,8.8186174,17.8069815,2.8062971,18.8062971,11.8056126
,5.8097194,3.8247775,39.9041752,7.8083504,13.9630390,17.8097194,12.9527721,9.8124572,19.8247776,19.8850103,2.8336756
,6.8172485,11.8275154,33.8288843,24.8295688,25.8288843,26.8281999,19.8466804,43.8275154,8.8295688,8.8870637,10.8309377
,9.8343600,21.8343600,22.8309377,3.8439425,15.8302533,26.8309377,9.8343600,36.8350445,0.8377823,33.8343600,59.8439425
,4.8350445,15.8439425,11.8439425,16.8487337,3.8466804,7.8466804,4.8487337,1.8480493,24.8487337,11.8850103,21.0157426
,0.8514716,2.8501027,11.8521561,51.8548939,4.8542094,1.8535250,12.8542094,9.8535250,0.8706366,5.8726899,2.8665298
,42.8692676,2.8720055,17.8863792,8.8870637,5.8726899,49.8726899,12.8761123,19.8713210,8.8843258,13.8836413,16.8870637
,4.8870637,60.8870637,28.8870637,10.8856947,11.8850103,16.7008898,20.8898015,3.9014374,26.9021218,19.9014374,26.9021218
,26.9048597,37.9110198,18.9048597,17.9055441,3.0609172,47.9069131,9.9082820,24.9089665,7.9288159,1.9301848,45.9411362
,29.9411362,8.9418207,28.9418207,1.9438741,24.9445585,39.9425051,17.9438741,12.9445585,8.9445585,14.9431896,36.9472964
,3.9452430,17.9466119,7.9452430,29.9548255,2.9322382,21.9603012,23.9589322,52.0574949,8.9637235,13.9630390,1.9630390
,3.9644079,7.9644079,26.9650924,32.9664613,14.9678303,4.9691992,20.0027379,13.0047912,22.0041068,6.0041068,9.0047912
,23.8247776,34.0177960,5.0075291,53.0020534,18.0068446,19.0034223,41.0047912,8.0054757,24.0191650,3.0198494,15.0308008
,5.0212183,3.0198494,4.0383299,14.0232717,9.0403833,12.0219028,34.0260096,16.0246407,18.0260096,8.0273785,4.0246407
,23.0362765,19.0390144,15.0390144,32.0383299,17.0403833,2.0424367,23.0554415,5.0568104,10.0588638,16.0547570,5.0568104
,4.0547570,27.0581793,3.0581793,28.0574949,14.0588638,10.0588638
)
qplot(factor(as.integer(d)), geom="histogram",  alpha=I(.5),
  binwidth=1)+theme(text = element_text(size=15))+   scale_colour_hue("clarity")+scale_x_discrete(breaks=seq(0, 70, by=10))

图表是 而当我使用 R base

中的 hist() 时
hist(d)

图表是

所以基本上 R base 将数字分成 10,而 ggplots 将每个单个数字显示为一组。我希望我的 ggplot 显示与基本 R 直方图相同的东西。我应该添加什么命令来控制它?我试图设置 binwidth 但没有任何改变。谢谢!

您没有获得值范围,因为您将 d 转换为一个因子。将其保留为数字,您将获得跨越范围的条形。此外,我已将您的数据转换为数据框,因为 ggplot 需要数据框。

dat = data.frame(d=d)

ggplot(dat, aes(x=d)) +
  geom_histogram(breaks=seq(0,max(dat$d)+10,10),
                 fill="lightblue", colour="black")