Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Know your problem
Know your data
Visualize your data – numerical variables
Work with your chart
13. Add a trend line
Check yourself

Instruction

Highlight the story by adding a trend line to show the relationship between variables.

Now we know that the positive correlation for our data is quite strong. But did you understand that from the graph? Probably not, and your reader certainly couldn't. You or your reader would probably expect a very apparent correlation in the data, but you may not see it. That's the nature of data – it is "noisy" and more often resembles a cloud of points rather than a straight line.

To help readers spot the correlation of data, you can add a trend line, which visually presents the strength and type of the relationship. If the line is upward, we see a positive correlation; if it is downward, we see a negative correlation. If the trend line is parallel to the horizontal axis, there is no straightforward linear relationship. The closer all points are to the trend line, the stronger the correlation is. Note: A trend line is constructed to minimize the distance between the line and the data points.

To add a trend line to your plot, use the R function geom_smooth():

+ geom_smooth(se=F, method="lm")

The two arguments ( se = F and method = "lm" ) are technical and we won't change them. The "se = F" argument disables a fitted trend line, and the "method" sets the type of trend that we fit. We're using only linear trends, so we'll stick with the "lm" method.

Exercise

Add a trend line to our scatterplot object using the above command.

When you're done, press the Run and Check Code button.

Stuck? Here's a hint!

You should write:

geom_smooth(se = F, method = "lm")