2013-02-22 40 views
7

基部图形可以用一个简单的命令如何在不指定x轴的情况下绘制箱形图?

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 

回答

13

您必须提供一些虚拟值给xtheme()元素用于删除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

+0

我明白了。所以qplot应该是qplot(factor(0),mtcars $ mpg,geom ='boxplot') – userJT 2013-02-22 16:37:17

2

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

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

enter image description here

1

你可以设置x美学factor(0)并通过除去不需要调整标签的外观:

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

enter image description here

+0

虽然这可能会回答这个问题,但请解释您的答案并可能会显示一个示例图像 – loki 2017-08-10 06:54:47

相关问题