如何在不指定x轴的情况下绘制箱线图?

时间:2013-02-22 15:34:30

标签: r ggplot2

基本图形可以使用简单的命令

很好地绘制箱线图
data(mtcars)
boxplot(mtcars$mpg)

enter image description here

但是qplot需要y轴。如何使用qplot与基本图形boxplot相同而不会出现此错误?

qplot(mtcars$mpg,geom='boxplot')
Error: stat_boxplot requires the following missing aesthetics: y

3 个答案:

答案 0 :(得分:17)

您必须为x提供一些虚拟值。 theme()元素用于删除x轴标题和刻度。

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())

或使用qplot()功能:

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

enter image description here

答案 1 :(得分:2)

您还可以使用latticeExtra混合boxplot语法和ggplot2-like主题:

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

enter image description here

答案 2 :(得分:1)

您可以将x美学设置为factor(0)并通过删除不需要的标签来调整外观:

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

enter image description here