根据 r 中直方图中的特定值不同的类别间隔(中断)

问题描述 投票:0回答:1

我想根据体重指数 (BMI) 分类在直方图中创建类别间隔,并对列进行着色。 类别是:

体重过轻(严重瘦)< 16.0 ->颜色:红色

体重过轻(中等厚度)16.0 – 16.9 -> 颜色:橙色

体重过轻(轻度瘦)17.0 – 18.4 -> 颜色:粉色

正常范围 18.5 – 24.9 -> 颜色:绿色

超重(肥胖前期)25.0 – 29.9 -> 颜色:蓝色

肥胖(I 级)30.0 – 34.9 -> 颜色:粉色

肥胖(II 级)35.0 – 39.9 -> 颜色:橙色

肥胖(III级)≥ 40.0 -> 颜色:红色

我尝试了下面的代码,但它返回了密度(y 轴),并且 x 轴未正确包含类范围。如何在 y 轴上绘制频率并在直方图中绘制类间隔限制?


Height = c(1.72,1.86,2.1,1.7,1.6,1.67,1.59,1.88,1.7,1.72,1.9,1.88,1.59,
           1.55,1.91,1.61,1.82,1.66,1.77,1.74)
Weight =c(77,79,102,70,63,62,55,89,88,88,128,100,55,60,79,59,57,70,72,74)
BMI <- Weight/Height^2

class_range = c(16,16.9,18.4,24.9,29.9,34.9,39.9)
hist(BMI, 
     main = "", breaks = class_range,
     col = c("red", "orange","pink","green","blue","pink","orange","red"))

r histogram intervals frequency frequency-distribution
1个回答
0
投票

通过将 BMI 值分组,您可以有效地创建分类变量。直方图不是可视化此类数据的最佳图。尝试使用条形图,如下所示使用 base R 和 ggplot2 包。

bmi.gp <- cut(BMI, breaks=c(0, class_range, Inf), labels=bmi.labels)

bmi.labels <- c("Underweight (severe)", "Underweight (moderate)", "Underweight (mild) ",
                "Normal",
                "Overweight",
                "Obese (class I)", "Obese (class II)", "Obese (class III)")

bmi.cols <- c("red", "orange","pink","green","blue","pink","orange","red")

A(基础R)

par(las=1, mar=c(4,10,1,1))
barplot(table(bmi.gp), col=bmi.cols, xlab="Frequency", horiz = TRUE, xlim=c(0,12))
mtext("BMI group", at=-3)
grid(ny = NA)

B(ggplot)

library(ggplot2)

data.frame(bmi.gp) |>
  mutate(bmi.gp=factor(bmi.gp, levels=bmi.labels)) |>
  count(bmi.gp, .drop=FALSE) |>
  ggplot(aes(x=bmi.gp, y=n, fill=bmi.gp)) +
  geom_col(show.legend=FALSE) +
  scale_fill_manual(values=bmi.cols) +
  theme_light() +
  labs(y="Frequency", x="BMI group") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))

© www.soinside.com 2019 - 2024. All rights reserved.