---
title: "Lab 4f: Curves, Surfaces, and Colors"
author: "Statistical Computing, 36-350"
date: "Friday September 23, 2016"
---

Name:  
Andrew ID:  
Collaborated with:  

This lab is to be completed in class. You can collaborate with your classmates, but you must identify their names above, and you must submit **your own** lab as an Rmd file on Blackboard, by 11:59pm on the day of the lab.

There are Homework 3 questions dispersed throughout. These must be written up in a separate Rmd document, together with all Homework 3 questions from other labs. Your homework writeup must start as this one: by listing your name, Andrew ID, and who you collaborated with. You must submit **your own** homework as a knit HTML file on Blackboard, by 6pm on **Sunday September 25**. This document contains 14 of the 45 total points for Homework 3.

**Important remark** on compiling the homework: many homework questions depend on variables that are defined in the surrounding lab. It is easiest just to copy and paste the contents of all these labs into **one big Rmd file**, with your lab solutions and homework solutions filled in, knit it, and submit the HTML file.

Curves
===

- Below, we generate and plot some noisy quadratic data. Add the curve $y=x^2$ to the plot with `curve()`, as a thick purple line.

```{r}
n.quad = 15
set.seed(0)
x.quad = runif(n.quad, -3, 3)
y.quad = x.quad^2 + rnorm(n.quad)
plot(x.quad, y.quad, xlim=c(-3, 3))
```

- Reproduce the above plot, but instead of using `curve()` to draw the curve $y=x^2$, use `lines()` along with the vector `x.quad` defined above. What do you notice about how smooth this curve is, compared to that produced with `curve()`? Why is this the case?

- Reproduce the above plot once again, but instead of using `lines()` with `x.quad` to draw the curve $y=x^2$, use `lines()` with a newly defined vector `x.new` of 100 points equally spaced between -3 and 3. (This is essentially all that `curve()` is doing under the hood for you.) What do you notice about how smooth the curve is now?

- Add a rug to your last plot, to draw tickmarks at the `x.quad` points, using `rug()`. (Now, if it wasn't before, it should be more obvious why using `lines()` with `x.quad` produced a choppy quadratic curve.)

**Hw3 Q7 (1 points).** Add a rug to your last plot, this time along the y-axis, with tickmarks at the `y.quad` points. (Hint: read the help file for `rug()`, to see how to control the side of the plot on which the rug is drawn.)

**Hw3 Q8 (3 points).** The code below generates 5000 draws from a standard normal distribution. Plot a histogram, on the probability scales, with 50 breaks. Color the histogram bars to be pink, and label the x-axis and title the plot appropriately. Then draw the standard normal density curve on top of your histogram, using `curve()`, as a thick black line. Do histogram bars conform to the density?

```{r}
x.norm = rnorm(5000)
```

Surfaces
===

- Plot the surface $z=cos(x)^2+sin(y)^2$, for $x,y$ both running in between 0 and $\pi$. Use the `surface()` function, available at http://www.stat.cmu.edu/~ryantibs/statcomp-F16/surface.R. For the colors, use 25 colors from the cyan-magenta color palette. (Hint: recall to use `source()`, as we did in the "Curves, Surfaces, and Colors" mini-lecture, to load this function into your workspace.)

- Plot this same surface again, but now with the viewing angle changed to `theta=45` and `phi=45`. Also, set up the axes to have detailed ticks (with numbers).

- Plot this same surface once again, but now with the viewing angle changed so that it gives a "bird's eye" angle. This means that the x-axis and y-axis should be in their usual orientation (i.e., as they would be usually in a 2d plot), and the z-axis should be coming out of the page.

Colors
===

- Below, we generate some noisy data around the surface $z=cos(x)^2+sin(y)^2$ that you've being plotting above. Plot this surface, with $x,y$ running from 0 to $\pi$ as before. Set the viewing angle to be `theta=45` and `phi=45`, and set the color to be transparent white. Then plot the points `x.trig`, `y.trig`, `z.trig` on top of this surface, as filled black circles. (Hint: recall that you should save the result of `surface()`, then use `trans3d()`, and `points()`, as in the "Curves, Surfaces, and Colors" mini-lecture.)

```{r}
n.trig = 250
x.trig = runif(n.trig, 0, pi)
y.trig = runif(n.trig, 0, pi)
z.trig = cos(x.trig)^2 + sin(y.trig)^2 + 0.5*rnorm(n.trig)
```

- The points in your last plot (should have) run off the top of the display. To fix this, set the argument `zlim` appropriately your initial call to `surface()`. This acts just like the `xlim`, `ylim` arguments in a call to `plot()`, which you have dealt with before; `zlim` should be a vector of length 2, as in `zlim = c(-1, 0)`, to set the z limit to be from -1 to 0. For the current data set, don't just set the z limit "by eye", set it programatically, by using either `min()` and `max()`, or `range()`, on the vector `z.trig`.

- As in the "Curves, Surfaces, and Colors" mini-lecture, color the points on your last plot according to their z value, using 25 colors from the heat color palette. (Hint: recall the `get.col.from.val()` function from the mini-lecture, which you can copy and paste into your lab code. For this function, you must specify the limit of possible values that can be assigned to colors; make use of your setting for `zlim` in the last question.)

**Hw3 Q9 (10 points).** Below, we read in a data table on earthquakes from around the world, of magnitude 6+, since 2002. This is a cleaned up version of the data set we looked at in Lab 3w. We then plot a map of the world, using the `map()` function (you will need to have the package `maps` installed for this). Building off of this starter code, plot each earthquake in the `anss.tab` data table on top of the world map according to the geographic location in which it occurred (hint: look at the "Lat" and "Lon" columns of the data table.) Each earthquake should be represented by a small filled circle. Also, color the circles according to earthquake magnitude (hint: the "Mag" column of the data table), using 25 colors from the topographic color palette.

```{r}
anss.tab = read.table("http://www.stat.cmu.edu/~ryantibs/statcomp-F16/data/anss.dat",
                      sep="\t", header=TRUE)
head(anss.tab)

library(maps)
map("world")
title("Earthquakes colored by magnitude")
```

Most of the circles appear in a darkish purple/blue color, with only a few in lighter green/yellow colors. Why? To support your explanation, plot a histogram, on the frequency scale, of the earthquake magnitudes in `anss.tab`, with break locations set from 5.9 to 9.1, in increments of 0.1. Label the x-axis and title the plot appropriate. 

Plot another map of the world, with earthquakes drawn on top using filled circles, this time coloring them according to earthquake depth (hint: the "Depth" column of the data table). Set the title appropriately. Where do some of the deepest earthquakes occur?

Plot another histogram, on the frequency scale, this time of the earthquake depths. Set the break locations to be from 0 to 700, in increments of 10, and set the x-axis label and title appropriately.

Finally, fit all 4 of the previous plots into one single graphical display, using a 2 x 2 grid for the plots. The top left plot should be the world map with earthquakes colored by magnitude, and the top right plot should be the world map with earthquakes colored by depth. The bottom two plots should be the magnitude and depth histograms, from left to right. (Hint: recall `par()` with `mfrow()`.)

**Hw3 Bonus.** Color the bars in the previous two histograms according to the same color scales you used in the previous two world maps, respectively. For example, in the histogram of earthquake magnitudes, the bar for a histogram of magnitude 6.1 should be colored in the same color you would use for the circle of a magnitude 6.1 earthquake on the world map. And the same for the histogram of earthquake depths.