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 ggplot graphics. please do not use base R graphic commands.

This week’s agenda: getting familiar with basic plotting tools; understanding the way geoms work; how to map data frame columns to plot aesthetics; recalling basic text manipulations.

Plot basics


n = 200
my_x <- runif(n, min = -2, max = 2)
my_c <- sample(c(0, 1),size = n,replace = TRUE)
my_y <- ifelse(my_c == 0, my_x^3, my_x^2) + rnorm(n)
my_data <- data.frame(x = my_x, y = my_y, c = my_c)

a <- ggplot(data = my_data, aes(x = x, y = y)) + geom_point() 
b <- ggplot(data = my_data, aes(x = x, y = y)) + geom_point() 


Reading about ggplot2:

Ben thinks this question is very important for your future use of ggplot. Also if you’re already taken 315 you’ve probably read all of these - just say “I took 315” instead of answering this question. (but they’re a good refresher if you want to do them still)

2a. Read this article on ggplot() from Liz Sander.

2b. Read this tutorial on ggplot().

2c. Read this article on ggplot().

2d. Read this article on data visualization in R.

Adding to plots

  1. my_data_cube: x column defined by sort(runif(100, -2, 2)), a y_clean column that is x^3, and y column as y_clean + rnorm(100)

  2. my_data_square: x column defined by sort(runif(50, -2, 2)) (make if different values than the x column in my_data_cube), a y_clean column that is x^2, and y column as y_clean + rnorm(50)


Below, we read in a data set related to shark attacks. The data is taken from Kaggle and was originally compiled by the global shark attack file. More information is available here.

shark_attacks <- read.csv("", = TRUE)

Text manipulations, and layered plots