2013-02-22 108 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