CSC 120, Lab Exercise 4.

In this lab exercise, you will write a plot_dots function that plots points at locations specified by a list of numeric vectors, plus two functions, called triangle and checkerboard, that produce such lists of vectors that can be passed to plot_dots.

The plot_dots function takes as arguments a list giving point locations, the horizontal extent of the region where dots are plotted, and the vertical extent of the region where dots are plotted. Its definition should begin with

plot_dots <- function (dots, horizontal, vertical)

It should create an emply plot with xlim set to c(0,horizontal+1) and ylim set to c(0,vertical+1), and then add points specified by the dots argument. The dots argument should be a list of numeric vectors. The first numeric vector in this list (which can be gotten by dots[[1]]) will contain the y coordinates of points that should be plotted at x coordinate 1, the second vector in dots will contain the y coordinates of points to be plotted with x coordiante 2, etc.

The triangle function should take a single integer, n, as its argument, and return a list with n elements, the first being the vector 1:1, the second 1:2, and so forth up to the last element being 1:n.

The checkerboard function should take a single integer, n, and return a list of numeric vectors that when passed to plot_dots will plot a checkerboard pattern of dots on an n by n square (ie, one that has a dot at (i,j) if i+j is an even number).

These functions should be put in a file called dots.R, which can be read with the source function to make them defined.

This file that you are reading now is the output, produced with knitr::spin, of an R script that reads the function definions from dots.R and then uses them to make three plots.

> source("dots.R")
> 
> # Test of plot_dots with a small list created manually.
> 
> plot_dots (list (c(2,5,1), c(3), c(), c(4,2)), 4, 5)

plot of chunk unnamed-chunk-2

> # Test of plot_dots with the triangle function.
> 
> plot_dots (triangle(7), 7, 7)

plot of chunk unnamed-chunk-2

> # Test of plot_dots with the checkerboard function.
> 
> plot_dots (checkerboard(15), 15, 15)

plot of chunk unnamed-chunk-2