-1

I am tring to plot(boxplots) multiple continuous variables (about 20 variables) with one binary outcome variable (either 0 or 1).

data:

ID  outcome  var1   var2    var3    var4  var5
1      0      62    2.01    13      1.94    8
2      0      150   4.32    9         99    6
3      0      18    1.86    0.6       99    22
4      0      60    4.08    3        -99    6
5      1      20    1.96    1         99    14
6      1      100   1.64    19       -99    3

my code:

tmp <- melt(data, id.vars=c("ID", "outcome"))

p <- ggplot(data = tmp, aes(x=outcome, y= value)) + 
   geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")

this code shows the following error:

Error in layout_base(data, vars, drop = drop) : At least one layer must contain all variables used for facetting

Any help would be greatly appreciated.

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
sri
  • 1
  • 2
  • 1
    It will help if you had an example of your dataset in your question. See [this link](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for ideas on how to add an example dataset. – aosmith Aug 30 '16 at 17:44

1 Answers1

0

There are a couple of problems here.

1) You don't have a variable called Label.

2) outcome is a continuous variable.

Removing Label and making outcome into a factor, the code works

ggplot(data = tmp, aes(x=as.factor(outcome), y= value)) + 
  geom_boxplot() +
  facet_wrap( ~ variable, scales="free")

data:

tmp <- structure(list(ID = c(1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 
5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 
3L, 4L, 5L, 6L), outcome = c(0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 
0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 
0L, 0L, 0L, 0L, 1L, 1L), variable = structure(c(1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L), .Label = c("var1", "var2", 
"var3", "var4", "var5"), class = "factor"), value = c(62, 150, 
18, 60, 20, 100, 2.01, 4.32, 1.86, 4.08, 1.96, 1.64, 13, 9, 0.6, 
3, 1, 19, 1.94, 99, 99, -99, 99, -99, 8, 6, 22, 6, 14, 3)), row.names = c(NA, 
-30L), .Names = c("ID", "outcome", "variable", "value"), class = "data.frame")
Richard Telford
  • 9,558
  • 6
  • 38
  • 51