ggstatsplot

ANOVA
Author

Duc Nguyen

Published

October 12, 2026

Cần sắp xếp theo đúng thứ tự cột x_ok rồi mới vẽ đồ thị.

Chỗ lệnh p_gtable$grobs[[5]], thỉnh thoảng version cũ ggplot2 sẽ chỉnh về p_gtable$grobs[[6]] hoặc ta sẽ dò thủ công lại vị trí của các grid object này.

Download dataset df2

# df <- PlantGrowth
# names(df) <- c("y_ok", "x_ok")
# 
# library(dplyr)
# df |> dplyr:::arrange(x_ok) -> df
# 
# df

df <- readRDS(paste0(getwd(), "/df2.rds"))

df <- df[ , -5]


library(dplyr)
df |> dplyr:::arrange(STUDY_ARM) -> df

df
   STUDY_ID     STUDY_ARM BEGIN_CD3+_T_CELL_FREQ FU_CD3+_T_CELL_FREQ_ok
1      ID_1   CONTROL_OLD              12.063157              12.063157
2      ID_2   CONTROL_OLD              10.964920              10.964920
3      ID_4   CONTROL_OLD              13.720531              13.720531
4      ID_5   CONTROL_OLD              10.207501              10.207501
5      ID_6   CONTROL_OLD              10.781260              10.781260
6      ID_7   CONTROL_OLD               8.561287               8.561287
7      ID_9   CONTROL_OLD              12.419222              12.419222
8     ID_11   CONTROL_OLD               9.877577               9.877577
9     ID_12   CONTROL_OLD              11.327839              11.327839
10    ID_15   CONTROL_OLD              12.618815              12.618815
11    ID_16   CONTROL_OLD               9.354778               9.354778
12    ID_18   CONTROL_OLD              10.481444              10.481444
13    ID_19   CONTROL_OLD              10.954867              10.954867
14    ID_29   CONTROL_OLD              10.438020              10.438020
15    ID_21 CONTROL_YOUNG              12.011571              12.011571
16    ID_23 CONTROL_YOUNG              12.267157              12.267157
17    ID_27 CONTROL_YOUNG               9.660034               9.660034
18    ID_28 CONTROL_YOUNG              12.845677              12.845677
19     ID_3          YEGD               6.135557              15.411331
20     ID_8          YEGD              10.378400              36.150698
21    ID_10          YEGD               4.698543              53.858413
22    ID_17          YEGD               4.883078              30.915139
23    ID_20          YEGD              10.257006              12.503797
24    ID_22          YEGD               9.304376              45.202878
25    ID_24          YEGD               9.591158               8.710753
26    ID_25          YEGD              10.470832              10.584096
27    ID_26          YEGD              12.824044              42.963687
28    ID_30          YEGD               9.466437              62.817237
library(ggstatsplot)
library(ggplot2)
library(grid)
library(gridExtra)

png(filename = paste0(getwd(), "/g1.png"),
    width = 8,
    height = 8,
    units = "in",
    res = 300)



# options(scipen = 4)
# options(digits = 4)
# options(pillar.sigfig = 4)


p1 <- ggbetweenstats(
  
                     # data = df,
                     # x = x_ok,
                     # y = y_ok,
                     
                     data = df,
                     x = STUDY_ARM,
                     y = `FU_CD3+_T_CELL_FREQ_ok`,
                     
                     
                     type = "nonparametric", 
                     plot.type = "box",
                     pairwise.comparisons = T,
                     pairwise.display = "significant",
                     p.adjust.method = "bonferroni",
                     centrality.plotting = F,
                     centrality.path = FALSE,
                     digits = 4L,
                     ggtheme = theme_classic(base_size = 20),
                     bf.message = F,
                     results.subtitle = F,
                     caption = "",
                     ggsignif.args = list(size = 0.5,
                                          vjust = 0,
                                          step_increase = 0.15,
                                          textsize = 8),
                     violin.args = list(width = 0, linewidth = 0, colour = NA),
                     boxplot.args = list(width = 0),
                     point.args = list(position = ggplot2::position_jitterdodge(dodge.width = 0.6),
                                       alpha = 1,
                                       size = 6,
                                       stroke = 0, 
                                       na.rm = TRUE),
                     ) +
  
  theme(legend.position = "none") +
  
  geom_boxplot(median.color = "black",
               color = NA,
               fill = NA) +
  
  labs(color = "Note") +
  
  coord_cartesian(ylim = c(0, 70),
                  clip = "off")

b1 <- ggplot_build(p1)
p_gtable <- ggplot_gtable(b1)

p_gtable -> p_gtable_ori

library(lemon)
lemon::gtable_show_names(p_gtable)

p_gtable$grobs[[13]]$children[[1]]$label <- NULL

p_gtable$grobs[[5]]$children[[4]] <- NULL

# chinh so luong o day, check table(df$STUDYARM)
p_gtable$grobs[[5]]$children[[3]]$pch <- c(rep(15,
                                             times = 14),
                                           rep(16,
                                               times = 4),
                                           rep(17,
                                               times = 10)
                                           )

# chinh so luong o day, check table(df$STUDYARM)
p_gtable$grobs[[5]]$children[[3]]$gp$col <- c(rep(adjustcolor(col = "red", alpha.f = 0.5),
                                                  times = 14),
                                              rep(adjustcolor(col = "blue", alpha.f = 0.5),
                                                  times = 4),
                                              rep(adjustcolor(col = "darkgreen", alpha.f = 0.5),
                                                  times = 10)
)

grid::grid.newpage()
grid::grid.draw(p_gtable)


dev.off()