How do you draw a box without specifying an x โ€‹โ€‹axis?

Basic graphics can display a box nicely with a simple command

data(mtcars) boxplot(mtcars$mpg) 

enter image description here

But qplot requires the y axis. How can I achieve with qplot the same as the base graphic box and not get this error?

 qplot(mtcars$mpg,geom='boxplot') Error: stat_boxplot requires the following missing aesthetics: y 
+7
source share
3 answers

You must specify some dummy value of x . theme() elements are used to remove the title and ticks of the x axis.

 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 the qplot() function:

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

enter image description here

+13
source

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

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

enter image description here

+2
source

you can set the aesthetics of x to factor(0) and customize the look by removing unnecessary labels:

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

enter image description here

+1
source

All Articles