Skip to main content

Section 8.5 Visualizing Relationships

As emphasized in every chapter, it is imperative that we first graph our data to see what it looks like before we run any statistical analyses. Visuals really help us understand our data. Is our data linear? Can we see any patterns? Let’s find out!
For regression models, we use scatterplots. For a scatterplot, all we need are two numerical variables.
# Now, let us graph our data using ggplot
# As always with ggplot, we have our data, our aesthetics, and our geometry.

pizza_plot <- ggplot(pizza_sales, aes(x = price, y = volume)) +
  geom_point(size = 3)  +
  geom_smooth(method = "lm", se = FALSE) + # this adds a "line of best fit"
  theme_minimal() +
  labs(
    title = "Analysis of Pizza Sales",
    subtitle = "You know the rule, one bite...",
    x = "Price ($)", y = "Volume"
  )

pizza_plot
A scatterplot with price on the x-axis and volume on the y-axis. Each point represents a week of sales data. A linear trend line with negative slope is fitted through the data.
Figure 8.5.1. Scatterplot showing the relationship between pizza price and weekly sales volume, with a fitted linear trend line. The downward slope indicates a negative linear association, suggesting that higher prices are associated with lower sales volume. This visualization is used to assess linearity and motivate the use of correlation and linear regression.
If we wanted to use another package instead of ggplot, we could also use the library ggformula, which has gf_point().
library(ggformula)
pizza_plot_ggformula <- gf_point(volume ~ price, data = pizza_sales) |> gf_lm(color ="purple")

pizza_plot_ggformula
A scatterplot with price on the x-axis and volume on the y-axis, created using ggformula. A purple linear trend line shows the negative relationship between price and volume.
Figure 8.5.2. Scatterplot of pizza price and weekly sales volume created using the ggformula package, with a fitted linear regression line. This plot conveys the same information as the ggplot version, demonstrating that different visualization frameworks can be used to explore linear relationships.
Here is a side by side comparison of what they look like. Very similar indeed.
library(patchwork)

pizza_plot + pizza_plot_ggformula
Two scatterplots placed side by side: the left uses ggplot2 with a red trend line, and the right uses ggformula with a purple trend line. Both show the same negative association between pizza price and sales volume.
Figure 8.5.3. Side-by-side comparison of scatterplots generated using ggplot2 and ggformula. Both visualizations display the same negative linear relationship between pizza price and sales volume, illustrating that different plotting systems can yield equivalent analytical insights.
Our data is already telling us a lot. From both of our graphics, we can see that as price is going up, volume is going down, indicating a negative correlation. Our line also seems pretty straight, indicating what seems to be a moderately strong correlation.