2013-02-22 10 views
7

La grafica base possono ben tracciare un grafico a scatole utilizzando un semplice comandoCome si disegna un boxplot senza specificare l'asse x?

data(mtcars) 
boxplot(mtcars$mpg) 

enter image description here

Ma qplot richiede asse y. Come posso ottenere con qplot lo stesso come boxplot della grafica di base e non ottenere questo errore?

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

risposta

13

È necessario fornire un valore fittizio a x. Gli elementi theme() vengono utilizzati per rimuovere il titolo e le zecche dell'asse 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()) 

o utilizzando la funzione qplot():

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

enter image description here

+0

vedo. Quindi il qplot sarebbe qplot (fattore (0), mtcars $ mpg, geom = 'boxplot') – userJT

2

È anche possibile utilizzare latticeExtra, per mescolare boxplot sintassi e ggplot2-like tema:

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

enter image description here

1

è possibile impostare l'estetica x per factor(0) e modificare l'aspetto rimuovendo etichette indesiderate:

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

enter image description here

+0

Mentre questo potrebbe rispondere alla domanda, per favore spiega la tua risposta e magari mostra un'immagine di esempio – loki

Problemi correlati