问题描述:

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

data(mtcars)

boxplot(mtcars$mpg)

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

网友答案:

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')
网友答案:

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

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

网友答案:

you can just use ggplot(mtcars,aes(x=factor(0),mpg))+geom_boxplot()+scale_x_discrete(breaks=NULL)

相关阅读:
Top