Flipped Classroom Exercises

What is a flipped classroom?

“A flipped classroom is an instructional strategy and a type of blended learning, which aims to increase student engagement and learning by having students complete readings at their home and work on live problem-solving during class time .” Source: https://en.wikipedia.org/wiki/Flipped_classroom

Learning objectives

  1. Create data graphics using the ggplot2 package

  2. Identify suitable aesthetics and geoms to create plots using qplot and ggplot

  3. Identify what faceting is and apply faceting in ggplot

  4. Customize data graphics using ggplot

Learning materials

Check the video lectures in Google Classroom.

Data set

This question is based on mpg dataset in the ggplot2 package. Load the ggplot2 package and mpg dataset using the following command.

library(ggplot2)
data(mpg)

Fuel economy data from 1999 to 2008 for 38 popular models of cars.

This dataset contains a subset of the fuel economy data that the EPA makes available on https://fueleconomy.gov/. It contains only models which had a new release every year between 1999 and 2008 - this was used as a proxy for the popularity of the car. To find out more information about the dataset you can run the command

help(mpg) # or ?mpg

You can observe the data structure using the following command

str(mpg)
tibble [234 × 11] (S3: tbl_df/tbl/data.frame)
 $ manufacturer: chr [1:234] "audi" "audi" "audi" "audi" ...
 $ model       : chr [1:234] "a4" "a4" "a4" "a4" ...
 $ displ       : num [1:234] 1.8 1.8 2 2 2.8 2.8 3.1 1.8 1.8 2 ...
 $ year        : int [1:234] 1999 1999 2008 2008 1999 1999 2008 1999 1999 2008 ...
 $ cyl         : int [1:234] 4 4 4 4 6 6 6 4 4 4 ...
 $ trans       : chr [1:234] "auto(l5)" "manual(m5)" "manual(m6)" "auto(av)" ...
 $ drv         : chr [1:234] "f" "f" "f" "f" ...
 $ cty         : int [1:234] 18 21 20 21 16 18 18 18 16 20 ...
 $ hwy         : int [1:234] 29 29 31 30 26 26 27 26 25 28 ...
 $ fl          : chr [1:234] "p" "p" "p" "p" ...
 $ class       : chr [1:234] "compact" "compact" "compact" "compact" ...

To get a summary use the command

summary(mpg)
 manufacturer          model               displ            year     
 Length:234         Length:234         Min.   :1.600   Min.   :1999  
 Class :character   Class :character   1st Qu.:2.400   1st Qu.:1999  
 Mode  :character   Mode  :character   Median :3.300   Median :2004  
                                       Mean   :3.472   Mean   :2004  
                                       3rd Qu.:4.600   3rd Qu.:2008  
                                       Max.   :7.000   Max.   :2008  
      cyl           trans               drv                 cty       
 Min.   :4.000   Length:234         Length:234         Min.   : 9.00  
 1st Qu.:4.000   Class :character   Class :character   1st Qu.:14.00  
 Median :6.000   Mode  :character   Mode  :character   Median :17.00  
 Mean   :5.889                                         Mean   :16.86  
 3rd Qu.:8.000                                         3rd Qu.:19.00  
 Max.   :8.000                                         Max.   :35.00  
      hwy             fl               class          
 Min.   :12.00   Length:234         Length:234        
 1st Qu.:18.00   Class :character   Class :character  
 Median :24.00   Mode  :character   Mode  :character  
 Mean   :23.44                                        
 3rd Qu.:27.00                                        
 Max.   :44.00                                        

Exercise

Write R codes to reproduce the following plots using both the qplot function and the ggplot function in the ggplot2 package. The first one is done for you.

Help:

  1. Identify the x variable, y variable, color variable.

  2. Identify the geom type: point, hist, smooth, density

  3. Identify the attribute: color, shape, etc.

Plot 1

qplot command:

qplot(displ, hwy, data=mpg, geom="point")

ggplot command:

## method 1
ggplot(data=mpg aes(x=displ, y=hwy)) + geom_point()
## method 2
ggplot(data=mpg) + geom_point(aes(x=displ, y=hwy))

Plot 2

qplot command:



ggplot command:



Plot 3

qplot command:



ggplot command:



Plot 4

qplot command:



ggplot command:



Plot 5

qplot command:



ggplot command:



Plot 6

qplot command:



ggplot command:



Plot 6

qplot command:



ggplot command:



Plot 7

qplot command:



ggplot command:



Plot 8

qplot command:



ggplot command:



Plot 9

qplot command:



ggplot command:



Plot 10

ggplot command only: