Search code examples
rggplot2ggpubr

Only showing significant p-values and annotations in ggplot2, using ggpubr


I am trying to generate a figure, with p-values shown at the top. I only want the p-values to show if they are less than 0.05. I can do this, as shown below, but the bars for the other p-values, which are not shown, remain. Is there a way to remove all of the annotations if the p-value for that comparison is greater than 0.05?

library(ggplot2)
library(ggpubr)

select_tab_cond <- structure(list(condition = structure(c(12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L), 
                                  levels = c("MUT1", "MUT2", "MUT3", "MUT4", "MUT5", "MUT6", "MUT7", "MUT8", "MUT9", "MUT10", "MUT_CTL", "WT_CTL"), class = "factor"), 
                                  Phase = c("Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light"), 
                                  n_samples = c(16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16), 
                                  exp_sums = c(117021, 102318, 94897, 36513, 37749, 105215, 210122, 135288, 234010, 339447, 368448, 353097, 46403, 28492, 10801, 6573, 9919, 59014, 103711, 98088, 150069, 197500, 205270, 156664), 
                                  mean = c(7313.8125, 6394.875, 5931.0625, 2282.0625, 2359.3125, 6575.9375, 13132.625, 8455.5, 14625.625, 21215.4375, 23028, 22068.5625, 2900.1875, 1780.75, 675.0625, 410.8125, 619.9375, 3688.375, 6481.9375, 6130.5, 9379.3125, 12343.75, 12829.375, 9791.5), 
                                  median = c(6245, 4971.5, 4388.5, 266.5, 1217.5, 6372, 13760, 6585.5, 13133, 21116, 20260.5, 22605.5, 1600, 1407, 207, 47.5, 236.5, 1689, 6854, 4422, 8872, 12615, 13605, 9480.5), 
                                  sd = c(6115.2127814574, 5692.07899775352, 6429.39608847518, 3900.58823715518, 2687.43408523819, 5074.04760809028, 6197.19779550941, 8966.10403687131, 6457.70916940881, 7937.58613155368, 11013.5447881234, 7929.45546653951, 3241.83697346119, 1851.81300351844, 1149.14208397685, 555.19464679816, 931.112056897557, 4624.50314268102, 4096.41302391495, 6910.45690722883, 5909.45542577035, 2226.4432472743, 6460.5826659314, 4045.45155287598), 
                                  sem = c(1528.80319536435, 1423.01974943838, 1607.3490221188, 975.147059288794, 671.858521309546, 1268.51190202257, 1549.29944887735, 2241.52600921783, 1614.4272923522, 1984.39653288842, 2753.38619703085, 1982.36386663488, 810.459243365297, 462.953250879611, 287.285520994213, 138.79866169954, 232.778014224389, 1156.12578567026, 1024.10325597874, 1727.61422680721, 1477.36385644259, 556.610811818575, 1615.14566648285, 1011.36288821899), 
                                  CI_lower = c(4317.35823708587, 3605.75629110078, 2780.65841664716, 370.774263793963, 1042.46979823329, 4089.65417203576, 10095.9980802004, 4062.10902193306, 11461.3475069897, 17326.0202955387, 17631.3630538195, 18183.1293213956, 1311.68738300402, 873.361628275963, 111.982878851342, 138.767123068902, 163.692592120197, 1422.3684600863, 4474.69511828168, 2744.37611545787, 6483.67934137253, 11252.7928088356, 9663.68949369361, 7809.22873909077), 
                                  CI_upper = c(10310.2667629141, 9183.99370889922, 9081.46658335284, 4193.35073620604, 3676.15520176671, 9062.22082796424, 16169.2519197996, 12848.8909780669, 17789.9024930103, 25104.8547044613, 28424.6369461805, 25953.9956786044, 4488.68761699598, 2688.13837172404, 1238.14212114866, 682.857876931098, 1076.1824078798, 5954.3815399137, 8489.17988171832, 9516.62388454213, 12274.9456586275, 13434.7071911644, 15995.0605063064, 11773.7712609092)), 
                             row.names = c(NA, -24L), 
                             class = c("data.table", "data.frame"))

max_row_index <- which.max(select_tab_cond$mean)

max_sd_value <- select_tab_cond[max_row_index, "sd"]

top <- max(select_tab_cond$mean) + max_sd_value

top_plus <- as.integer(top + top*0.045)

ttest_results <- structure(list(condition = c("WT_CTL", "MUT1", "MUT2", "MUT3", "MUT4", "MUT5", "MUT6", "MUT7", "MUT8", "MUT9", "MUT10", "MUT_CTL"), 
                                phase1 = c("Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark", "Dark"), 
                                phase2 = c("Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light", "Light"), 
                                mean_phase1 = c(121.896875, 106.58125, 98.8510416666667, 38.034375, 39.321875, 109.598958333333, 218.877083333333, 140.925, 243.760416666667, 353.590625, 383.8, 367.809375), 
                                mean_phase2 = c(93.554435483871, 57.4435483870968, 21.7762096774194, 13.2520161290323, 19.9979838709677, 118.979838709677, 209.094758064516, 197.758064516129, 302.558467741935, 398.185483870968, 413.850806451613, 315.854838709677), 
                                statistic = c(0.776364230767638, 1.75321971841152, 2.71899604992884, 1.47007321058792, 1.43328098648914, -0.218820827071926, 0.233302454710926, -0.847080497958769, -1.07436527415354, -1.18499863745119, -0.432825698846891, 1.11892872909341), 
                                df = c(29.9801794938917, 25.278812262899, 18.5394196689052, 17.2637911387746, 26.2213795621827, 23.738499578623, 28.3466871357047, 26.2163646592435, 23.6810768625728, 23.1352449193132, 29.5291586190763, 29.9952128626254), 
                                p.value = c(0.443616226073701, 0.0916847650175253, 0.0138208089919202, 0.159531900263495, 0.163594334585789, 0.828660050800472, 0.817203105733135, 0.404617113072423, 0.293484573390386, 0.248048300181225, 0.668286371634389, 0.272051458077831), 
                                method = c("Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test", "Welch Two Sample t-test")), 
                           row.names = c(NA, -12L), class = c("data.table", "data.frame"))

#This generates a graph for the total of the movement data in the dark phases
p_mean_condition <- ggplot(select_tab_cond, aes(x = condition, y = mean, fill = Phase, color = Phase)) +
  geom_line(lwd = 1) + 
  theme_classic(base_size = 20) +
  scale_fill_discrete(name = "Phase") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.spacing.x = unit(0.5, "cm")) +
  geom_bar(stat = "identity", position = "dodge", color = 'black') +
  labs(title = " ", x = 'Conditions', y = "Means of Pixel Differences") + 
  geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.1, position = position_dodge(width = 0.9), color = 'black') +
  geom_signif(
    xmin = 1:nrow(ttest_results)-0.2, 
    xmax = 1:nrow(ttest_results)+0.2,
    y_position = rep(top_plus, nrow(ttest_results)),
    annotation = ifelse(ttest_results$p.value < 0.05, round(ttest_results$p.value, 3), ""), tip_length = 0.025, color='black', textsize = 9,
    map_signif_level = TRUE
    )

enter image description here


Solution

  • You could filter your ttest_results dataset for the significant conditions:

    library(ggplot2)
    library(ggsignif)
    library(dplyr)
    
    ttest_results <- ttest_results |> 
      mutate(
        id = as.numeric(factor(condition, levels = levels(select_tab_cond$condition))),
        xmin =  id - .2,
        xmax = id + .2
      ) |> 
      filter(p.value < .05)
    
    ggplot(select_tab_cond, aes(x = condition, y = mean, fill = Phase, color = Phase)) +
      theme_classic(base_size = 20) +
      scale_fill_discrete(name = "Phase") +
      theme(
        axis.text.x = element_text(angle = 45, hjust = 1),
        plot.title = element_text(hjust = 0.5),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.spacing.x = unit(0.5, "cm")
      ) +
      geom_bar(stat = "identity", position = "dodge", color = "black") +
      labs(title = " ", x = "Conditions", y = "Means of Pixel Differences") +
      geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = 0.1, position = position_dodge(width = 0.9), color = "black") +
      geom_signif(
        xmin = ttest_results$xmin,
        xmax = ttest_results$xmax,
        y_position = rep(top_plus, nrow(ttest_results)),
        annotation = round(ttest_results$p.value, 3), tip_length = 0.025, color = "black", textsize = 9,
        map_signif_level = TRUE
      )
    

    enter image description here