【问题标题】:Broken t-test in facet_wrapfacet_wrap 中的 t 检验损坏
【发布时间】:2020-09-28 15:33:45
【问题描述】:

我正在尝试同时使用 facet_wrap 和 stat_compare_means,但我遇到了问题。 数据的两侧没有相同数量的点。因此 stat_compare_means 失败...... 以图片为例:

Type1“B”有3个点,Type2“B”只有1个点。 这种差异使得几乎所有的 t 检验都失败并且没有被绘制出来。 我需要的是具有匹配点数的组的 t 检验(在这种情况下 Type1 上的所有 t 检验,Type2 中的 A vs C)。 使用的情节如下:

library(RColorBrewer)
library(ggpubr)
library(BBmisc)


adf=read.csv("test1.txt", sep=" ")

myColors <- brewer.pal(length(unique(adf$ID)) ,"Set1")
names(myColors) <- unique(adf$ID)
colScale <- scale_colour_manual(name = "ID",values = myColors)

my_comparison=as.data.frame(combn(unique(adf$sampletype) ,2))
my_comparison=convertColsToList(my_comparison)

ggplot(adf, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
  facet_wrap(~response, scale="free")+
  colScale+
  ggtitle("Entropy")+
  theme(text = element_text(size=20))+
  stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)

数据(另存为test1.txt):

sampletype value ID response
A 8.192 gr_6 Type2
B 13.99 gr_6 Type2
C 9.186 gr_6 Type2
A 5.616 gr_5 Type1
B 15.55 gr_5 Type1
C 7.126 gr_5 Type1
A 5.484 gr_4 Type1
B 12.54 gr_4 Type1
C 4.492 gr_4 Type1
A 9.949 gr_3 Type2
C 6.631 gr_3 Type2
A 2.533 gr_7 Type2
C 12.25 gr_7 Type2
A 2.196 gr_2 Type2
C 6.447 gr_2 Type2
A 11.20 gr_1 Type1
B 16.63 gr_1 Type1
C 6.637 gr_1 Type1

有解决办法吗?

【问题讨论】:

  • 您是否尝试过仅使用 ggpubr 构建绘图而不将其添加到使用 ggplot 构建的绘图中?见这里stackoverflow.com/questions/64051045/…
  • 但是有了这个答案,我将不得不为每个响应构建一个 ggpubr 图,而不是使用 facet_wrap。它可能会解决我的问题,但在这一点上,我也可以拆分数据并继续使用 ggpplot。还是不行?
  • 您可以通过在第一个 ggpubr 调用中添加 facet.by= 或按照此处所述单独添加 rpkgs.datanovia.com/ggpubr/reference/facet.html 在 ggpubr 中进行构面
  • 我尝试运行页面中的代码,因为我不太了解 ggpubr,我最终出现:错误:mutate() 输入问题data。 x "_tidyr_melt_dataframe" 未从当前命名空间 (tidyr) 中解析 ℹ 输入 datamap(.data$data, .f, ...)。运行 rlang::last_error() 以查看错误发生的位置。

标签: r ggplot2 ggpubr


【解决方案1】:

我有办法做到这一点,但提供的数据由于缺乏方差而未能通过 t 检验,所以我确实对其进行了更改。

library(tidyverse)
library(rstatix)
library(ggpubr)
df <- enframe(c("A 8.192 gr_6 Type2",
"B 13.99 gr_6 Type2",
"C 9.186 gr_6 Type2",
"A 5.616 gr_5 Type1",
"B 15.55 gr_5 Type1",
"C 7.126 gr_5 Type1",
"A 5.484 gr_4 Type1",
"B 12.54 gr_4 Type1",
"C 4.492 gr_4 Type1",
"A 9.949 gr_3 Type2",
"C 6.631 gr_3 Type2",
"A 2.533 gr_7 Type2",
"C 12.25 gr_7 Type2",
"A 2.196 gr_2 Type2",
"C 6.447 gr_2 Type2",
"A 11.20 gr_1 Type1",
"B 16.63 gr_1 Type1",
"C 6.637 gr_1 Type1"))
df <- df %>%
  separate(value, into =c("sampletype", "value", "ID", "response"), 
           sep=" ") %>% select(-name) %>%
  mutate(
    value = sample(1:50, 18)
  )

keep_vars <- df %>%
  group_by(response, sampletype) %>% tally() %>% filter(n>1) %>% 
  pivot_wider(names_from = sampletype, values_from = n) %>%split(.$response) %>%
  map(pivot_longer, cols=-c(response)) %>% map(filter,value>=0) %>% bind_rows(.) %>%
  mutate(UID = paste0(response, name)) %>%pull(UID)
  
df_plot <- df %>% mutate(UID = paste0(response, sampletype))  %>%
  filter(UID %in% keep_vars) %>%
  group_by(response) %>%
  t_test(value~sampletype) %>% add_xy_position(x='sampletype')

ggpubr::ggline(df, 
                  x='sampletype', 
                  y='value',
                  color = 'ID',
                  add='jitter',
                  facet.by = 'response'
                  ) +
  stat_pvalue_manual(df_plot, 
                     label = "p.adj.signif", 
                     tip.length = 0.01)

【讨论】:

  • 值是成对的,因此,通过使用 t_test(value~sampletype,paired=TRUE) 您不必改变值。我认为您的解决方案有效,让我尝试一下,但感谢您的努力。
【解决方案2】:

NColl 的解决方案很棒,我也赞同它,但对于我的具体情况,它失败了。特别是当其中一种情况存在单个值时(例如 Type2、A、gr_X 处的单个值)。这是我的错,这个例子缺乏我想要的普遍性。 尽管如此,Ncoll 让我意识到我必须查看我想要比较的变量。 我不想继续打扰,所以我实施了我的解决方案。 在实践中,我绘制了两个 ggplot 并将它们与 gridExtra 合并:

adf_r=adf[adf$sampletype %in% ctime  & adf$response=="Responders", ]
adf_nr=adf[adf$sampletype %in% ctime &d adf$response=="Non-responders", ]

adrmyColors <- brewer.pal(length(unique(adf$ID)) ,"Set1")
names(myColors) <- unique(adf$ID)
colScale <- scale_colour_manual(name = "ID",values = myColors)


for(st in unique(adf_r$sampletype)){
  if(sum(adf_r$sampletype==st)>=3){
    keepvar1=c(keepvar1,st)
  }
}

my_comparison=NULL    
if(length(keepvar1)>1){
  my_comparison=as.data.frame(combn(keepvar1,2))
  my_comparison=convertColsToList(my_comparison)
}

p1=ggplot(adf_r, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
   colScale+
   ggtitle("Entropy")+
   theme(text = element_text(size=20))+
   guides(fill=FALSE)+
   stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)


keepvar2=NULL
for(st in unique(adf_nr$sampletype)){
  if(sum(adf_nr$sampletype==st)>=3){
    keepvar2=c(keepvar2,st)
  }
}

my_comparison=NULL
if(length(keepvar2)>1){
  my_comparison=as.data.frame(combn(keepvar2,2))
  my_comparison=convertColsToList(my_comparison)
}

p2=ggplot(adf_nr, aes(x=sampletype, y=value, fill=sampletype ))+
  geom_point(aes(group=ID, colour=ID))+
  geom_line(aes(group=ID, colour=ID))+
  colScale+
  ggtitle("Entropy")+
  guides(fill=FALSE)+
  theme(text = element_text(size=20))+
  stat_compare_means(comparisons = my_comparison, method = "t.test", paired = TRUE)

final_p=gridExtra::grid.arrange(p1, p2, ncol = 2)

【讨论】:

    猜你喜欢
    • 2012-04-13
    • 2011-08-20
    • 1970-01-01
    • 2011-08-26
    • 2011-07-24
    • 1970-01-01
    • 2012-11-04
    • 2020-01-15
    • 1970-01-01
    相关资源
    最近更新 更多