I have data that can be roughly replicated using
n = 10
df = data.frame(
val= rnorm(n),
var1 = rbinom(n, 1, 0.5),
var2 = rbinom(n, 1, 0.5),
var3 = rbinom(n, 1, 0.5))
How can I plot this on a boxplot? What I'm looking for will have var1, var2 and var3 on the x-axis and val on the y-axis i.e. the box-and-whisker for var1 will contain only observations that have a 1 in the var1 column.
I've tried
df2 = melt(df, id.vars = c('val'),
variable.name ='vars', value.name = "include")
ggplot(df2, aes(x = include, y = val)) + geom_boxplot(aes(fill = vars)
but when I plot this I get 3 boxplots that look exactly the same.
Where am I going wrong?


