Key topics

Key R Functions

Readings

These questions cover materials in:

Sections 6.2 - 6.4 are reference guides for plotting techniques in R.

You should focus on tables 6.1, 6.2, 6.4 - 6.6

Data File

You’ll need to download the file butterfly_table.csv from the course GitHub page.

NOTES

  • You’ll be using this file to complete the Have a Go: Use R for multiple series bar charts exercise in Chapter 6.
  • The book lists the filename as butterfly table.csv, with a space between butterfly and table.
  • Our version of the file uses an underscore character instead of a space.
  • You’ll need to make sure you type the filename with an underscore (instead of a space) when you use read.csv() to read the data.

Penguin Data

We’ll be using a dataset consisting of various measurements of three species of penguins.

To use the dataset, you’ll need to use install.packages() to install the palmerpenguins package:

install.packages("palmerpenguins")

After you’ve installed it, you can load it using either library() or require().

Once you’ve successfully installed/loaded the penguins data, you should be able to run the following code on your machine to produce a summary of the first 4 columns of the dataset:

summary(penguins[, 1:4])
##       species          island    bill_length_mm  bill_depth_mm  
##  Adelie   :152   Biscoe   :168   Min.   :32.10   Min.   :13.10  
##  Chinstrap: 68   Dream    :124   1st Qu.:39.23   1st Qu.:15.60  
##  Gentoo   :124   Torgersen: 52   Median :44.45   Median :17.30  
##                                  Mean   :43.92   Mean   :17.15  
##                                  3rd Qu.:48.50   3rd Qu.:18.70  
##                                  Max.   :59.60   Max.   :21.50  
##                                  NA's   :2       NA's   :2

Questions

Question 1

Instructions

Review the of hist() in R to produce histograms.

  1. Load the palmerpenguins dataset.
  2. Create a histogram plot of penguin flipper length.
  3. Give your histogram a descriptive title and x-axis label.

Hints

You must create a histogram of flipper length.

To get started, you can use my template code for a histogram of body mass:

hist(penguins$body_mass_g)

Notice the ugly title and x-axis label. Your histogram must have a better title and x-axis label than mine!

Histogram: 2 pts, file upload

  • Save your flipper length histogram to an image file (.png).
  • Locate the file on your computer.
  • Insert the image into the text input box on Moodle.

Question 2

Instructions

Review the section on boxplots (6.3.2) in Gardener.

  1. Load the palmerpenguins dataset.
  2. Create a boxplot of penguin flipper length grouped by penguin sex.
  3. Your plot needs to have:
  • Descriptive title
  • Appropriate x-axis label

Hints

You’ll need to create boxplot of flipper length grouped by sex.

I created the following simple boxplot of penguin body mass grouped by sex using code very similar to the code in your textbook on page 146.

See if you can modify Gardener’s code to create your boxplot of of penguin flipper length grouped by sex.

Note how Gardener’s code uses the formula notation and the data argument.

Boxplot: 3 pts

  • Save your flipper length/sex boxplot to an image file (.png).
  • Locate the file on your computer.
  • Insert the image into the text input box on Moodle.

Question 3

Warning

The code in the Gardener book will not work as as it appears in the text!

In order to follow along with the book code, you’ll have to use the following two parameters in your call to read.csv():

  • check.names = FALSE
  • row.names = 1

Gardener explains the check.names parameter, but he does not mention the row.names parameter.

Instructions

  1. Download the butterfly_table.csv file from the course GitHub site and save it in the data subdirectory of your main course folder.

  2. Using read.csv() read the data into a data.frame object called butterfly in R. Make sure you read the warning above.

  3. Follow along with the code in the Have a Go: Use R for multiple series bar charts at the end of section 6.3.1 of Gardener chapter 6 to create a barplot.

  4. Adjust the code from step 8 to include a vector of six custom colors.

  • You should use the code from steps 8 and 9 as a template for your plot.
  • Your colors have to be different than the code in step 8 and they cannot be the same colors as Gardener used in mycols from step 9.
  1. Save your plot to an image file and drag-n-drop it into the file upload box.

Hints

  • Check out the R help entry for terrain.colors() for some other color ideas.
  • Take a look at the R Colors cheatsheet for a list of color names you can use in R.

Butterfly Barplot (3 pts - file upload)

  • Save your butterfly barplot to an image file (.png), locate the file on your computer, and drag it into the file upload box.

Submit to Moodle

These questions are for your reference, the same questions will appear in the assignment page on Moodle.

You should do your work outside of Moodle, saving your answers in a document and/or R script file.

When you are ready to submit your answers, you can paste your complete responses in the corresponding Moodle question entries for the assignment.