# R for Psychological Research, Lesson 8: Charts: one variable
# 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")))


# ---- The recipe ----

ggplot(students, aes(x = ExamScore))

ggplot(students, aes(x = ExamScore)) +
  geom_histogram()


# ---- Histograms: one number ----

ggplot(students, aes(x = ExamScore)) +
  geom_histogram(binwidth = 5, fill = "steelblue", colour = "white")

ggplot(students, aes(x = ExamScore, fill = Gender)) +
  geom_histogram(binwidth = 5, colour = "white") +
  facet_wrap(~ Gender)


# ---- Bar charts: categories ----

ggplot(students, aes(x = Motivation)) +
  geom_bar()

ggplot(students, aes(x = Motivation, fill = Gender)) +
  geom_bar(position = "dodge")
