## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## 
## Attaching package: 'rstatix'
## 
## 
## The following object is masked from 'package:stats':
## 
##     filter
# Read data from the Excel file
df <- read_excel("datacitometria.xlsx")

Convert the data to long format, assuming the first column is ‘Concentration’ and the rest are peptide sizes named ‘<3 KDa’, ‘3-5 KDa’, ‘5-10 KDa’, and ‘>10 - <30 Kda’

df_long <- df %>%
  pivot_longer(
    cols = -Concentration, # Exclude the Concentration column from reshaping
    names_to = "Peptide_Size",
    values_to = "Value"
  ) %>%
  mutate(
    Concentration = as.factor(Concentration), # Ensure Concentration is a factor
    Peptide_Size = as.factor(Peptide_Size) # Ensure Peptide_Size is a factor
  )

Run ANOVA for each concentration and extract the ANOVA table

anova_results <- df_long %>%
  group_by(Concentration) %>%
  anova_test(Value ~ Peptide_Size) %>%
  get_anova_table()

anova_results
## # A tibble: 2 × 8
##   Concentration Effect         DFn   DFd     F         p `p<.05`   ges
## * <fct>         <chr>        <dbl> <dbl> <dbl>     <dbl> <chr>   <dbl>
## 1 150           Peptide_Size     4     5  51.1 0.000303  *       0.976
## 2 300           Peptide_Size     4     5 110.  0.0000469 *       0.989

Run pairwise comparisons and add significance labels

pairwise_comparisons <- df_long %>%
  group_by(Concentration) %>%
  pairwise_t_test(Value ~ Peptide_Size, p.adjust.method = "bonferroni",
                  ref.group = "IC")%>%
  add_y_position(ref.group = "IC")


#pairwise_comparisons <- pairwise_comparisons %>%
  #add_xy_position()


pairwise_comparisons
## # A tibble: 8 × 12
##   Concentration .y.   group1 group2           n1    n2        p p.signif   p.adj
##   <fct>         <chr> <chr>  <chr>         <int> <int>    <dbl> <chr>      <dbl>
## 1 150           Value IC     <3 KDa            2     2  1.82e-4 ***      7.28e-4
## 2 150           Value IC     >10 - <30 Kda     2     2  4.91e-5 ****     1.97e-4
## 3 150           Value IC     3-5 KDa           2     2  2.01e-4 ***      8.03e-4
## 4 150           Value IC     5-10 KDa          2     2  9.92e-5 ****     3.97e-4
## 5 300           Value IC     <3 KDa            2     2  3.8 e-5 ****     1.52e-4
## 6 300           Value IC     >10 - <30 Kda     2     2  6.43e-6 ****     2.57e-5
## 7 300           Value IC     3-5 KDa           2     2  4.31e-5 ****     1.72e-4
## 8 300           Value IC     5-10 KDa          2     2  1.84e-5 ****     7.37e-5
## # ℹ 3 more variables: p.adj.signif <chr>, y.position <dbl>, groups <named list>

Ensure Peptide_Size is ordered correctly before plotting

df_long$Peptide_Size <- factor(df_long$Peptide_Size, levels = c("IC", "<3 KDa", "3-5 KDa", "5-10 KDa", ">10 - <30 Kda"))

Plot with significance bars

p <- ggplot(df_long, aes(x = Peptide_Size, y = Value)) +
    geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.8), width = 0.6) +
    geom_errorbar(stat = "summary", fun.data = "mean_se", position = position_dodge(width = 0.8), width = 0.25) +
    facet_wrap(~Concentration, scales = "free")  +
    theme_minimal() +
    theme(legend.position = "none") + # Remove the legend
    labs(x = "Peptide Size", y = "Mean % Infected Cells") +
    stat_pvalue_manual(
      pairwise_comparisons,
      label = "p.adj.signif",
      tip.length = 0
    )
  
  # Print the plot
  print(p)

  library(ggplot2)
  library(dplyr)
  
  # Assuming pairwise_comparisons is already created and in the correct format
  # And df_long is already prepared with 'Concentration' and 'Peptide_Size' as factors
  
  # Define custom labels for the facets
  my_labels <- c(`150` = "A)", `300` = "B)")
  
  p <- ggplot(df_long, aes(x = Peptide_Size, y = Value)) +
    geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.8), width = 0.6) +
    geom_errorbar(stat = "summary", fun.data = "mean_se", position = position_dodge(width = 0.8), width = 0.25) +
    facet_wrap(~Concentration, scales = "free", nrow = 2, labeller = labeller(Concentration = my_labels)) + # Use custom labels
    theme_minimal() +
    theme(legend.position = "none") + # Remove the default strip text
    labs(x = "Peptide Size", y = "Mean % Infected Cells") +
    stat_pvalue_manual(pairwise_comparisons
, label = "p.adj.signif", tip.length = 0)
  
  # Print the plot
  print(p)

library(ggplot2)
library(dplyr)

# Assuming pairwise_comparisons is already created and in the correct format
# And df_long is already prepared with 'Concentration' and 'Peptide_Size' as factors

# Define custom labels for the facets
my_labels <- c(`150` = "A)", `300` = "B)")

barplotfacts <- ggplot(df_long, aes(x = Peptide_Size, y = Value)) +
  geom_bar(stat = "summary", fun = "mean", position = position_dodge(width = 0.8), width = 0.6) +
  geom_errorbar(stat = "summary", fun.data = "mean_se", position = position_dodge(width = 0.8), width = 0.25) +
  facet_wrap(~Concentration, scales = "free", nrow = 2, labeller = as_labeller(my_labels)) + # Use custom labels
  theme_pubr() +
  theme(legend.position = "none", 
        strip.text = element_text(face = "bold", hjust = 0, size = 12), # Customize facet labels
        strip.background = element_blank()) + # Remove the background of labels
  labs(x = "Peptide Size", y = "Mean % Infected Cells") +
  stat_pvalue_manual(pairwise_comparisons, label = "p.signif", tip.length = 0)

# Print the plot
print(barplotfacts)

#ggsave("combined_plotsflowcyt.b", width = 12, height = 14, dpi = 300)