r - ggplot2 - 将小提琴剧情分为两组以上 [英] r - ggplot2 - split violin plot with more than 2 groups

查看:170
本文介绍了r - ggplot2 - 将小提琴剧情分为两组以上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续从此线程离开。



我想在ggplot 2中创建一个拆分的小提琴图。上面介绍的方法仅限于x轴上的2个类别。

例子:

  set.seed(20160229)
my_data = data.frame(
y = c(rnorm(1000),rnorm(1000,0.5),rnorm(1000,1),rnorm(1000,1.5)),
x = c(rep ('',2000),rep('b',2000)),
m = c(rep('i',1000) ))

#获取密度
库(dplyr)
pdat< - my_data%>%
group_by(x,m)%>%
do(data.frame(loc = density(。$ y)$ x,
dens = density(。$ y)$ y))

# group
pdat $ dens< - ifelse(pdat $ m =='i',pdat $ dens * -1,pdat $ dens)
pdat $ dens< - ifelse(pdat $ x == 'b',pdat $ dens + 1,pdat $ dens)
Plot

ggplot(pdat,aes(dens,loc,fill = m,group = interaction(m,x)) )+
geom_polygon()+
scale_x_continuous(breaks = 0:1,labels = c('a','b'))+
ylab('density')+
theme_minimal()+
theme(axis.title.x = element_blank())

< img src =https://i.stack.imgur.com/0GByF.pngalt =结果>



看起来像放置超过2 X轴上的类别是可能的,但我不知道如何去做。

解决方案

使用for循环将组形式定位到组因子级别。 b

  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(' ('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)))

#获取密度
library(dplyr)
pdat< - my_data%>%
group_by(x,m)%>%
do(data.frame(loc = density(。$ y)$ x,
dens = density(。$ y)$ y))

#组的滑动和偏移密度
pdat $ dens< - ifelse(pdat $ m = ='i',pdat $ dens * -1,pdat $ dens)

#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

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.

Example:

set.seed(20160229)
my_data = data.frame(
y=c(rnorm(1000), rnorm(1000, 0.5), rnorm(1000, 1), rnorm(1000, 1.5)),
x=c(rep('a', 2000), rep('b', 2000)),
m=c(rep('i', 1000), rep('j', 2000), 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)
pdat$dens <- ifelse(pdat$x == 'b', pdat$dens + 1, pdat$dens)
Plot

ggplot(pdat, aes(dens, loc, fill = m, group = interaction(m, x))) + 
geom_polygon() +
  scale_x_continuous(breaks = 0:1, labels = c('a', 'b')) +
  ylab('density') +
  theme_minimal() +
  theme(axis.title.x = element_blank())

It looks like placing more than 2 categories on the x axis is possible but I'm not sure how to go about it.

解决方案

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

这篇关于r - ggplot2 - 将小提琴剧情分为两组以上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆