Practical Worksheet

Package Loading

As mentioned within the session setup, load the following packages using the library() function.


Exercise 1: Using the data provided (ex1.dat), generated by the code below, plot the data onto a scatterplot using ggplot(). Plotting the variable sine from the data onto the x variable, and the index (1:1001) onto the y variable

# Generate the sequence 1 to 100, in steps of 0.1
  ex1.dat <-
    seq(from = 0, 
        to = 100, 
        by = 0.1))

# Apply the sine function
  ex1.dat <- sin(ex1.dat)

# Rename the columns
  colnames(ex1.dat) <- "sine"

Exercise 1, Bonus Question: Rather than using geom_point(), use geom_line() or another geom_ function to plot this same data in another way.

Exercise 2: Add one of the two following themes to clean up your code!

    panel.grid.major = element_blank(),     
    panel.grid.minor = element_blank(),
    panel.background = element_blank(),     
    axis.line = element_blank(), 
    axis.title = element_blank(), 
    axis.text = element_blank(),
    axis.ticks = element_blank()) 


Exercise 3: Combined Mathematical Sequences

Exercise 3a: Using the same data as before (ex1.dat) transform the data and layer it onto the previous plots. Use the geom_line() function to more easily observe the impact of this transformation.

Hint: Use the following as an example to base your transformation on

  geom_line(data = (ex1.dat + 1),
            mapping = aes(x = 1:1001, y = sine))

Exercise 3 Bonus: Repeat this progress building multiple layers

Exercise 4: Changing Coordinate System

Step 1: Create a new data set ex4.dat using the following code

ex4.dat <- = 1,
                                 to = 51.3, 
                                 by = 0.1))
    ex4.dat <- sin(ex4.dat)
    colnames(ex4.dat) <- "sine" 

Step 2: Using the code we have previously used, add the function coord_polar()

Hint: Use “mapping = aes(x = 1:504, y = sine)” for ex4.dat

Step 3: Once again, repeat the transformation adding new layers to this new plot

Exercise 4 Bonus: Return to the sequence generation in step 1, How does this plot change if a function like cos() or tan() is applied rather than tan()

Exercise 5: Layering Colours, using the code created in Exercise 4, replace geom_point() with geom_polygon() and apply a colour within using fill = or colour =.

To view all the colours which can be used: display.brewer.all(), or see: this cheatsheet

Exercise 6: Exploration and Experimentation, using the skills previously known and the other topics discussed in the learning material section, explore and experiment to see what types of patterns you can create. Look at the inspiration section for ideas.