0

这是我的大数据矩阵示例 & 每一列都用多个信息命名,并用下划线分隔。

structure(list(Gene = c("AGI4120.1_UBQ", "AGI570.1_Acin"), WT_Tissue_0T_1 = c(0.886461437, 1.093164915), WT_Tissue_0T_2 = c(1.075140682, 1.229862834), WT_Tissue_0T_3 = c(0.632903012, 1.094003128), WT_Tissue_1T_1 = c(0.883151274, 1.26322126), WT_Tissue_1T_2 = c(1.005627276, 0.962729188), WT_Tissue_1T_3 = c(0.87123469, 0.968078993), WT_Tissue_3T_1 = c(0.723601456, 0.633890322), WT_Tissue_3T_2 = c(0.392585237, 0.534819363), WT_Tissue_3T_3 = c(0.640185369, 1.021934772), WT_Tissue_5T_1 = c(0.720291294, 0.589244505), WT_Tissue_5T_2 = c(0.362131744, 0.475251717), WT_Tissue_5T_3 = c(0.549486925, 0.618177919), mut1_Tissue_0T_1 = c(1.464415756, 1.130533457), mut1_Tissue_0T_2 = c(1.01489573, 1.114915728), mut1_Tissue_0T_3 = c(1.171797418, 1.399956009), mut1_Tissue_1T_1 = c(0.927507448, 1.231911575), mut1_Tissue_1T_2 = c(1.089705396, 1.256782289 ), mut1_Tissue_1T_3 = c(0.993048659, 0.999044465), mut1_Tissue_3T_1 = c(1.000993049, 1.103486794), mut1_Tissue_3T_2 = c(1.062562066, 0.883617224 ), mut1_Tissue_3T_3 = c(1.037404833, 0.851875438), mut1_Tissue_5T_1 = c(0.730883813, 0.437440083), mut1_Tissue_5T_2 = c(0.480635551, 0.298762126 ), mut1_Tissue_5T_3 = c(0.85468388, 0.614923997)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"), spec = structure(list( cols = list(Gene = structure(list(), class = c("collector_character", "collector")), WT_Tissue_0T_1 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_0T_2 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_0T_3 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_1T_1 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_1T_2 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_1T_3 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_3T_1 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_3T_2 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_3T_3 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_5T_1 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_5T_2 = structure(list(), class = c("collector_double", "collector")), WT_Tissue_5T_3 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_0T_1 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_0T_2 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_0T_3 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_1T_1 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_1T_2 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_1T_3 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_3T_1 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_3T_2 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_3T_3 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_5T_1 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_5T_2 = structure(list(), class = c("collector_double", "collector")), mut1_Tissue_5T_3 = structure(list(), class = c("collector_double", "collector"))), default = structure(list(), class = c("collector_guess", "collector"))), class = "col_spec"))

我想遵循 Tukey 测试并为每个基因(响应与时间;由两种基因型填充)绘制条形图,并带有多个比较字母。

句法

df1 <- df %>% 
         gather(var, response, WT_Tissue_0T_1:mut1_Tissue_5T_3) %>% 
         separate(var, c("Genotype", "Tissue", "Time"), sep = "_") %>% 
         arrange(desc(Gene))
df2 <- df1 %>% 
         group_by(`Gene`,Genotype,Tissue,Time) %>% 
         mutate(Response=mean(response),n=n(),se=sd(response)/sqrt(n))

双向方差分析

fit1 <- aov(Response ~ Genotype*Time, df2) 

此后,我想继续进行 Tukey 测试(多重比较),例如基因“AGI4120.1_UBQ”,绘制响应与时间,并查看每个基因型(WT 和 mut1)在每个时间点(0T、1T、3T)的行为, 和 5T)? 如果响应显着不同,并在图中用字母表示。

如下 lsmeans 语法将所有基因组合为一个并给出输出,我怎样才能使其分别为每个基因循环(即“AGI4120.1_UBQ”,“AGI570.1_Acin”)并获取字母以显示统计上不同的组(又名“紧凑型字母显示")

 lsmeans(fit1, pairwise ~ Genotype | Time) 

我的最终目标是像下图这样绘制每个基因并表示重要性字母。

df2$genotype <- factor(df2$genotype, levels = c("WT","mut1")) colours <- c('#336600','#ffcc00')library(ggplot2)ggplot(df2,aes( x=Time, y=Response, fill=Genotype))+geom_bar(stat='identity', position='dodge')+scale_fill_manual(values=colours)+geom_errorbar(aes(ymin=average_measure-se, ymax=average_measure+se)+facet_wrap(~`Gene`)+labs(x='Time', y='Response')

表示紧凑字母显示的期望图

如果可能的话,我会很感激你的帮助。

4

1 回答 1

1

您的代码存在许多问题。我什至会说这不是一个适合 StackOverflow 的帖子,因为这里的问题是多种多样的,并且除了这些特定的错误和语法问题之外,并不能真正扩展。但我会发布一些建议作为让您入门的答案 - 希望他们有所帮助。

1. lsmeans()
此函数需要一个拟合模型(如 from lm())或一个ref.grid对象。但是您正在向它传递一个数据框,而没有任何它需要计算最小二乘均值的回归属性。(想一想:当您要求与作为自变量进行成对比较时,如何lsmeans()知道因变量应该是什么Genes?)查看使用lsmeans文档以获取更多详细信息。

根据您的数据,我会说您可能想要运行多级回归,使用类似lme4包的东西,Gene并且Genotype可能Time作为嵌套分组级别。

但为了演示,我会用lm(). 将拟合的回归对象传递给lsmeans()按预期工作:

fit <- lm(Response ~ Gene + Genotype + Time, data=df2)

lsmeans(fit, pairwise ~ Gene)[[2]] 

# Output
contrast                        estimate        SE df t.ratio p.value
AGI4120.1_UBQ - AGI570.1_Acin -0.0515123 0.0299492 42   -1.72  0.0928

Results are averaged over the levels of: Genotype, Time 

2 ggplot().:
你没有定义colours或者average_measure在你提供的代码中;调用这些未声明的变量会导致失败。

从结构上讲,我建议您使用df1并允许ggplot进行分组,而不是group_by进入df2. 然后使用stat="summary"fun.y="mean"完成summarise()您在df2. 这样做还允许您使用mean_se错误栏的功能。像这样:

ggplot(df1,aes( x=Time, y=response, fill=Genotype))+ 
  geom_bar(stat='summary', fun.y='mean', position=position_dodge(0.9))+
    stat_summary(fun.data = mean_se, geom = "errorbar", 
                 color="gray60", width=.1, position=position_dodge(0.9)) +
  scale_fill_manual(values=c("steelblue","orange"))+
  facet_wrap(~`Gene`)+ 
  labs(x='Time', y='Response')

刻面图

最后,请注意,您使用separate()indf1将引发警告,尽管不是错误,因为拆分 on 会产生额外的值sep="_"。您可以通过添加一个级别来捕获最终值(这似乎是一个时间索引)来避免这种情况(如果它引起混淆):

separate(var, c("Genotype", "Tissue", "Time", "Index"), sep = "_")
于 2018-05-30T03:17:13.330 回答