如何在ggplot2箱线图中使用facet_nested后添加p值或显着性

问题描述 投票:0回答:2

我遇到了一个问题,我不知道我的数据出了什么问题,也不知道如何处理。

这是我的数据和脚本,用于通过 ggplot2 绘制带有面的箱线图。

library(gtools)
library(ggplot2)
library(stringr)
library(ggh4x)
library(openxlsx)
#library(ggplot2)
#library(dplyr)
#library(reshape2)
#library(stringr)
#install.packages("ggpubr")
#install.packages("rstatix")
library(ggpubr)
library(rstatix)
library(ggsignif)
library(ggtext)
dput(Visual_value_2)
    structure(list(Average = c(1, 0.8, 0.4, 1, 1, 1, 0, 0, 0, 0.5, 
    0, 0, 0, 0.875, 0.875, 0.75, 0.875, 1, 0.625, 0.6667, 1, 0.6667, 
    1, 0.2222, 0.1111, 0, 0, 0.5, 0.5, 0, 0, 0.9444, 0.7778, 0.9444, 
    0.8889, 0.5, 0.8333, 1, 0.5714, 1, 0.5, 1, 0.25, 0.4545, 0, 0, 
    0, 0.5, 0.1818, 0, 1, 0.5, 0.5, 0.8, 0.6, 0.4667, 0.6, 1, 1, 
    0.75, 1, 1, 0.2105, 0.1111, 0, 0.2857, 0.2857, 0.5455, 0.1667, 
    0.8, 0.4, 0.5, 0.6, 1, 1, 1, 1, 0.1538, 0.1111, 0, 0, 0, 0, 0, 
    0.875, 0.375, 0.875, 1, 0.5, 0.2, 1, 0.6667, 1, 0, 0.5556, 0.7778, 
    0.625, 0.625, 0.3333, 0, 0.38, 0.52, 0.6, 0.54, 0.14, 0.5, 1, 
    0.44, 0.4375, 0.625, 0.5556, 0.5, 0.8333, 0.3333, 1, 0.6667, 
    1, 1, 1, 0.8333, 0, 0.6667, 1, 0.6, 0.6, 0.75, 0.5, 1, 0.7692, 
    0.3333, 0.6667, 1, 0.8889, 0.8889, 0.7778, 0.8889, 0.3333, 0.8889, 
    0.5, 0, 0.25, 1, 1, 1, 1, 0.6667, 0.7, 0, 0.5, 0.6667, 1, 0.7778, 
    0.6667, 1, 1, 1, 0.7143, 0.8571, 0.7143, 0.3571, 1, 1, 0.8571, 
    0.7778, 0.7778, 1, 1, 1, 1, 0.5714, 0.6923, 1, 1, 0.2273, 0.2083, 
    0.625, 1, 0.9375, 0.875, 0.625, 0.875, 1, 0.4375, 0.6667, 1, 
    1, 0.5, 0.5, 0.8571, 0.8571, 1, 1, 0, 0.8889, 1, 1, 0.7222, 0.6667, 
    1, 1, 0.8333, 1, 1, 0.3333, 1, 1, 1, 0.3714, 0.625, 0.2083, 0.1538, 
    0.375, 0.7059, 1, 1, 0.5, 0.7333, 1, 1, 0.8667, 0.7333, 1, 0.6667, 
    0.5, 1, 0.7143, 0.7143, 0.8571, 0.5, 0.6667, 1, 0.6667, 0.5, 
    0.6667, 0.6, 0.5, 0.75, 1, 1, 0.9, 1, 1, 1, 0.8, 0.9, 1, 0.6, 
    0.8, 0.3333, 1, 0.6364, 0.7059, 0.5714, 0.4615, 0.5833, 0.5385, 
    0.5, 0.3333, 1, 0.6667, 0.7273, 0.9091, 1, 0.8182, 0.6364, 0.8, 
    1, 0.5455, 0.75, 0.75, 0.5, 1, 0.3333, 1, 0.6, 0.6667, 0.75, 
    0.5714, 0.8, 1, 1, 0.8, 0.8889, 1, 0.8, 1, 1, 1, 1, 0.7778, 0.5, 
    0, 0.6667, 0.6667, 1, 1, 0.8, 0.2, 0.8, 0.1667, 0.8182, 1, 1, 
    1, 0.8214, 1, 0.9643, 0.9643, 0.5, 0.8571, 0.8, 0.5, 1, 1, 0.5, 
    1, 0, 1, 0.2, 0, 0.6, 0.6), Group = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2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 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
    2L), levels = c("aaa", "bbb"), class = "factor")), row.names = c(157L, 
    158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 
    169L, 194L, 195L, 196L, 197L, 198L, 199L, 200L, 201L, 202L, 203L, 
    204L, 205L, 206L, 207L, 208L, 209L, 210L, 211L, 325L, 326L, 327L, 
    328L, 329L, 330L, 331L, 332L, 333L, 334L, 335L, 336L, 337L, 338L, 
    339L, 340L, 341L, 342L, 343L, 369L, 370L, 371L, 372L, 373L, 374L, 
    375L, 376L, 377L, 378L, 379L, 380L, 381L, 382L, 383L, 384L, 385L, 
    386L, 387L, 414L, 415L, 416L, 417L, 418L, 419L, 420L, 421L, 422L, 
    423L, 424L, 425L, 426L, 427L, 428L, 237L, 238L, 239L, 240L, 241L, 
    242L, 243L, 244L, 245L, 246L, 247L, 248L, 249L, 250L, 281L, 282L, 
    283L, 284L, 285L, 286L, 287L, 288L, 289L, 290L, 291L, 292L, 293L, 
    294L, 295L, 296L, 297L, 298L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 
    78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 112L, 
    113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 
    124L, 125L, 126L, 127L, 128L, 129L, 130L, 1L, 2L, 3L, 4L, 5L, 
    6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
    19L, 20L, 21L, 22L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 
    55L, 56L, 57L, 58L, 59L, 60L, 451L, 452L, 453L, 454L, 455L, 456L, 
    457L, 458L, 459L, 460L, 461L, 462L, 463L, 464L, 465L, 466L, 467L, 
    468L, 469L, 470L, 471L, 472L, 473L, 501L, 502L, 503L, 504L, 505L, 
    506L, 507L, 508L, 509L, 510L, 511L, 512L, 513L, 514L, 515L, 516L, 
    517L, 518L, 519L, 520L, 521L, 522L, 523L, 550L, 551L, 552L, 553L, 
    554L, 555L, 556L, 557L, 558L, 559L, 560L, 561L, 562L, 563L, 564L, 
    565L, 566L, 567L, 568L, 569L, 570L, 597L, 598L, 599L, 600L, 601L, 
    602L, 603L, 604L, 605L, 606L, 607L, 608L, 609L, 610L, 611L, 612L, 
    613L, 614L, 615L, 616L, 617L, 645L, 646L, 647L, 648L, 649L, 650L, 
    651L, 652L, 653L, 654L, 655L, 656L, 657L, 658L, 659L, 660L, 661L, 
    662L, 684L, 685L, 686L, 687L, 688L, 714L, 715L, 716L, 717L, 718L, 
    719L, 720L, 721L, 722L, 723L, 724L, 725L, 726L, 727L, 728L, 729L, 
    730L, 731L, 732L, 733L, 734L), class = "data.frame")
    
    stat.test <- Visual_value_2 %>%
      group_by(Group2) %>%
      t_test(
        Average ~ Group
        #p.adjust.method = "BH"
      ) %>%
      adjust_pvalue(method = "BH") %>%
      add_significance() %>%
      add_xy_position(x = "Group")
    
    p5<-ggplot(Visual_value_2, aes(x=fct_rev(Group2), y=Average, fill=Group)) +
      geom_boxplot(position=position_dodge(1),na.rm = T,fatten = 1)+
      facet_nested(.~Group2+Group,
                   #cols=vars(dmr,Fill),
                   scales="free_x",
                   switch = "x",
                   #axes = "y",
                   # drop = F,
                   #margins = T,
                   solo_line = F,
                   space='free_x')+
      theme_bw()+
      guides(x = "axis_truncated", y = "axis_truncated")+
      theme(#
        #plot.title = element_text(hjust = 0.5,size = 15),
        axis.line = element_line(colour = "black"),
        #axis.title.x=element_blank(),
        plot.title = element_markdown(hjust = 0.5,size = 15),
        axis.text.x = element_blank(), 
        axis.ticks.x = element_blank(),
        panel.spacing=unit(0,"lines"),
        panel.border = element_blank(), # Remove internal facet borders
        #panel.background = element_rect(colour = "black", linewidth =0),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),  # Remove major grid lines
        panel.grid.minor = element_blank(),  # Remove minor grid lines
        axis.text = element_text(size = 15),axis.title = element_text(size = 15),
        #legend.text = element_text(size = 11),legend.title = element_text(size = 13),
        plot.margin = unit(c(0.4,0.4,0.4,0.4),'cm'),
        #strip.switch.pad.grid = unit(0, "cm"),
        strip.text.y = element_text(size = 12))+
      guides(color=guide_legend(override.aes = list(size=6),
      ))
    p5
    
    p5 <- p5 + stat_pvalue_manual(
      stat.test, 
      label = "p.adj.signif", 
      tip.length = 0.01, 
      hide.ns = TRUE,
      y.position = "y.position"
    )
    p5     

我觉得脚本还可以,但实际上行不通。

这里的错误:

Error:
! Problem while computing aesthetics.
i Error occurred in the 2nd layer.
Caused by error:
! object 'Group' not found
Run `rlang::last_trace()` to see where the error occurred.

我真的很困惑。

第一步制作箱线图就ok了。但是下一步添加p值后出现错误。

希望各位专家能给我一些建议或解决方案。

提前致谢。

r ggplot2 boxplot p-value
2个回答
0
投票

更新:

好吧,没有人回答我的问题,我在某种程度上真的很困惑。

我尝试了 ggboxplot,虽然我不喜欢它。

#?facet_grid
bxp <- ggboxplot(
  Visual_value_2, x = "Tissue", y="Average", fill="Tissue",
  #palette = "jco", 
  facet.by = "dmr"
)
bxp 
stat.test <- Visual_value_2 %>%
  group_by(dmr) %>%
  t_test(Average ~ Tissue) %>%
  add_xy_position(x = "Tisue", dodge = 0.8)
stat.test <- stat.test %>%
  add_xy_position(x = "dmr", dodge = 0.8)
stat.test

bxp + 
  stat_pvalue_manual(
    stat.test, label = "p.adj.signif", tip.length = 0.01,
    hide.ns = TRUE
  ) +
  scale_y_continuous(expand = expansion(mult = c(0.01, 0.1)))

enter image description here

所以新问题是为什么重要性与框不匹配??

我也修改了以下信息,但不起作用。

dodge = 0.8 或类似的参数,如框宽度或 位置=位置闪避(1)

有人可以帮助我解决这个问题吗?如果您能给我答复,我将不胜感激。

非常感谢。


0
投票

更新:

好吧,我认为这是我的错或者是一个错误

facet_grid() 效果很好,而facet_nested() 不适合这里。

我一次又一次尝试,发现下面的脚本可以工作。

pp<-ggplot(Visual_value_2, aes(x = Tissue, y=Average)) + 
  geom_boxplot(aes(fill=Tissue),outlier.size=0.5) + 
  theme_classic()+
  theme(axis.text.x = element_text(
    #angle =0, 
    vjust = 0.5, 
    #hjust=1,
    size = 12),
    axis.text = element_text(face="bold"))+
  theme(legend.position = "none")+
  facet_grid(
             .~dmr,
             scales="free_x",
             switch = "x",
             # drop = F,
             space='free'
             )+
  # stat_pvalue_manual(
  #   stat.test, 
  #   label = "p", 
  #   tip.length = 0.01,
  #   #step.increase = c(0.2,0.2),
  #   vjust = 0.01
  # )+
  scale_y_continuous(limits = c(0, 2),breaks = c(0.0,0.3,0.6,0.75,0.9,1.2,2))
pp
pp+
  stat_pvalue_manual(
    stat.test, 
    label = "p.adj.signif", 
    tip.length = 0.01, 
    hide.ns = TRUE,
    y.position = "y.position"
  )

#?facet_grid
bxp <- ggboxplot(
  Visual_value_2, x = "Tissue", y="Average", fill="Tissue",
  #palette = "jco", 
  facet.by = "dmr"
)
bxp 
stat.test <- Visual_value_2 %>%
  group_by(dmr) %>%
  t_test(Average ~ Tissue) %>%
  add_xy_position(x = "Tisue", dodge = 0.8)
stat.test <- stat.test %>%
  add_xy_position(x = "dmr", dodge = 0.8)
stat.test

bxp + 
  stat_pvalue_manual(
    stat.test, label = "p.adj.signif", tip.length = 0.01,
    hide.ns = TRUE
  ) +
  scale_y_continuous(expand = expansion(mult = c(0.01, 0.1)))

修改是我将 fill=Tissue 移至 geom_boxplot() 中。

并直接从 ggboxplot 到 ggplot。

谢谢大家。完成了。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.