# R for Psychological Research, Lesson 9: Charts: compare & relate
# Code from the lesson page, in order. Run it line by line with Ctrl+Enter (Cmd+Enter on a Mac).
# Lines that start with # are comments: R ignores them.


# ---- Set up ----

library(tidyverse)
students <- read_csv("data/students.csv", show_col_types = FALSE) %>%
  mutate(Motivation = factor(Motivation, levels = c("Low", "Medium", "High")))


# ---- Boxplots: compare groups ----

ggplot(students, aes(x = Motivation, y = ExamScore)) +
  geom_boxplot()

ggplot(students, aes(x = Motivation, y = ExamScore)) +
  geom_boxplot() +
  geom_jitter(width = 0.15, alpha = 0.4)


# ---- Scatter plots: two numbers ----

ggplot(students, aes(x = StudyHours, y = ExamScore)) +
  geom_point(alpha = 0.6)

ggplot(students, aes(x = StudyHours, y = ExamScore)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm")


# ---- Make it report-ready ----

ggplot(students, aes(x = StudyHours, y = ExamScore)) +
  geom_point(alpha = 0.6, colour = "steelblue") +
  geom_smooth(method = "lm", colour = "black") +
  labs(title = "Study time and exam score",
       x = "Study hours per week",
       y = "Exam score (out of 100)") +
  theme_classic()

# (Shown for reference. Remove the # to run it.)
# ggsave("study-exam.png", width = 6, height = 4, dpi = 300)
