Load packages

library(ggplot2)
library(cowplot)

Read in r2star & group data

r2star <- read.csv("r2star_finalSample_03152017.csv")
r2star$ageAtScan1 <- (r2star$ageAtScan1)/12
groups <- read.csv("n1601_go1_diagnosis_dxpmr_20161014.csv")
r2star_groups <- merge(r2star, groups, by=c("bblid","scanid"))

Male sample

male <- subset(r2star, sex == "1")
maleCount <- nrow(male)
maleAvgAge <- round(mean(male$ageAtScan1), digits = 0)

Female sample

female <- subset(r2star, sex == "2")
femaleCount <- nrow(female)
femaleAvgAge <- round(mean(female$ageAtScan1), digits = 0)

TD sample

r2starTD <- subset(r2star_groups, goassessDxpmr6 == "TD")
tdCount <- nrow(r2starTD)

tdMale <- subset(r2starTD, sex == "1")
tdMaleCount <- nrow(tdMale)
tdMaleAge <- round(mean(tdMale$ageAtScan1), digits = 0)

tdFemale <- subset(r2starTD, sex == "1")
tdFemaleCount <- nrow(tdFemale)
tdFemaleAge <- round(mean(tdFemale$ageAtScan1), digits = 0)

PS sample

r2starPS <- subset(r2star_groups, goassessDxpmr6 == "PS")
psCount <- nrow(r2starPS)

psMale <- subset(r2starPS, sex == "1")
psMaleCount <- nrow(psMale)
tdMaleAge <- round(mean(tdMale$ageAtScan1), digits = 0)

psFemale <- subset(r2starPS, sex == "2")
psFemaleCount <- nrow(psFemale)
psFemaleAge <- round(mean(psFemale$ageAtScan1), digits = 0)

Male & Female Sample Size Plot

count <- data.frame(
  Sex = factor(c("Male","Female"), levels=c("Male","Female")),
  Size = c(maleCount, femaleCount)
)

sex = c("Male", "Female")

number <- c(maleCount,femaleCount)
  
countdf = cbind(count, sex)

fullSamplePlot = ggplot(data=countdf, aes(x=Sex, y=Size, fill=sex)) + 
    geom_bar(stat="identity",width=0.5) + 
    guides(fill=FALSE) +
    xlab("Sex") + ylab("Size") +
    ggtitle("Full Sample") +
    geom_text(aes(label=number), vjust=1.6, color="white", size=3)+
  theme_minimal()

plot(fullSamplePlot)

Male & Female Age Plot

avgAge <- data.frame(
  Sex = factor(c("Male","Female"), levels=c("Male","Female")),
  Size = c(maleAvgAge, femaleAvgAge)
)

sex = c("Male", "Female")

avg <- c(maleAvgAge,femaleAvgAge)
  
avgAgedf = cbind(avgAge, sex)

agePlot = ggplot(data=avgAgedf, aes(x=Sex, y=Size, fill=sex)) + 
    geom_bar(stat="identity",width=0.5) + 
    guides(fill=FALSE) +
    xlab("Sex") + ylab("Average Age (years)") +
    ggtitle("Full Sample: Average Age") +
    geom_text(aes(label=avg), vjust=1.6, color="white", size=3)+
  theme_minimal()

plot(agePlot)

TD & PS Sample Size

td_ps <- data.frame(
    Sex = factor(c("Male","Female","Male","Female")),
    Dx = factor(c("TD","TD","PS","PS"), levels=c("TD","PS")),
    Size = c(tdMaleCount, tdFemaleCount, psMaleCount, psFemaleCount)
)

number_td_ps = c(tdMaleCount, tdFemaleCount, psMaleCount, psFemaleCount)

td_ps_plot = ggplot(data=td_ps, aes(x=Dx, y=Size, fill=Sex)) +
    geom_bar(stat="identity", position = 'dodge') +
    ggtitle("TD & PS Sample") +
    geom_text(aes(label=number_td_ps), position=position_dodge(width=0.9), vjust=2, color="white", size=3) +
  theme_minimal()

plot(td_ps_plot)

Arrange Plots

plot_grid(fullSamplePlot, agePlot, td_ps_plot, labels = c("A","C","B"))