ggplot2 - facet_grid或facet_wrap不能正确分割绘图

时间:2016-03-31 08:23:15

标签: r ggplot2

我有以下数据:

HDL=c(26.41779, 45.99568, 70.74717, NA, 22.35012, 60.46269, 48.03072, 23.87873 ,54.92130 ,64.95151, 57.94742, 44.75888 ,32.57670,
  39.26278, 53.05259, 38.76336, 74.73936, 63.62279, 99.27376, 35.42466, 35.48192, 43.56407, 45.23391, 58.27397, 15.15950, 46.20695,
  35.95102, 55.05239, 39.73222, 53.68377, 35.21194, 95.44346, 38.53242, 62.78161, 56.30534, 67.35458, 22.75741, 51.94800, 66.61517,
  35.35236)

LDL=c(183.83648, 169.59815 ,106.58631 ,137.96398, 164.25937,  94.39745, 189.70669, 167.62298 ,176.85359, 127.54434, 115.63603, 140.43276,
  165.68687, 150.71473, 131.29033, 150.66534, 137.26902, 156.01673, 118.18147, NA, 161.35154, 157.89021, 138.93356, 139.51652,
  206.24948, 168.27322, 176.91744,  92.03747, 144.61200, 127.93379, 142.59781,  88.22650, 157.32140, 149.79619, 121.23857, 141.68063,
  173.50586, 133.91838, 123.99608, 138.68897)

BMI=c(35, 33 ,25, 27 ,31, 21, 32, 34, 33, 29, 23, 27, 33, 26, 26, 32, 25, 30, 22, 36, 33, 30, 27, 29, 36, 35, 35, 20, 29, 27, 29, 20, 32, 30, 22, 29, 33, 27, 22, 28)

newdf=data.frame(HDL, LDL, BMI)

newdf$BMI_group[newdf$BMI<25]="lean"
newdf$BMI_group[newdf$BMI>=25 & newdf$BMI<30]="overweight"
newdf$BMI_group[newdf$BMI>=30]="obese"

数据完全是虚构的。当我使用以下代码时,facet_grid或facet_wrap不会正确分割关于分组变量的图。

ggplot(data = plotdf, aes(x = plotdf$HDL, y = plotdf$LDL)) +
    geom_point() +
    facet_wrap(~BMI_group)

详细说明,它是分开的,例如在df内没有LDL超过150的瘦的患者。但情节显示了我的确如此。也许我在推理上有错误。

enter image description here
我对此行为没有解决方案或解释。希望你能帮帮我。

修改:我添加了@Roland

建议的数据

0 个答案:

没有答案