R split violinplot ggplot2

时间:2017-08-07 23:13:02

标签: r plot ggplot2 split violin-plot

我正在尝试用ggplot2制作分裂小提琴情节,类似于this。我找到了一个非常好的code,但我无法使用它,因为当我尝试创建pdat时,它是空的,我不知道它为什么会发生。下面我附上我的数据摘要和我正在做的事情,以及结果。有人可以帮帮我吗?

我的数据摘要:

summary(object = my_data)
      Sample    Treatment         VAR1            VAR2      
 Sample_1:500   Cond1:1000   Min.   :36.00   Min.   :21.13  
 Sample_2:500   Cond2:1000   1st Qu.:90.00   1st Qu.:36.92  
 Sample_3:500                Median :90.00   Median :38.11  
 Sample_4:500                Mean   :88.91   Mean   :37.53  
                             3rd Qu.:90.00   3rd Qu.:38.90  
                             Max.   :90.00   Max.   :40.60  


dput(head(my_data, 20))
structure(list(Sample = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("Sample_1", 
"Sample_2", "Sample_3", "Sample_4"), class = "factor"), Treatment = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L), .Label = c("Cond1", "Cond2"), class = "factor"), 
    VAR1 = c(90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 90, 
    90, 90, 90, 90, 90, 90, 90, 90), VAR2 = c(34.8888888888889, 
    38.2333333333333, 32.8333333333333, 37.7111111111111, 38.4111111111111, 
    36.7222222222222, 34.5555555555556, 35.7666666666667, 37.7111111111111, 
    37.3777777777778, 36.4888888888889, 37.8222222222222, 35.4777777777778, 
    34.0333333333333, 37.1222222222222, 39.0555555555556, 38.5666666666667, 
    34.8555555555556, 38.6, 34.6555555555556)), .Names = c("Sample", 
"Treatment", "VAR1", "VAR2"), row.names = c(NA, 20L), class = "data.frame")

我在做什么:

library(dplyr)
pdat <- my_data %>%
  group_by(Sample, Treatment) %>%
  do(data.frame(loc = density(.$VAR2)$Sample,
                dens = density(.$VAR2)$VAR2))

结果:

summary(object = pdat)
      Sample  Treatment
 Sample_1:0   Cond1:0  
 Sample_2:0   Cond2:0  
 Sample_3:0            
 Sample_4:0 

1 个答案:

答案 0 :(得分:1)

函数density返回列的x列和相应密度的y列。您可以参考未列出的列$Sample$VAR2

pdat <- data %>%
  group_by(Sample, Treatment) %>%
  do(data.frame(loc = density(.$VAR2)$x,
                dens = density(.$VAR2)$y))