0

I have the following data:

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"

The data is completely fictitious. When I use the following code, facet_grid or facet_wrap do not split the plot correctly regarding the grouping variable.

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

In detail, it is splitted but e.g. there are no lean patients which have LDL of over 150 within the df. But the plot shows me exactly this. Maybe I have a error in reasoning.

enter image description here
I have no solution or explanation for this behavior. Hope you can help me.

Edit: I added the data like suggested by @Roland

Tobias
  • 564
  • 3
  • 13
  • 2
    Please share your data inside the question. See [this FAQ](http://stackoverflow.com/a/5963610/1412059) for advice how to do that. Anyway, try `ggplot(data = plotdf, aes(x = HDL, y = LDL)) + geom_point() + facet_wrap(~BMI_group)`. You only pass the column symbols to `aes` and it extracts the columns from the data.frame passed to `data`. Otherwise you get results like this, because the splitting variable might not be in the same order as `x` and `y` values. – Roland Mar 31 '16 at 08:25
  • 1
    The example link you provided is private – Dieter Menne Mar 31 '16 at 08:37
  • I changed the post like suggested by @Roland. I added the data directly in my post. – Tobias Mar 31 '16 at 13:14
  • 1
    Did you try Roland's suggestion at all? It solves your problem.. – erc Mar 31 '16 at 13:17
  • Yes, I tried it and it worked perfectly. Thanks. I gave his comment a upvote. However, I wanted to edit the post, so that it follows the FAQ. – Tobias Mar 31 '16 at 13:20

0 Answers0