Get startedGet started for free

Correlations plot

It is often interesting to look at the correlations between variables in the data. The function cor() can be used to create the correlation matrix. A more visual way to look at the correlations is to use corrplot() function (from the corrplot package).

Use the corrplot to visualize the correlation between variables of the Boston dataset.

This exercise is part of the course

Helsinki Open Data Science

View Course

Exercise instructions

  • Calculate the correlation matrix and save it as cor_matrix. Print the matrix to see how it looks like.
  • Adjust the code: use the pipe (%>%) to round the matrix. Rounding can be done with the round() function. Use the first two digits. Print the matrix again.
  • Plot the rounded correlation matrix
  • Adjust the code: add argument type = "upper" to the plot. Print the plot again.
  • Adjust the code little more: add arguments cl.pos = "b", tl.pos = "d" and tl.cex = 0.6 to the plot. Print the plot again.
  • See more of corrplot here

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# MASS, corrplot, tidyverse and Boston dataset are available

# calculate the correlation matrix and round it
cor_matrix<-cor(Boston) 

# print the correlation matrix


# visualize the correlation matrix
corrplot(cor_matrix, method="circle")
Edit and Run Code