使用ggpubr箱形图其中Y值是列表且使用facet.by时,stat_compare_means无法使用

问题描述

我有94行样品,每列样品中有26种金属浓度。我还有一列用于说明年份的采样(一个因子有6个级别),还有一个用于采样位置(总计3个因子)。 我想按年份将每个元素按位置(共26个图)进行分组,每组包含一组箱形图,其中每个图面均使用stats_compare_means显示均值比较。 我一次可以做一种金属
#example data with 3 Metals
my.df <- data.frame(Location = sample(c("1","2","3"),100,replace = TRUE),Year = as.factor(sample(c("2011","2012","2015","2016","2017","2018"),replace = TRUE)),Co=sample(1:100),Cd=sample(1:100),Ni=sample(1:100))

#define comparisons between all three sites in a list
my_comparisons <- list( c("1","2"),c("2",c("1","3") )

#plot cobalt data grouped by locations faceted by sample year + mean comparisons based on list
Co<-ggBoxplot(my.df,x = "Location",y = "Co",color = "Location",palette = "jco",add="jitter",facet.by = "my.df$Year")+stat_compare_means(comparisons = my_comparisons)

enter image description here

与其对每个元素执行此操作,不如我可以创建一个包含所有元素的列标题列表,并使用该列表定义Y。我确实获得了所有图表,但是stat_compare_means的结果为NULL而不是地块列表。

#list of columns to Feed Y variable
Metals<-colnames(my.df[,3:5])
#Feed this to the Box plot
Metals.all<-ggBoxplot(my.df,y = Metals,facet.by = "my.df$Year")+stat_compare_means(comparisons = my_comparisons)

这行不通吗? 如果没有stat_compare_means,我会得到每种金属的图

Metals.all<-ggBoxplot(my.df,facet.by = "my.df$Year")

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)