9

The base graphics can nicely plot a boxplot using a simple command

data(mtcars)
boxplot(mtcars$mpg)

enter image description here

But qplot requires y axis. How can I achieve with qplot the same like base graphics boxplot and not get this error?

qplot(mtcars$mpg,geom='boxplot')
Error: stat_boxplot requires the following missing aesthetics: y
Didzis Elferts
  • 90,455
  • 13
  • 256
  • 198
userJT
  • 10,476
  • 19
  • 70
  • 85

3 Answers3

19

You have to provide some dummy value to x. theme() elements are used to remove x axis title and ticks.

ggplot(mtcars,aes(x=factor(0),mpg))+geom_boxplot()+
   theme(axis.title.x=element_blank(),
    axis.text.x=element_blank(),
    axis.ticks.x=element_blank())

Or using qplot() function:

qplot(factor(0),mpg,data=mtcars,geom='boxplot')

enter image description here

PatrickT
  • 9,078
  • 9
  • 67
  • 99
Didzis Elferts
  • 90,455
  • 13
  • 256
  • 198
2

You can also use latticeExtra, to mix boxplot syntax and ggplot2-like theme:

bwplot(~mpg,data =mtcars,
        par.settings = ggplot2like(),axis=axis.grid)

enter image description here

agstudy
  • 116,828
  • 17
  • 186
  • 250
1

you can set the x aesthetics to factor(0) and tweak the appearance by removing unwanted labels:

ggplot(mtcars, aes(x = factor(0), mpg)) +
    geom_boxplot() + 
    scale_x_discrete(breaks = NULL) +
    xlab(NULL)

enter image description here

PatrickT
  • 9,078
  • 9
  • 67
  • 99
Medhat
  • 1,494
  • 15
  • 30
  • While this might answer the question, please explain your answer and perhaps show an example image – loki Aug 10 '17 at 06:54