0

I have a dataset with 3 columns. One is monthly expenditure (y-variable). Each value in this variable is categorised as either 1 or 0 under two different variables.

Data looks something like this:

  df_UP.q234_month_exp df_UP.LFT df_UP.LF
1                   NA         0        1
2                   NA         1        1
3                12000         1        1
4                   NA         1        1
5                20000         1        1
6                   NA         0        1

Data has about 1200 rows.

I want a plot which creates a box plot for 'df_UP.q234_month_exp' as y-variable for all rows of 'df_UP.LFT' which are 1, and another box plot in the same plot with same y-variable, but for all rows of 'df_UP.LF' which are 1.

How to accomplish this using ggplot2?

2
  • Could you show us what you have tried so far? Commented Aug 12, 2020 at 14:26
  • Something like that should work: ggplot(dat, aes(factor(f), var)) + geom_boxplot() + facet_wrap(.~g) where f and g are your binary variables and var your numeric variable. Commented Aug 12, 2020 at 14:29

1 Answer 1

2

You can try this:

library(tidyverse)
#Data
df <- structure(list(df_UP.q234_month_exp = c(NA, NA, 12000L, NA, 20000L, 
NA), df_UP.LFT = c(0L, 1L, 1L, 1L, 1L, 0L), df_UP.LF = c(1L, 
1L, 1L, 1L, 1L, 1L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

The code:

df %>% pivot_longer(cols = -df_UP.q234_month_exp) %>%
  filter(value==1) %>% ggplot()+
  geom_boxplot(aes(x = name,y = df_UP.q234_month_exp,color=name,group=name))

Output:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

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.