1. Reproduce the Following Plots
data(mtcars)
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE) +
labs(title = "MPG vs Weight",
x = "Weight",
y = "Miles Per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

1b. mtcars Dataset (Scatterplot with Line of Best Fit + SE)
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE) +
labs(title = "MPG vs Horsepower",
x = "Horsepower",
y = "Miles Per Gallon")
## `geom_smooth()` using formula = 'y ~ x'

1c. airquality Dataset
data(airquality)
ggplot(airquality, aes(x = Temp, y = Ozone)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, na.rm = TRUE) +
labs(title = "Temperature vs Ozone",
x = "Temperature",
y = "Ozone")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).

1d. airquality Dataset
ggplot(airquality, aes(x = Wind, y = Ozone)) +
geom_point() +
geom_smooth(method = "lm", se = TRUE, na.rm = TRUE) +
labs(title = "Wind vs Ozone",
x = "Wind",
y = "Ozone")
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).

3. Loops
3a. Nested for Loop
for (a in 1:3) {
for (b in 1:3) {
print(c(a, b))
}
}
## [1] 1 1
## [1] 1 2
## [1] 1 3
## [1] 2 1
## [1] 2 2
## [1] 2 3
## [1] 3 1
## [1] 3 2
## [1] 3 3
3b. While Loop
set.seed(123)
while (TRUE) {
num <- rnorm(1)
print(num)
if (num > 1) {
break
}
}
## [1] -0.5604756
## [1] -0.2301775
## [1] 1.558708
3c. Repeat Loop
msg <- c("PSY290")
i <- 1
repeat {
print(msg)
i <- i + 1
if (i > 6) {
break
}
}
## [1] "PSY290"
## [1] "PSY290"
## [1] "PSY290"
## [1] "PSY290"
## [1] "PSY290"
## [1] "PSY290"