2015-12-14 10 views
7

ggplot2 in grado di creare una molto attraente riempito trama violino:trama del violino ggplot2: riempire solo il 95% centrale?

ggplot() + geom_violin(data=data.frame(x=1, y=rnorm(10^5)), 
    aes(x=x, y=y), fill='gray90', color='black') + 
    theme_classic() 

vorrei limitare il riempimento al 95% centrale della distribuzione, se possibile, lasciando intatto il contorno. Qualcuno ha suggerimenti su come realizzare questo?

risposta

6

Fa questo ciò che vuoi? Richiede qualche elaborazione dati e il disegno di due violini.

set.seed(1) 
dat <- data.frame(x=1, y=rnorm(10^5)) 

#calculate for each point if it's central or not 
dat_q <- quantile(dat$y, probs=c(0.025,0.975)) 
dat$central <- dat$y>dat_q[1] & dat$y < dat_q[2] 

#plot; one'95' violin and one 'all'-violin with transparent fill. 
p1 <- ggplot(data=dat, aes(x=x,y=y)) + 
    geom_violin(data=dat[dat$central,], color="transparent",fill="gray90")+ 
    geom_violin(color="black",fill="transparent")+ 

    theme_classic() 

enter image description here

Edit: i bordi arrotondati mi dava fastidio, ecco un secondo approccio. Se lo facessi, vorrei linee rette. Così ho fatto qualche gioco con la densità (che è quello che trame violino si basano su)

d_y <- density(dat$y) 

right_side <- data.frame(x=d_y$y, y=d_y$x) #note flip of x and y, prevents coord_flip later 
right_side$central <- right_side$y > dat_q[1]&right_side$y < dat_q[2] 

#add the 'left side', this entails reversing the order of the data for 
#path and polygon 
#and making x negative 
left_side <- right_side[nrow(right_side):1,] 
left_side$x <- 0 - left_side$x 

density_dat <- rbind(right_side,left_side) 


p2 <- ggplot(density_dat, aes(x=x,y=y)) + 
    geom_polygon(data=density_dat[density_dat$central,],fill="red")+ 
    geom_path() 


p2 

enter image description here

+0

Ah, solo battere me! – Axeman

+1

@Axeman La grande mente pensa allo stesso modo? Ho aggiunto un secondo approccio. – Heroka

+0

@Heroka, questo è fantastico! Come hai intuito, sono incappato nel tuo primo approccio, ma non ero soddisfatto. Il tuo secondo approccio è esattamente quello che volevo. Grazie molto! – dewarrn1

2

Basta fare una selezione di prima. Proof of concept:

df1 <- data.frame(x=1, y=rnorm(10^5)) 
df2 <- subset(df1, y > quantile(df1$y, 0.025) & y < quantile(df1$y, 0.975)) 

ggplot(mapping = aes(x = x, y = y)) + 
    geom_violin(data = df1, aes(fill = '100%'), color = NA) + 
    geom_violin(data = df2, aes(fill = '95%'), color = 'black') + 
    theme_classic() + 
    scale_fill_grey(name = 'level') 

enter image description here

Problemi correlati