0

I would like to know, how I can combine my tables to create one bar plot. I first mutated and created new columns... but the answers are TRUE OR FALSE

master1 <- master %>%
  mutate(underweight = BMI < 18.5,
         normal = between(BMI, 18.5, 24.9),
         overweight = between(BMI, 25, 29.9),
         obese = between(BMI, 30, 34.9),
         extreme = BMI > 35)

as I didn't know how to create from this table a bar plot, I splitted them in new tables:

t.underweight<-master1 %>%
  filter(Income>0,underweight==TRUE) %>%
  select(Income,underweight)

t.normal<-master1%>%
  filter(Income>0,normal==TRUE)%>%
  select(Income,normal)

t.overweight<-master1%>%
  filter(Income>0,overweight==TRUE)%>%
  select(Income,overweight)

t.obese<-master1%>%
  filter(Income>0,obese==TRUE)%>%
  select(Income,obese)

t.extreme<-master1%>%
  filter(Income>0,extreme==TRUE)%>%
  select(Income,extreme)

maybe there is an easier way to direct after mutating, if yes I would appreciate to learn how. if not how can I just combine this table to create a barplot? my second variable would be " Income " ... on the y-axis

1 Answer 1

1

are you looking for this? enter image description here

library(tidyverse)
set.seed(1)
bmi <- runif(100, 10, 50)
income <- rnorm(100, 400, 10)
id <- 1:100

df <- data.frame(id = id, bmi = bmi, income = icnome)

res <- df %>% 
  mutate(BMI_cat = cut(
    x = bmi,
    breaks = c(0, 18.5, 25, 30, 35, Inf),
    labels = c("underweight", "normal", "overweight", "obese", "extreme"),
    ordered_result = T
  ))

ggplot(res, aes(income)) +
  geom_histogram(bins = 10) +
  facet_wrap(~BMI_cat, scales = "fixed")
Sign up to request clarification or add additional context in comments.

3 Comments

Hello Yuriy, thanks for your help .. I am kind of searching something like this...
Great! If you believe this answer was helpful for you, you could accept it by clicking the tick on the left side of this answer :)
Yes sure.. one more question, but how is it possible when the answers are like TRUE and FALSE? t.extreme<-master1%>% filter(Income>0,extreme==TRUE)%>% select(Income,extreme) > head(t.extreme) Income extreme 1 21600 TRUE 2 4000 TRUE 3 12720 TRUE 4 26772 TRUE

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.