Load CSV

exam_data <- read_csv("data/Exam_data.csv")

Working with aes()

ggplot(data=exam_data, aes(x=MATHS))

ggplot geom_bar

ggplot(data=exam_data, aes(x=RACE))+
  geom_bar()

ggplot geom_histogram

ggplot(data=exam_data, aes(x=MATHS))+
  geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

ggplot geom_histogram with additional configuration

ggplot(data=exam_data, aes(x=MATHS))+
  geom_histogram(bins=20,
                 color="black",
                 fill="lightblue")

In-class exercise 1

ggplot(data=exam_data, aes(x=MATHS,
                           fill=GENDER))+
  geom_histogram(bins=20,
                 color="grey30")+
  scale_fill_manual(values=c("grey","orange"))

ggplot geo_dotplot

ggplot(data=exam_data, aes(x=MATHS))+
  geom_dotplot(dotsize = 0.5)+
  scale_y_continuous(NULL, breaks = NULL) #turn off the y-axis
## `stat_bindot()` using `bins = 30`. Pick better value with `binwidth`.

ggplot geom_density

ggplot(data=exam_data, aes(x=MATHS))+
  geom_density()

ggplot geom_boxplot

ggplot(data=exam_data, aes(y=MATHS,
                           x=GENDER))+
  geom_boxplot() +
  geom_point(position="jitter",
             size=0.5)

ggplot geom_violin

ggplot(data=exam_data,
       aes(y=MATHS, x=GENDER))+
  geom_violin()

ggplot geom_point

ggplot(data=exam_data,
       aes(x=MATHS, y=ENGLISH))+
  geom_point()

ggplot geom_boxplot with stat_summary

ggplot(data=exam_data,
       aes(y=MATHS, x=GENDER))+
  geom_boxplot()+
  stat_summary(geom="point",
               fun.y="mean",
               colour="red",
               size=4)
## Warning: `fun.y` is deprecated. Use `fun` instead.

ggplot geom_boxplot with geom_point

ggplot(data=exam_data,
       aes(y=MATHS, x=GENDER))+
  geom_boxplot()+
  geom_point(stat="summary",
             color="red",
             size=4)
## No summary function supplied, defaulting to `mean_se()`

ggplot geom_point with geom_smooth, smoothing method is loess by default

ggplot(data=exam_data,
       aes(x=MATHS, y=ENGLISH))+
  geom_point()+
  geom_smooth(size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot geom_point with geom_smooth, smoothing method is lm

ggplot(data=exam_data,
       aes(x=MATHS, y=ENGLISH))+
  geom_point()+
  geom_smooth(method=lm,
              size=0.5)
## `geom_smooth()` using formula 'y ~ x'

ggplot stat summary on bar and erorrbar

ggplot(data=exam_data,
       aes(x=RACE, y=MATHS))+
  stat_summary(geom="bar",
               fun.y="mean",
               fill="lightblue")+
  stat_summary(geom="errorbar",
               color="black",
               width=0.5)
## Warning: `fun.y` is deprecated. Use `fun` instead.
## No summary function supplied, defaulting to `mean_se()`

ggplot facets_wrap

ggplot(data=exam_data,
       aes(x=MATHS))+
  geom_histogram(bins=20)+
  facet_wrap(~CLASS)

ggplot facets_grid

ggplot(data=exam_data,
       aes(x=MATHS))+
  geom_histogram(bins=20)+
  facet_grid(~CLASS)

In-class exercise 2

ggplot(data=exam_data,
       aes(x=CLASS, y=MATHS))+
  geom_boxplot()+
  facet_grid(~GENDER)

In-class exercise 3

ggplot(data=exam_data,
       aes(x=CLASS, y=MATHS))+
  geom_boxplot()+
  facet_grid(rows=vars(GENDER))

In-class exercise 4

ggplot(data=exam_data,
       aes(x=GENDER, y=MATHS))+
  geom_boxplot()+
  facet_grid(GENDER ~ CLASS)+
  theme(axis.text.x = element_text(size=6))

ggplot geom_bar with coord_fip

ggplot(data=exam_data, aes(x=RACE))+
  geom_bar()+
  coord_flip()

ggplot geom_point with geom_smooth, smoothing method is lm

ggplot(data=exam_data,
       aes(x=MATHS, y=ENGLISH))+
  geom_point()+
  geom_smooth(method=lm,
              size=0.5)+
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))
## `geom_smooth()` using formula 'y ~ x'

ggplot geom_bar with coord_fip and theme classic

ggplot(data=exam_data, aes(x=RACE))+
  geom_bar()+
  coord_flip()+
  theme_classic()

ggplot geom_bar with coord_fip and theme minimal

ggplot(data=exam_data, aes(x=RACE))+
  geom_bar()+
  coord_flip()+
  theme_minimal()

ggplot geom_bar with coord_fip and custom theme

ggplot(data=exam_data, aes(x=RACE))+
  geom_bar()+
  coord_flip()+
  theme(panel.background = element_rect(fill = "lightblue"),
        panel.grid.major = element_line(size = 0.5,
                                        linetype = "solid",
                                        color = "white"),
        panel.grid.minor = element_line(size = 0,25,
                                        linetype = "dashed",
                                        color = "white"),
        axis.text = element_text(colour = "red",
                                 size = rel(0.8)),
        title = element_text(colour = "grey20",
                             size = rel(1)))

ggplot stat_summary with reorder on x-axis

ggplot(data=exam_data, aes(x=reorder(RACE, -MATHS), y=MATHS))+
  stat_summary(geom="bar",
               fun.y="mean",
               fill="lightblue")
## Warning: `fun.y` is deprecated. Use `fun` instead.

ggplot geom_histogram with geom_vline on mean and median

ggplot(data=exam_data, aes(x=MATHS))+
  geom_histogram(bins=20,
                 color="black",
                 fill="lightblue")+
  geom_vline(aes(xintercept = mean(MATHS)), col="red", size=1)+
  geom_vline(aes(xintercept = median(MATHS)), col="blue", size=1)

In-class exercise 5

ggplot(data=exam_data,
       aes(x=ENGLISH, fill=GENDER))+
  geom_histogram(data=exam_data['ENGLISH'],
                 bins=20,
                 fill="grey",
                 alpha=0.5)+
  geom_histogram(bins=20,
                 color="black")+
  facet_grid(~GENDER)

In-class exercise 6

ggplot(data=exam_data,
       aes(x=MATHS, y=ENGLISH))+
  geom_point()+
  geom_vline(aes(xintercept = 50), linetype="longdash", size=1)+
  geom_hline(aes(yintercept = 50), linetype="longdash", size=1)+
  coord_cartesian(xlim=c(0,100),
                  ylim=c(0,100))