2011-12-14 13 views
5

In un ggplot boxplot, è facile usare il jitter per aggiungere i punti di dati grezzi con vari gradi di jitter. Con zero jitter il seguente codicepuò un offset punti di jitter in ggplot boxplot

dat <- data.frame(group=c('a', 'b', 'c'), values = runif(90)) 

ggplot(dat, aes(group, values)) + 
geom_boxplot(outlier.size = 0) + 
geom_jitter(position=position_jitter(width=0), aes(colour=group), alpha=0.7) + 
ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') + 
opts(legend.position = "right") + ylab("values") + xlab("group") 

produce la trama di seguito.

E 'possibile utilizzare lo zero jitter ma aggiungere un offset in modo tale che i punti sono in linea, ma spostata a sinistra del 25% della larghezza scatola? Ho provato geom_point con dodge ma questo generato un jitter. enter image description here

risposta

7

Se convertiamo il gruppo in numerico e quindi aggiungiamo un offset, sembra che tu abbia ottenuto l'output desiderato. C'è probabilmente un modo più efficace/efficiente, ma dare a questo un vortice:

ggplot(dat, aes(group, values)) + 
    geom_boxplot(outlier.size = 0) + 
    geom_point(aes(x = as.numeric(group) + .25, colour=group), alpha=0.7) + 
    ylim(0, 1) + stat_summary(fun.y=mean, shape=3, col='red', geom='point') + 
    opts(legend.position = "right") + ylab("values") + xlab("group") 

enter image description here

+0

li ho spostato a destra, ma ovviamente è possibile spostare le lasciarono così sottraendo 0,25 o qualsiasi numero che si dà l'aspetto che stai cercando. – Chase

+0

Molto bene. Grazie! – user441706

Problemi correlati