Visualizing correlation with double y-axes using the ggplot2 R package (CC235)

Published 2022-08-01
Should you visualize your data using double y-axes? In this Code Club, Pat will show how to assess correlations using ggplot2 using faceted line plots, line plots on double y-axes, and using a scatter plot. Finally, he'll use cor.test there's a lot of other great gems to be found in the episode.


You can find my blog post for this episode at www.riffomonas.org/code_club/2022-08-01-temp_prcp_….

#ggplot2 #dplyr #R #Rstudio #Rstats

Want more practice on the concepts covered in Code Club? You can sign up for my weekly newsletter at shop.riffomonas.org/youtube to get practice problems, tips, and insights.

If you're interested in taking an upcoming 3 day R workshop be sure to check out our schedule at riffomonas.org/workshops/

You can also find complete tutorials for learning R with the tidyverse using...
Microbial ecology data: www.riffomonas.org/minimalR/
General data: www.riffomonas.org/generalR/

0:00 Introduction
1:02 Calculating average annual temperature and total precipitation data
5:21 Creating a faceted line plot
8:39 Creating a line plot with a double y-axis
13:16 Rescaling both y-axes
17:58 Should we use double y-axes?
19:44 Plotting variables against each other
21:03 Measuring correlation between variables

All Comments (14)
  • Another terrific installment. Thanks for reminding me about "cor.test." I almost never report p-values and I should. Also reminded me why dual y-axes are flawed.
  • @ndongoseye837
    Thanks Pat, It's so helpful. I enjoy how you explain and your pedagogical approach.
  • Thank you for a great tutorial Prof. Pat. I live in Bangkok, Thailand. There is a correlation between tmax (average) and prcp with p-value = 2.081e-05 and cor = -0.49.
  • @tongakuot
    Great tutorial; thank you for sharing!
  • I have seen much worse. Plots with four variables on the same plot. On read from the left side up (temp I belive), another read on the right side (precip). The third variable (vegitation) was added to the top part but without a scale. Finally the lower half of the graph was in meters and showed the depth of erosion. All of this plotted against latitude along the x. I think if anyone can read this monstrosity they should get a medal for the "worst" plot.
  • @md.izazulhaq
    You're the best! Loved your content. Please make a video tutorial on how to convert irregular daily/fortnightly timesereis with lots of NA's and mutlple varibles into regular motntly timeserie data.
  • @maisamsna
    In 14:11, you should have remembered to subtract tmax_min. It should be divided by (tmax_max - tmax_min).
  • Pat! It was a nice and helpful video, Thank you so much. I have a question: It is possible to do figures with double X-axes in ggplot? As you mentioned in the video the best option is to show the variables in different panels, but sometimes when you work with hydroclimatological variables could be useful to have a double X-axis.
  • Hi, i want to sum each five (5) days cumulative precipitation using R. any help?
  • Can you please do a correlation visualization of species from any metagenomic dataset? Because with a lot of species, the visualization becomes challenging, would like to see if it can be done in a better way.