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

10
基础图形可以使用简单的命令绘制出一个漂亮的箱线图。
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个回答

22
你需要为 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


我明白了。那么qplot将是qplot(factor(0),mtcars$mpg,geom='boxplot')。 - userJT

3

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

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

enter image description here


虽然这可能回答了问题,但请解释一下你的答案,并可能展示一个示例图像。 - loki

2

您也可以使用latticeExtra,将boxplot语法和ggplot2类似的主题混合使用:

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

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接