r - ggplot2 - split violin plot with more than 2 groups

后端 未结 1 1216
小鲜肉
小鲜肉 2021-01-24 13:36

Continuing from where this thread left off.

I want to make a split violin plot in ggplot 2. The method presented above is limited to 2 categories on the x axis.

相关标签:
1条回答
  • 2021-01-24 14:07

    I figured it out using a for loop to position the shapes by the group factor level.

     set.seed(20160229)
    my_data = data.frame(
    y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5), rnorm(1000, 1.25), rnorm(1000, 0.75)),
    x=c(rep('a', 2000), rep('b', 2000), rep('c', 2000)),
    m=c(rep('i', 1000), rep('j', 1000), rep('i', 1000,rep('j', 1000), rep('i', 1000,rep('j', 1000), rep('i', 1000)))
    
    #Get densities
    library(dplyr)
    pdat <- my_data %>%
    group_by(x, m) %>%
    do(data.frame(loc = density(.$y)$x,
                dens = density(.$y)$y))
    
    #Flip and offset densities for the groups
    pdat$dens <- ifelse(pdat$m == 'i', pdat$dens * -1, pdat$dens)
    
    #Flip and offset densities for x
        #for(pdat$x){pdat$dens <- (pdat$dens + (as.numeric(as.factor(pdat$x))))}
        for(i in 1:nrow(pdat)){(pdat$dens[i] <- (pdat$dens[i] + as.numeric(as.factor(pdat$x[i]))))}
    
    
        #Plot
        library(ggplot2)
        ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) + 
          geom_polygon() +
          scale_x_continuous(breaks = (1:(as.numeric(length(levels(unique(pdat$x)))))), labels = levels(pdat$x)) +
    
          #scale_x_continuous(breaks = length(pdat$x), labels=pdat$x)+
          ylab("y") +
          theme_minimal() +
          theme(axis.title.x = element_blank())
    

    https://i.stack.imgur.com/bxfHh.png

    0 讨论(0)
提交回复
热议问题