0

How do I plot two variables box plots side by side using ggplot2 and/or boxplot()

generate data

#generate data
var1<- c(11.5,45,33,67,89,12,5)
var2<- c(23,45,66.7,33,42,88,1)
var3<- c(12,13,14,15.8,16,11,10)
case<- c(1,2,3.4,4,5,6.1,7)
dframe<- as_tibble(cbind(case,var1, var2, var3))

the problem I am facing is that when I use boxplot() and lable the x axis all the lables are stacked

boxplot(var1, var2, var3,
        xlab= c("var1", "var2"))

whem I use ggplot2: I get a warningand the graph shows just one box plot.

ggplot(dframe, aes(var1, var2, var3))+
  geom_boxplot()

Warning message: Continuous x aesthetic -- did you forget aes(group=...)?

www
  • 38,575
  • 12
  • 48
  • 84
CarLinneo
  • 47
  • 7

1 Answers1

0

You need to covert your data frame to long format like below.

library(tidyverse)

dframe2 <- dframe %>%
  gather(Var, Value, -case)


ggplot(dframe2, aes(x = Var, y = Value))+
  geom_boxplot()

enter image description here

www
  • 38,575
  • 12
  • 48
  • 84