基于R中范围的分组直方图

Grouped histogram based on range in R

我有一个这种格式的数据集,我想制作一个分组直方图和范围,即 10-20, 20-30, ..., 90-100

   group weight
1  A     54
2  A     55
3  B     52
4  B     53
5  C     60

我尝试了以下生成直方图的方法,但不确定如何在 X 轴上设置重量范围。

ggplot(df, aes(x = weight)) +
  geom_histogram(aes(color = gp), fill = "white",
                 position = "identity", bins = 30) +
  scale_color_manual(values = c("red", "blue", 'green'))

我实际上需要像下面这样的直方图

这是条形图,不是直方图。这是重现它的方法:

library(dplyr)

df %>%
  mutate(weight_class = cut(weight,
                            breaks = seq(20, 60, by = 10))) %>%
  ggplot() +
    geom_bar(aes(x = weight_class, fill = group), position = "dodge") +
    scale_fill_manual(values = c("red", "blue", "green"))