ggplot2小提琴情节:只填充中央95%?

时间:2015-12-14 06:33:44

标签: r ggplot2

ggplot2可以创造一个非常有吸引力的填充小提琴情节:

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

如果可能的话,我想将填充限制在95%的分布中心,保持轮廓完好无损。有人有关于如何做到这一点的建议吗?

3 个答案:

答案 0 :(得分:6)

这样做你想要的吗?它需要一些数据处理和两把小提琴的绘制。

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

编辑:圆润的边缘困扰着我,所以这是第二种方法。如果我这样做,我会想要直线。所以我做了一些玩密度(这是小提琴图所基于的)

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

答案 1 :(得分:2)

首先做出选择。概念证明:

Xcode 6

enter image description here

答案 2 :(得分:0)

@Heroka给出了一个很好的答案。根据他的回答,这是一个更通用的功能,可以根据任何范围(而不仅仅是分位数)填充小提琴图。

violincol <-function(x,from = -Inf,to = Inf,col ='grey'){ d <-密度(x)

right <- data.frame(x=d$y, y=d$x) #note flip of x and y, prevents coord_flip later

whichrange <- function(r,x){x <= r[2] & x > r[1]}
ranges <- cbind(from,to)  

right$col <- sapply(right$y,function(y){
    id <- apply(ranges,1,whichrange,y)
    if(all(id==FALSE)) NA else col[which(id)]
})

left <- right[nrow(right):1,]
left$x <- 0 - left$x

dat <- rbind(right,left)

p <- ggplot(dat, aes(x=x,y=y)) +
    geom_polygon(data=dat,aes(fill=col),show.legend = F)+
    geom_path()+
    scale_fill_manual(values=col)
return(p)
}

x <- rnorm(10^5)
violincol(x=x)
violincol(x=x,from=c(-Inf,0),to=c(0,Inf),col=c('green','red'))
r <- seq(-5,5,0.5)
violincol(x=x,from=r,to=r+0.5,col=rainbow(length(r)))