R for Everyone
  1. Plots
  2. 15  Plots exercises
  • Start
    • 1  Introduction
  • Foundations & Objects
    • 2  Foundations of R
    • 3  Foundations of R exercises
    • 4  R objects
    • 5  R objects exercises
    • 6  R objects solutions
  • Files, Subsetting, & Tips
    • 7  Files and subsetting data
    • 8  Files exercises
    • 9  Files solutions
    • 10  Handy tips & tricks
    • 11  Handy exercises
    • 12  Handy Solutions
  • Plots
    • 13  Histograms and Line graphs
    • 14  Scatter plots and Box plot
    • 15  Plots exercises
    • 16  Plots solutions
  • Stats & Programming
    • 17  Basic stats
    • 18  Stats exercises
    • 19  Stats solutions
    • 20  Basic programming
    • 21  Student grades example
  • Appendices
    • A  Further resources

In this chapter

  • 15.1 Line graph
  • 15.2 Histogram
  • 15.3 Scatterplot
  • 15.4 Boxplot

15  Plots exercises

These exercises will primarily use the “penguin_df” from the theory and practice session.

Please use your “Exercises.R” script for this exercise and the main workshop directory as the working directory. Ensure you are using annotations and code sections to keep the contents clear and separated.

For convenience the code to load the Penguin data frame directory will be:

penguin_df <- read.csv("Chapter_13-16/penguin.tsv", 
                      check.names = FALSE,
                      stringsAsFactors = TRUE,
                      sep = "\t"
                      )

Your plots might not look exactly the same. As long as your plot contains the same data it is good. If you are not sure if your plot is correct, feel free to look at the solutions to look at my code.

15.1 Line graph

First we will ignore “penguin_df”.

Create the below plot. You will need to create the vectors yourself.

Tip: The x and y labels may help you figure out the commands to create the vectors.

15.2 Histogram

From the “penguin_df” create the below plot:

Note: The colour of the bars are “blue”.

15.3 Scatterplot

From the “penguin_df” create the below plot:

Note: You can check what numbers refer to what shape for pch = at: http://www.sthda.com/english/wiki/r-plot-pch-symbols-the-different-point-shapes-available-in-r

Before you continue save this plot. Details below:

  • Save the file as a png called “Penguins_2009_bill_depth_vs_length_scatterplot.png”
  • Save the plot with a width and height of 250 and resolution of 150

15.4 Boxplot

From the “penguin_df” create the below plot:

Note: Make sure the x axis is in the same order as below.

For the last task, save this plot. Details below:

  • Save the file as a jpg called “Penguins_species_and_flipper_length_boxplot.jpg” in “Chapter_13-16”.
  • Save the plot with a width of 300, height of 250, and resolution of 150
14  Scatter plots and Box plot
16  Plots solutions