Hand-on Exercise 1

Author

Chenbin Zhang

Getting Started

Install and launching R packages

The code chunk below uses p_load() of pacman package to check if tidyverse package are installed in the compute. if the are, then they will be lauched into R.

pacman::p_load(tidyverse)

Importing the data

exam_data <- read_csv("data/Exam_data.csv")
Warning: One or more parsing issues, call `problems()` on your data frame for details,
e.g.:
  dat <- vroom(...)
  problems(dat)
Rows: 650 Columns: 7
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (7): ID, CLASS, GENDER, RACE, ENGLISH, MATHS, SCIENCE

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Plotting a simple bar chart

R Graphics VS ggplot

exam_data$MATHS <- as.numeric(as.character(exam_data$MATHS))
Warning: NAs introduced by coercion
ggplot(data=exam_data, aes(x = MATHS)) +
  geom_histogram(bins=10, 
                 boundary = 100,
                 color="black", 
                 fill="grey") +
  ggtitle("Distribution of Maths scores")
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_bin()`).

Essential Grammatical Elements in ggplot2: Aesthetic mappings

ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot(dotsize = 0.5)
Bin width defaults to 1/30 of the range of the data. Pick better value with
`binwidth`.
Warning: Removed 6 rows containing missing values or values outside the scale range
(`stat_bindot()`).

ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_dotplot(binwidth=2.5,         
               dotsize = 0.5) +      
  scale_y_continuous(NULL,           
                     breaks = NULL)  
Warning: Removed 6 rows containing missing values or values outside the scale range
(`stat_bindot()`).

ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_histogram()       
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_bin()`).

ggplot(data=exam_data, 
       aes(x= MATHS)) +
  geom_histogram(bins=20,            
                 color="black",      
                 fill="light blue")  
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_bin()`).

ggplot(data=exam_data, 
       aes(x= MATHS, 
           fill = GENDER)) +
  geom_histogram(bins=20, 
                 color="grey30")
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_bin()`).

ggplot(data=exam_data, 
       aes(x = MATHS)) +
  geom_density()           
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_density()`).

ggplot(data=exam_data, 
       aes(x = MATHS, 
           colour = GENDER)) +
  geom_density()
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_density()`).

ggplot(data=exam_data, 
       aes(y = MATHS,       
           x= GENDER)) +    
  geom_boxplot()            
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_boxplot()`).

ggplot(data=exam_data, 
       aes(y = MATHS, 
           x= GENDER)) +
  geom_boxplot(notch=TRUE)
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_boxplot()`).

ggplot(data=exam_data, 
       aes(y = MATHS, 
           x= GENDER)) +
  geom_violin()
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_ydensity()`).

ggplot(data=exam_data, 
       aes(x= MATHS, 
           y=ENGLISH)) +
  geom_point()            
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).

ggplot(data=exam_data, 
       aes(y = MATHS, 
           x= GENDER)) +
  geom_boxplot() +                    
  geom_point(position="jitter", 
             size = 0.5)        
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Warning: Removed 6 rows containing missing values or values outside the scale range
(`geom_point()`).

Essential Grammatical Elements in ggplot2: stat

ggplot(data=exam_data, 
       aes(y = MATHS, x= GENDER)) +
  geom_boxplot() +
  stat_summary(geom = "point",       
               fun.y="mean",         
               colour ="red",        
               size=4)               
Warning: The `fun.y` argument of `stat_summary()` is deprecated as of ggplot2 3.3.0.
ℹ Please use the `fun` argument instead.
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_boxplot()`).
Warning: Removed 6 rows containing non-finite outside the scale range
(`stat_summary()`).

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'

exam_data <- na.omit(exam_data)

ggplot(data=exam_data, aes(x=MATHS, y=ENGLISH)) +
  geom_point() +
  geom_smooth(method="lm", size=0.5) +
  ggtitle("Relationship between Maths and English scores")
`geom_smooth()` using formula = 'y ~ x'

Essential Grammatical Elements in ggplot2: Facets

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

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

Essential Grammatical Elements in ggplot2: Coordinates

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

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'

Essential Grammatical Elements in ggplot2: themes

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

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