Name:

Andrew ID:

Collaborated with:

This lab is to be done in class (completed outside of class if need be). You can collaborate with your classmates, but you must identify their names above, and you must submit **your own** lab as an knitted HTML file on Canvas, by Thursday 10pm, this week.

**Important note**: *this assignment is to be completed using base R graphics. That means, e.g., no ggplot commands are allowed!*

**This week’s agenda**: getting familiar with basic plotting tools; understanding the way layers work; recalling basic text manipulations; producing histograms and overlaid histograms.

**1a.**Below is some code that is very similar to that from the lecture, but with one key difference. Explain: why does the`plot()`

result with with`type="p"`

look normal, but the`plot()`

result with`type="l"`

look abnormal, having crossing lines? Then: modify the code below (hint: modify the definition of`x`

), so that the lines on the second plot do not cross.

```
n = 50
set.seed(0)
x = runif(n, min=-2, max=2)
y = x^3 + rnorm(n)
plot(x, y, type="p")
```

`plot(x, y, type="l")`

**1b.**The`cex`

argument can used to shrink or expand the size of the points that are drawn. Its default value is 1 (no shrinking or expansion). Values between 0 and 1 will shrink points, and values larger than 1 will expand points. Plot`y`

versus`x`

, first with`cex`

equal to 0.5 and then 2 (so, two separate plots). Give titles “Shrunken points”, and “Expanded points”, to the plots, respectively.**1c.**The`xlim`

and`ylim`

arugments can be used to change the limits on the x-axis and y-axis, repsectively. Each argument takes a vector of length 2, as in`xlim = c(-1, 0)`

, to set the x limit to be from -1 to 0. Plot`y`

versus`x`

, with the x limit set to be from -1 to 1, and the y limit set to be from -5 to 5. Assign x and y labels “Trimmed x” and “Trimmed y”, respectively.**1d.**Again plot`y`

versus`x`

, only showing points whose x values are between -1 and 1. But this time, define`x.trimmed`

to be the subset of`x`

between -1 and 1, and define`y.trimmed`

to be the corresponding subset of`y`

. Then plot`y.trimmed`

versus`x.trimmed`

without setting`xlim`

and`ylim`

: now you should see that the y limit is (automatically) set as “tight” as possible. Hint: use logical indexing to define`x.trimmed`

,`y.trimmed`

.**1e.**The`pch`

argument, recall, controls the point type in the display. In the lecture examples, we set it to a single number. But it can also be a vector of numbers, with one entry per point in the plot. So, e.g.,`plot(1:10, 1:10, pch=1:10)`

displays the first 10 point types. If

`pch`

is a vector whose length is shorter than the total number of points to be plotted, then its entries are recycled, as appropriate. Plot`y`

versus`x`

, with the point type alternating in between an empty circle and a filled circle.**1f.**The`col`

argument, recall, controls the color the points in the display. It operates similar to`pch`

, in the sense that it can be a vector, and if the length of this vector is shorter than the total number of points, then it is recycled appropriately. Plot`y`

versus`x`

, and repeat the following pattern for the displayed points: a black empty circle, a blue filled circle, a black empty circle, a red filled circle.

**2a.**Produce a scatter plot of`y`

versus`x`

, and set the title and axes labels as you see fit. Then overlay on top a scatter plot of`y2`

versus`x2`

, using