Here’s the code I used to create both the 2D plot and the 3D plot:
library(ggplot2)
library(plotly)
model <- lm(Exam_Scores ~ Hours_Studied, data = study_data)
ggplot(study_data, aes(x = Hours_Studied, y = Exam_Scores)) +
geom_point() +
labs(title = "Scatterplot with Line of Best Fit",
x = "Hours Studied",
y = "Exam Scores")
study_data$Grade <- 70 + 1.5 * study_data$Hours_Studied + rnorm(50, mean = 0, sd = 5)
head(study_data)
plot_ly(study_data, x = ~Hours_Studied, y = ~Exam_Scores, z = ~Grade, type = "scatter3d", mode = "markers",
marker = list(size = 5, color = 'red')) %>%
layout(scene = list(xaxis = list(title = 'Hours Studied'),
yaxis = list(title = 'Exam Scores'),
zaxis = list(title = 'Grade')))