Chapter 5 ggplot2 HW (week 4)

5.0.1 Instruction

  1. Create your own new R markdown file in your RStudio (and remove unnecessary parts if needed)
  2. Create a code chunk and Write your own code to replicate the plot in each problem
  3. Knit your R markdown to Microsoft Word
  4. Submit the word file to 턴잇인(Turnitin) for HW3 in Week4 section.

5.0.2 Sample problem

Using the mpg dataset in the ggplot2 package, replicate the plot below using the following settings:

  • Set alpha = 0.5 for the width of bars
  • Use theme_minimal()
# you need `ggplot2` package to use `mpg` dataset
library(ggplot2)
mpg
## # A tibble: 234 x 11
##    manufacturer model    displ  year   cyl trans   drv     cty   hwy fl    class
##    <chr>        <chr>    <dbl> <int> <int> <chr>   <chr> <int> <int> <chr> <chr>
##  1 audi         a4         1.8  1999     4 auto(l~ f        18    29 p     comp~
##  2 audi         a4         1.8  1999     4 manual~ f        21    29 p     comp~
##  3 audi         a4         2    2008     4 manual~ f        20    31 p     comp~
##  4 audi         a4         2    2008     4 auto(a~ f        21    30 p     comp~
##  5 audi         a4         2.8  1999     6 auto(l~ f        16    26 p     comp~
##  6 audi         a4         2.8  1999     6 manual~ f        18    26 p     comp~
##  7 audi         a4         3.1  2008     6 auto(a~ f        18    27 p     comp~
##  8 audi         a4 quat~   1.8  1999     4 manual~ 4        18    26 p     comp~
##  9 audi         a4 quat~   1.8  1999     4 auto(l~ 4        16    25 p     comp~
## 10 audi         a4 quat~   2    2008     4 manual~ 4        20    28 p     comp~
## # ... with 224 more rows
ggplot(mpg, aes(hwy)) + 
  geom_histogram(aes(fill=drv), alpha = 0.5) + 
  theme_minimal() + 
  labs(title = "Histogram", 
       subtitle="Histogram of Highway Mile Per Gallon", 
       caption = "Source: mpg")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

5.0.3 Problem 1

Using the mpg dataset in the ggplot2 package, replicate the plot below using the following settings:

  • Set alpha = 0.5 for the width of bars in histogram
  • Use facet_grid()
  • Use theme_minimal()
# you need `ggplot2` package to use `mpg` dataset
library(ggplot2)

5.0.4 Problem 2

Using the midwest dataset in the ggplot2 package, replicate the plot below using the following settings:

  • Map x=area and y=poptotal
  • Set alpha = 0.4
  • Set the limit of x-axis is c(0, 0.1)
  • Set the limit of y-axis is c(0, 500000)
  • Use se=FALSE option within geom_smooth() to remove confidence bands
  • Use theme_classic()
library(ggplot2) # you need `ggplot2` to use `midwest` data
options(scipen=999)  # turn-off scientific notation like 1e+48

5.0.5 Problem 3

Using the iris dataset in the datasets package (dataset package belongs to Base R and so you don’t need to download the package), replicate the plot below using the following settings:

  • Set size = 6 for the size of points
  • Set alpha = 0.5
  • Use theme_minimal()

(iris is another famous dataset in R. You may google or check the this link to learn more about the dataset)

5.0.6 Problem 4

Using the heightweight dataset in the gcookbook package, replicate the plot below using the following settings:

  • Set size = 3 of points
  • Set alpha = 0.5
  • Use theme_classic()
library(gcookbook) # you need `gcookbook` to use `heightweight` data

5.0.7 Problem 5

Using the mpg dataset in the ggplot2 package, replicate the plot below using the following settings:

  • Set width = 0.5 for the width of bars
  • Rotate tick labels in the x-axis by 65 degree
  • Use palette = "Spectral" for color
  • Use theme_minimal()
library(ggplot2)

5.0.8 Problem 6

Using the cabbage_exp dataset in the gcookbook package, replicate the plot below using the following settings:

  • You need geom_text(aes(label = Weight), colour = "white", size = 4, vjust = 1.5, position = position_dodge(.9)) to put text labels.
  • Use theme_minimal()

5.0.9 Problem 7

Using the gapminder dataset in the gapminder package, replicate the plot below using the following settings:

  • Set alpha = 0.3 to adjust the transparency of lines
  • Use theme_classic()
library(gapminder)