I wouldl like to use stat_pvalue_manual in order to add pvalue on my geom barplot. I have these following dataset and stat.table (created to reproduce my data). I can add the pvalue, but the bracket does not appear as it should. Can you help me to see what is wrong in my code.
# data
#########################
Data_test=structure(list(PE = c(87, 32, 15, 87, 43, 65, 76, 25),
BU = c("A1", "A2", "A3", "A4", "A1", "A2", "A3", "A4"),
PH = c("NAF", "NAF","NAF", "NAF", "AF", "AF", "AF", "AF")),
class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
row.names = c(NA, -8L),
groups = structure(list(BU = c("A1","A1", "A2", "A2", "A3", "A3","A4", "A4"),
PH = c("AF","NAF", "AF", "NAF", "AF", "NAF","AF", "NAF"), .rows = structure(list(5L, 1L, 6L, 2L, 7L, 3L, 8L, 4L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -8L), .drop = TRUE))
# stat table
#########################
Stat_test= structure(list(group1 = c("NAF", "NAF", "NAF",
"NAF"), group2 = c("AF", "AF", "AF",
"AF"), PE = c(65, 87, 87,
43),
p = c(0.7654,
0.456, 0.789, 0.003), BU = c("A1", "A2", "A3", "A4")), class = "data.frame", row.names = c(NA,
-4L))
# Graph
#########################
library(ggpubr)
library(ggplot2)
ggplot(Data_test, aes(x=BU, y=PE)) +
geom_bar(aes(fill=PH), stat="identity", position=position_dodge(), color="black") +
stat_pvalue_manual(Stat_test, y.position = 100, size=4,
label = "p", inherit.aes = FALSE, remove.bracket = FALSE, x="BU",
tip.length = 0.02, bracket.size = 4, ) +
ylim(c(0,100))
> Data_test
# A tibble: 8 × 3
# Groups: BU, PH [8]
PE BU PH
<dbl> <chr> <chr>
1 87 A1 NAF
2 32 A2 NAF
3 15 A3 NAF
4 87 A4 NAF
5 43 A1 AF
6 65 A2 AF
7 76 A3 AF
8 25 A4 AF
> Stat_test
group1 group2 PE p BU ypos
1 NAF AF 65 0.7654 A1 100
2 NAF AF 87 0.4560 A2 100
3 NAF AF 87 0.7890 A3 100
4 NAF AF 43 0.0030 A4 100
There may be an easier way, but adding xmin
and xmax
values to your Stat_test
data frame will do the trick:
ggplot(Data_test, aes(x = BU, y = PE)) +
geom_col(aes(fill = PH), position = position_dodge(), color = "black") +
stat_pvalue_manual(Stat_test %>%
mutate(xmin = 1:4 -0.2, xmax = 1:4 + 0.2),
y.position = 100, size = 4, label = "p",
tip.length = 0.02, bracket.size = 0.8) +
ylim(c(0, 100)