Next, we must create a scatterplot of the tree data points and add the regression line to the plot using the following code:
x = trees$Height
y = trees$Volume
xax <- list(title = "Height", titlefont = list(family = "Modern Computer Roman"))
yax <- list(title = "Volume", titlefont = list(family = "Modern Computer Roman"),
range = c(0, 80))
fig <- plot_ly(x = x, y = y, type = "scatter", mode = "markers",
name = "Data", width = 800, height = 430) %>%
add_lines(x = x, y = predict(mod), mode = "lines", name = "Regression Line") %>%
layout(xaxis = xax, yaxis = yax) %>%
layout(margin = list(l = 150, r = 50, b = 20, t = 40))
Plotly plot Displayed
Displaying the code for the scatterplot from the following slide results in the model below:
Ggplot
The simple linear regression model is not limited to just plotly plots. The correlation between tree Height and Volume can be displayed as a ggplot as well using the following code:
fig2 <- ggplot(aes(x = Height, y = Volume), data = trees) + geom_point() +
geom_smooth(method = "lm", se = FALSE)
Ggplot Displayed
The code from the previous slide displays the following figure:

Analysis
The simple linear regression models that we created show the evidence of a loose positive correlation between the Height and Volume variables of the ‘trees’ data set. The model shows that Volume increases as Height increases.
Further Exploration
Since we have determined that height and volume appear to have a positive correlation, we can explore the data further by visualizing the correlation between other variables in the ‘trees’ data set. Using the following code, we can analyze the correlation between tree Girth and tree Volume.
fig3 <- ggplot(aes(x = Girth, y = Volume), data = trees) + geom_point() +
geom_smooth(method = "lm", se = FALSE)
Ggplot 2
The simple linear regression model displays evidence of a strong positive correlation between tree Girth and Volume.

End of Presentation
Thank You