library(datarium)
library(rstatix)
## 
## Attaching package: 'rstatix'
## The following object is masked from 'package:stats':
## 
##     filter
library(tidyverse)
## ── Attaching packages
## ───────────────────────────────────────
## tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0      ✔ purrr   1.0.0 
## ✔ tibble  3.1.8      ✔ dplyr   1.0.10
## ✔ tidyr   1.2.1      ✔ stringr 1.5.0 
## ✔ readr   2.1.3      ✔ forcats 0.5.2 
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks rstatix::filter(), stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(datarium)
library(shiny)
library(emmeans)
library(gt)
library(gtsummary)
library(ggpubr)

##PRACTICA DEL DIA MIERCOLES

jobsatisfaction
## # A tibble: 58 × 4
##    id    gender education_level score
##    <fct> <fct>  <fct>           <dbl>
##  1 1     male   school           5.51
##  2 2     male   school           5.65
##  3 3     male   school           5.07
##  4 4     male   school           5.51
##  5 5     male   school           5.94
##  6 6     male   school           5.8 
##  7 7     male   school           5.22
##  8 8     male   school           5.36
##  9 9     male   school           4.78
## 10 10    male   college          6.01
## # … with 48 more rows
bp2<-ggplot(jobsatisfaction, aes(x=gender, y=score, color= education_level))+
  geom_boxplot(aes(fill=education_level),colour = "black")+ 
  geom_jitter(colour="black", alpha=0.5)+
  scale_fill_manual(values = c("#BB9E0D", "#033190", "#DBE0EA"))+theme_classic()

bp2

pwc2 <- jobsatisfaction %>% 
  group_by(gender) %>% 
  tukey_hsd(score ~ education_level)

pwcc <- jobsatisfaction %>% 
  group_by(gender) %>% emmeans_test(score~education_level, p.adjust.method = "bonferroni")


pwcc %>% gt()
gender term .y. group1 group2 df statistic p p.adj p.adj.signif
female education_level score school college 52 -2.935226 4.952503e-03 1.485751e-02 *
female education_level score school university 52 -10.834318 6.074300e-15 1.822290e-14 ****
female education_level score college university 52 -7.899092 1.838428e-10 5.515284e-10 ****
male education_level score school college 52 -3.072573 3.374111e-03 1.012233e-02 *
male education_level score school university 52 -15.295045 6.865425e-21 2.059628e-20 ****
male education_level score college university 52 -12.142652 8.421634e-17 2.526490e-16 ****
pwc2
## # A tibble: 6 × 10
##   gender term     group1 group2 null.…¹ estim…² conf.…³ conf.…⁴    p.adj p.adj…⁵
## * <fct>  <chr>    <chr>  <chr>    <dbl>   <dbl>   <dbl>   <dbl>    <dbl> <chr>  
## 1 male   educati… school colle…       0   0.797  0.341     1.25 5.59e- 4 ***    
## 2 male   educati… school unive…       0   3.87   3.42      4.31 3.28e-14 ****   
## 3 male   educati… colle… unive…       0   3.07   2.62      3.51 3.99e-14 ****   
## 4 female educati… school colle…       0   0.722 -0.0163    1.46 5.62e- 2 ns     
## 5 female educati… school unive…       0   2.66   1.93      3.40 4.3 e- 9 ****   
## 6 female educati… colle… unive…       0   1.94   1.20      2.68 1.58e- 6 ****   
## # … with abbreviated variable names ¹​null.value, ²​estimate, ³​conf.low,
## #   ⁴​conf.high, ⁵​p.adj.signif
pwc2 <- pwc2 %>% add_xy_position(x="gender")

size <- 5 #tamaño de los asteriscos
bp2 + stat_pvalue_manual(pwc2,label.size = size) + theme(text = element_text(size = 15)) +
  labs(fill="Education level")+theme(legend.position = "top")