ggcorrplot - Correlation plot with Google Analytics data in R
Hello,
I came across this correlation plot type on Sthda and thought of applying it to Google Analytics data. Here’s the article from the package creator: http://www.sthda.com/english/wiki/ggcorrplot-visualization-of-a-correlation-matrix-using-ggplot2
If you’d like to apply this to Google Analytics in R, there are a few steps you can follow [Full script is at the end]:
Use the GCP service credentials key for your R project
Make the API call to GA via googleAnalyticsR. In this example, I pulled sessions, bounceRate, pageviewsPerSession, users, goal1Completions as metrics along with date and dayOfWeek as my dimensions. Once you pull your data, it should look like this.
Install and load ggcorr package
Run the correlation function in base R via… corr <- round(cor(data[ , c(3:7)]),1) . As you need numeric columns for correlation, I’ve asked R to use columns 3:7 only. If you try running this with non numeric columns, you’ll get an error in console “row x needs to be numeric”
Last bit is to just apply the ggcorrplot command to your corr object and put labels = TRUE to show correlation in the plot
That’s about it.
Correlations can vary between -1 and 1. Sessions and Users have a correlation of 1. PagesPerSession and BounceRate have a negative correlation of -0.5, makes sense as they should move in the opposite direciton. The STHDA article has more options on visualizations. You may also run ?ggcorrplot()
in R to read the documentation.