且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据变量的值对变量进行分组并获得直方图

更新时间:2023-11-26 10:14:34

在基数R中

r <-c(1,899,1,2525,763,3,2,2,1863,695,9,4,2876,1173,1156,5098,3,3876,1,1,5,
      3023,76336,13,003,9898,1,10,843,10546,617,1375,1,1,5679,1,21,1,13,6,28,1,14088,682)
cut.vals <- cut(r, breaks = c(1, 5, 10, 100, 500, Inf), right = FALSE)
xy <- data.frame(r, cut = cut.vals)
barplot(table(xy$cut))

请注意,我添加了xy变量以简化比较值的分组方式.您可以直接将cut.vals放入barplot(table()).

Note that I added the xy variable to ease in comparing how values were grouped. You can directly put cut.vals into the barplot(table()).

要使用ggplot2,您可以预先计算所有垃圾箱并绘制

To use ggplot2, you can pre-calculate all the bins and plot

ggplot(xy, aes(x = cut)) +
  theme_bw() +
  geom_bar() +
  scale_x_discrete(drop = FALSE)

geom_histogram控制箱大小的最常用参数是binwidth,对于所有箱都是恒定的.

geom_histogram's most common parameter that controls bin size is binwidth, which is constant for all bins.