Clean up data

# Clean up data 
subsetdata <- ageDataTable[, c("Age", "Task", "Score")]
subsetdata$Task <- factor(subsetdata$Task, levels = c("Overall", "Memory Task", "Sorting Task"))

avg_accuracy <- ageDataTable %>%
  group_by(Age, Task) %>%
  summarize(Average_Score = mean(Score, na.rm = TRUE), nAcc=n(), .groups = "drop")

avg_accuracy$Task = factor(avg_accuracy$Task, levels=c("Overall","Memory Task","Sorting Task"))

age_counts <- ageDataTable %>%
  group_by(Age) %>%
  summarize(N = n())

overall_data <- subset(avg_accuracy, Task == "Overall")
memory_data <- subset(avg_accuracy, Task == "Memory Task")
sorting_data <- subset(avg_accuracy, Task == "Sorting Task")

overall_data <- merge(overall_data, age_counts)
memory_data <- merge(memory_data, age_counts)
sorting_data <- merge(sorting_data, age_counts)

all_data <- rbind(overall_data, memory_data, sorting_data)

# Specify the desired order of the facets
facet_order <- c("Overall", "Memory Task", "Sorting Task")

# Convert "Task" column to an ordered factor
all_data$Task <- factor(all_data$Task, levels = c("Overall", "Memory Task", "Sorting Task"))

Plot

fig_8 <- ggplot(avg_accuracy, aes(x = Age, y = Average_Score, size = nAcc, colour = nAcc, fill = nAcc)) +
  geom_point(shape = 21, color = "black", stroke = 0.5) +
  facet_grid(cols=vars(Task)) +
  labs(
    x = "Participant Age",
    y = "Percent Correct",
    size = "N",
    colour = "N",
    fill = "N",
    title = "Fig 8. Average accuracy for each participant age on the UNSW Face Test",
    caption = "Note: Size and shade of each data point show the number of participants in that age group"
  ) +
  theme(
    panel.background = element_blank(),
    panel.grid = element_blank(),
    axis.line = element_line(color = "black"),
    strip.background = element_blank(),
    strip.text = element_text(color = "black", size = 14, face = "bold"),
    legend.position = "right",
    legend.key=element_rect(fill='white'),
    legend.title = element_text(face = "bold", hjust = 0.5),
    plot.title = element_text(hjust = 0.5),
    plot.margin = margin(b = 50),
    axis.text = element_text(face = "bold"),
    axis.title = element_text(face = "bold"),
    plot.caption = element_text(hjust = 0)  # Align the caption to the right
  ) +
  scale_fill_continuous(trans="log10")+
  scale_size_continuous(trans="log10",range=c(1,3))+
  scale_x_continuous(limits = c(10, 80), breaks = seq(10,80,by=20)) +
  scale_y_continuous(breaks = seq(56, 70,by=2))+
  guides(fill=guide_legend(),size=guide_legend())

fig_8