【问题标题】:R split violinplot ggplot2R分裂小提琴图ggplot2
【发布时间】:2018-01-15 08:30:01
【问题描述】:

我正在尝试使用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 

【问题讨论】:

  • 我们需要有关您的数据的更多详细信息才能提供帮助。你能粘贴dput(head(my_data, 20))的输出吗?
  • 我附上我的数据和here

标签: r plot ggplot2 split violin-plot


【解决方案1】:

函数density 返回列x 用于位置,y 用于相应密度。您引用了不存在的列 $Sample 和 $VAR2。

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

【讨论】:

  • 非常感谢! =D
  • 我的荣幸。如果可行,请考虑接受我的回答。
  • 它真的很好!
  • 您可以正式接受问题,以免其他人尝试寻找其他解决方案:meta.stackexchange.com/questions/5234/…
猜你喜欢
  • 2016-06-13
  • 1970-01-01
  • 2021-02-16
  • 2018-05-15
  • 1970-01-01
  • 2019-01-21
  • 2018-05-19
  • 2019-12-18
  • 2019-05-21
相关资源
最近更新 更多