Visualizing bivariate relationships
If you want to go even further than simply plotting variables and instead investigate whether any relationship exists between 2 variables, you can draw a scatterplot. This is a graph where the values of two variables are plotted along two axes.
The pattern of the resulting points is used to reveal the presence of any correlation; usually, a regression line is added to identify the tendency, if there is any:
- An upward sloping regression line indicates a positive linear relationship between A and B (when A goes up B tends to goes up as well)
- A downward sloping regression line indicates a negative linear relationship between A and B
You can draw a scatterplot and then create a regression model with the following functions:
plot(x = A, y = B)
lm(B ~ A)
In this exercise, you will draw a scatterplot and regression line for the return series for the SP500 (sp500) and Citigroup (citi) from January 2015 to January 2017, both of which are provided in your workspace
This exercise is part of the course
Visualizing Time Series Data in R
Exercise instructions
- Draw a scatterplot with
sp500on the x-axis andcition the y-axis. - Add a regression line of
citiagainstsp500usinglm()andabline().- Specify the regression as the
regargument toabline(). - Make this line red and twice as thick as the default.
- Specify the regression as the
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Draw the scatterplot
# Draw a regression line