-1

I have a data frame with the two columns bloodlevel and sex (F & M only), with 14 male and 11 female.

   bloodlevel sex
1        14.9   M
2        12.9   M
3        14.7   M
4        14.7   M
5        14.8   M
6        14.7   M
7        13.9   M
8        14.1   M
9        16.1   M
10       16.1   M
11       15.3   M
12       12.8   M
13       14.0   M
14       14.9   M
15       11.2   F
16       14.5   F
17       12.1   F
18       14.8   F
19       15.2   F
20       11.2   F
21       15.0   F
22       13.2   F
23       14.4   F
24       14.7   F
25       13.2   F

I am trying to create two histograms that differentiate females' and males' blood levels with facet_wrap.

I have tried

ggplot(Physiology, aes(x=sex, y=bloodlevel))+
  geom_histogram(binwidth=5, fill="white", color="black")+
  facet_wrap(~Physiology)+
  xlab("sex")

but I’m getting the error

Error in `combine_vars()`:
! At least one layer must contain all faceting variables: `Physiology`.
* Plot is missing `Physiology`
* Layer 1 is missing `Physiology`

I am trying trying to facet the variable with plot like this:

zephryl
  • 14,633
  • 3
  • 11
  • 30
  • 2
    Take `sex` out of `aes()` and add to `facet_wrap()`: `ggplot(Physiology, aes(bloodlevel)) + geom_histogram(binwidth=5, fill="white", color="black") + facet_wrap(~sex)` – zephryl Feb 12 '23 at 12:26

1 Answers1

0

Is this what you're trying?

df <- data.frame(bloodlevel = sample(12:16,25,T),
                 sex=sample(c("M","F"),25,T))
df %>% ggplot(aes(x=bloodlevel))+geom_histogram()+
  facet_wrap(~sex)

enter image description here Next time please provide a working code sample for us to use (Copying the table you printed doesnt do the trick..)

RYann
  • 567
  • 1
  • 7