JA_pilot_analysis.R 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. rm(list = ls())
  2. library(openxlsx)
  3. library(dplyr)
  4. library(ggplot2)
  5. library(tidyverse)
  6. library(ez)
  7. library(plotly)
  8. library(ggsignif)
  9. library(cowplot)
  10. library(multipanelfigure)
  11. library(ggh4x)
  12. library(svglite)
  13. library(BayesFactor)
  14. setwd("/Users/b1037210/Desktop/Progetti/JA_Stage1/")
  15. df = read.xlsx("final_table_for_R_10-2500_def_o1_rep.xlsx")
  16. df$TMS_timings_rep = as.factor(df$TMS_timings_rep)
  17. levels(df$TMS_timings_rep) = c("Late TMS","Early TMS")
  18. df$TMS_timings_rep <- factor(df$TMS_timings_rep, levels = c("Early TMS","Late TMS"))
  19. df$subject = as.factor(df$subject)
  20. df$CF_colors_order = as.factor(df$CF_colors_order)
  21. levels(df$CF_colors_order) = c("Lift", "Catch", "Press")
  22. df <- subset(df, CF_colors_order == "Press"| CF_colors_order == "Lift" )
  23. df$CF_colors_order = droplevels(df$CF_colors_order)
  24. groups <- group_by(df, CF_colors_order, subject, TMS_timings_rep)
  25. # summarize by using the medians of each subject --> have a table with a "mean" column for each cell for each participant
  26. data_avgs <- summarise(groups,
  27. mean = median(ECD_MEP_amplitudes, na.rm=TRUE))
  28. # take a peek
  29. data_avgs = as.data.frame(data_avgs)
  30. df_final = data_avgs
  31. attach(df_final)
  32. ## descriptive statistics
  33. s_alt = ezStats(data=df_final,
  34. dv = .(mean),
  35. wid = .(subject),
  36. within = .(CF_colors_order, TMS_timings_rep))
  37. ## plot now
  38. pd = position_dodge(width=0.0)
  39. # create basic plot with x axis as Cngruendy and y as mean (which is median actually)
  40. g = ggplot(df_final, aes(x = CF_colors_order, y = mean))
  41. # create Facets, the level of one variable above, the name of the levels of the other on the new line (\n)
  42. g = g+facet_grid(col = vars(interaction(TMS_timings_rep, sep = "\n",lex.order = TRUE)))
  43. # create bar plot
  44. g = g+ geom_bar(aes(fill = CF_colors_order), stat = "summary", fun = "mean")
  45. g = g+ geom_line(aes(group = subject)
  46. , stat = "summary"
  47. , fun = "mean"
  48. ,size= .3
  49. , position = pd)
  50. #new filling colors for the bars --> values = c("red","orange","blue","green"))
  51. g = g+scale_fill_manual(values = c("red","blue","tomato","dodgerblue"))
  52. g = g+theme_classic()
  53. g = g+theme(plot.margin = unit(c(0, 0, 0, 0), "cm")) #top, right, bottom, left
  54. g = g+theme(strip.text.x = element_text(size=20, angle=0, vjust = 0),
  55. strip.background = element_rect(colour="white", fill="white"))
  56. g = g + theme(
  57. axis.title.x = element_text( size = 20, face = "bold", margin = margin(t = 10, r = 0, b = 0, l = 0)),
  58. axis.title.y = element_text(size = 20, face = "bold", margin = margin(t = 0, r = 10, b = 0, l = 0)),
  59. axis.text.x = element_text(size = 15),# family = 'Helvetica',
  60. axis.text.y = element_text(size = 15))
  61. g = g+xlab("Cue instruction") + ylab("peak-to-peak amplitude (µV)") #ylab("TMS-evoked pressure (a.u.)") #
  62. g = g+theme(
  63. legend.position = "none")
  64. file_name = "ECU" #"Pressure (z)"#"ECU(z) - FDS(z)"#"Flexor Digitorum Superficialis (Z-scores)\n"
  65. g = g+ggtitle(file_name)
  66. g = g+theme(plot.title = element_text(hjust = 0.5, size = 35, face = "bold"))
  67. g
  68. ## stats
  69. late_lift = subset(data_avgs, TMS_timings_rep == "Late TMS" & CF_colors_order == "Lift")
  70. late_press = subset(data_avgs, TMS_timings_rep == "Late TMS" & CF_colors_order == "Press")
  71. early_lift = subset(data_avgs, TMS_timings_rep == "Early TMS" & CF_colors_order == "Lift")
  72. early_press = subset(data_avgs, TMS_timings_rep == "Early TMS" & CF_colors_order == "Press")
  73. shapiro.test(late_lift$mean)
  74. shapiro.test(late_press$mean)
  75. shapiro.test(early_lift$mean)
  76. shapiro.test(early_press$mean)
  77. t.test(early_lift$mean, early_press$mean, paired = TRUE, alternative = "greater")
  78. t.test(late_lift$mean, late_press$mean, paired = TRUE, alternative = "greater")
  79. ## save
  80. file_title = paste0(file_name, ".jpg");
  81. ggsave(file_title,g,width = 10, height = 7, device = "jpg")