How to Create Correlation Plots in R

Sdílet
Vložit
  • čas přidán 23. 07. 2024
  • In this tutorial I show you how you can create Correlation Plots in R with various functions from different packages.
    Namely the corrplot, GGally, corrgram and psych packages.
    Often you can add distribution charts like histograms or density plots as well as scatter plots to these correlograms.
    ⏱ Time Stamps ⌚
    0:00 - Intro and video overview
    0:37 - corrplot()
    5:20 - ggpairs() from GGally
    6:23 - ggcorr() from GGally
    7:15 - corrgram()
    9:06 - pairs.panels() and corPlot() from psych
    11:41 - geom_tile() and other functions
    External Links:
    www.r-graph-gallery.com/corre...
    cran.r-project.org/web/packag...
    r-coder.com/correlation-plot-r/
    www.sthda.com/english/wiki/gga...
    www.sthda.com/english/wiki/vis...
    www.r-bloggers.com/2021/06/gg...

Komentáře • 70

  • @sawersoft
    @sawersoft Před rokem

    Excellent! Wonderfull explanation! Thank you very much!

  • @AlexisMelo
    @AlexisMelo Před 2 lety +2

    Thanks again for another awesome video!
    Big fan in here!

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      My pleasure :)
      I hope I will gather more fans in the future that look forward to weekly new content. But great to hear I have at least one already.

  • @munkhdulamotgonbayar9923
    @munkhdulamotgonbayar9923 Před 2 lety +1

    Great. Thank you very much for sharing this useful tutorial.

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      Thanks for leaving such a nice comment. Glad it helped :)

  • @naumanejaz8861
    @naumanejaz8861 Před 2 lety +1

    This is a great video I just love your videos.Great job

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      Hi, thanks for the kind comment. Just the encouragement I needed to upload new video after a six week long break.

  •  Před 2 lety +1

    This is very useful, I'm sure I'll use this. Thanks!

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      Thanks and I agree. It is a quick way to look for relationships between the variables. Some of the functions are quite powerful in doing hierarchical clustering already.

  • @XXTargaryenXX
    @XXTargaryenXX Před měsícem

    Fantastic! It helped a lot

  • @mikkimooh
    @mikkimooh Před rokem +1

    wow this was so helpful and perfect for my needs. Thank you for your effort!

    • @TheDataDigest
      @TheDataDigest  Před rokem

      Glad you liked it and left such a nice comment.

    • @Hassan_MM.
      @Hassan_MM. Před rokem

      ​@TheDataDigest Please ,Show Correlation of Random Variables at Levels & at first Differences❤

  • @vineethavs9260
    @vineethavs9260 Před 8 dny

    Life saver. Thank you sir..

  • @TaraGhimite
    @TaraGhimite Před rokem +1

    Thank you Sir for the excellent explanation, I believe now, I will be able to learn and I have a hope. Please keep upload more videos

    • @TheDataDigest
      @TheDataDigest  Před rokem

      Hi Tara, thank you for leaving such a nice and encouraging comment. I am planning many more uploads and hopefully get to some in the next weeks. Until then :)

  • @stevennye5075
    @stevennye5075 Před rokem

    excellent overview!

  • @nasimsadra1595
    @nasimsadra1595 Před rokem

    this was amazing! thank you

  • @pinakikkkm
    @pinakikkkm Před rokem +1

    Helped a Lot. Thank you.

    • @TheDataDigest
      @TheDataDigest  Před rokem

      Glad to hear that and thanks for letting me know in a comment :)

  • @umagenetics3975
    @umagenetics3975 Před rokem +1

    @0.38 sec, i liked the video and subscribed your channel. very easy to get information

    • @TheDataDigest
      @TheDataDigest  Před rokem +1

      Glad to have you as a subscriber. Sorry I haven't uploaded much lately, but that will change next week :)

  • @liadirakusuma145
    @liadirakusuma145 Před rokem +1

    it was very helpfull.. thank you so much

  • @tajamulislamshah8494
    @tajamulislamshah8494 Před 2 lety +2

    You are doing great service. I am loving your videos. Kindly keep uploading more videos on R. Also, create a video to show multivariates in single graph.

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      Thank you for this encouraging comment. I keep track of all the requests and suggestions and will hopefully be able to fulfill some wishes of my subscribers. Luckily I am interested in a wide range of R-related topics so there will be videos covering many things in the future.

    • @zahkiani2581
      @zahkiani2581 Před 7 měsíci

      @@TheDataDigest Hello is it possible to give me R code for drawing coreplot?

  • @jeffgoblue
    @jeffgoblue Před rokem +2

    Great video!
    How do you reverse/flip the color scale in the corrplot package? I prefer to have negative correlation blue and positive correlations red. Sort of a temperature/ cold-and-hot mentality. Thanks!

  • @rwaewae
    @rwaewae Před 2 lety +1

    Great, thanx

  • @sintubmb
    @sintubmb Před 2 lety +1

    Superb video

  • @wildlifeireland9514
    @wildlifeireland9514 Před 2 měsíci +1

    Brilliant detail

    • @TheDataDigest
      @TheDataDigest  Před 2 měsíci +1

      Thanks. It is my best performing video so far. Maybe I should make one about ggpairs which is also a good first visualization package.

    • @wildlifeireland9514
      @wildlifeireland9514 Před 2 měsíci

      Absolutely, if it's not too much hassle....they're of great help

  • @Geology_monster
    @Geology_monster Před 2 měsíci

    bro i love u

  • @rifathossain9272
    @rifathossain9272 Před rokem

    In the following code, how can I change the default color? I have tried using color brewer palette where only histograms colors are being changed but scatterplot color remain unchanged. How can I change default color for everything including labels & scatterplots?
    GGally::ggpairs(iris, columns = 1:4,
    ggplot2::aes(colour=species))

  • @pipertripp
    @pipertripp Před 5 měsíci +1

    for getting help you can also put the cursor on the function you want to know more about and hit the F1 key.

    • @TheDataDigest
      @TheDataDigest  Před 5 měsíci +1

      Thanks for the tip! F1 is usually the help in many programs but I never tried it out in RStudio.

  • @korman9872
    @korman9872 Před rokem +1

    Wow tx sir

  • @rgonzalezz
    @rgonzalezz Před rokem +1

    Hello! thanks for you video, may i ask for a little help... when i call the function ggcorr the console print Error in ggcorr, could not found the function "ggcorr"

    • @TheDataDigest
      @TheDataDigest  Před rokem

      The ggcorr function is part of the GGally package. Did you make sure to install and load it first?
      install.packages("GGally"); library(GGally)

  • @renanarruda9855
    @renanarruda9855 Před 2 lety +1

    Great! This video is really helpful. Is possible include *** for the significance level in corrplot package?

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +2

      Hi Renan, yes it is. The help function and examples are actually quite good for this function.
      Try: ?corrplot() or help(corrplot) and then scroll down to this section:
      It shows you many examples that you can customize on how to adding p-values and stars etc.
      data(mtcars)
      M = cor(mtcars)
      ##the input matrix is not square
      testRes = cor.mtest(mtcars, conf.level = 0.95)
      ## specialized the insignificant value according to the significant level
      corrplot(M, p.mat = testRes$p, sig.level = 0.05, order = 'hclust', addrect = 2)
      ## leave blank on no significant coefficient
      corrplot(M, p.mat = testRes$p, method = 'circle', type = 'lower', insig ='blank',
      addCoef.col ='black', number.cex = 0.8, order = 'AOE', diag = FALSE)
      ## add p-values on no significant coefficients
      corrplot(M, p.mat = testRes$p, insig = 'p-value')
      ## add all p-values
      corrplot(M, p.mat = testRes$p, insig = 'p-value', sig.level = -1)
      ## add significant level stars
      corrplot(M, p.mat = testRes$p, method = 'color', diag = FALSE, type = 'upper',
      sig.level = c(0.001, 0.01, 0.05), pch.cex = 0.9,
      insig = 'label_sig', pch.col = 'grey20', order = 'AOE')

    • @renanarruda9855
      @renanarruda9855 Před 2 lety

      @@TheDataDigest Lot of thanks!

  • @findthetruth3021
    @findthetruth3021 Před 2 lety +1

    Can you make a video on Flexdashboard and how to add filters to the dashboard to create full interactivity? For instance, I filter on a specific item then the whole dashboard will be reflected on it.

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      More interactive graphs, dashboards and shinyApps are definitely on my list of topics I want to cover. I am not sure when I will get there but I keep track of your request and thank you for the comment.

    • @findthetruth3021
      @findthetruth3021 Před 2 lety +1

      @@TheDataDigest Yes, please. Flexdashboard is also really awesome if you can make a video on it as well and applying filter on the dashboard as I mentioned before. That would be awesome. But please tech us in an easy way like to use your Dashboard as a template just changing the data source. Thanks in advance, much appreciated 👏👏👏

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      @@findthetruth3021 I will definitely create something on that topic this year :) Maybe here is already something that fits your needs: pkgs.rstudio.com/flexdashboard/articles/examples.html
      In the top right corner you get the source code an can try to adjust it accordingly.

  • @ayesharukhsar5122
    @ayesharukhsar5122 Před 6 měsíci

    How I apply this script on my data set

  • @mohammedadjeli6849
    @mohammedadjeli6849 Před 2 lety +1

    thanks for this video. I have a problem whith #adding species information and color#, this step didn't work for me when I executeö
    GGally::ggpairs(iris, columns = 1:4,
    ggplot2::aes(colour=species))
    this message appears:
    plot: [1,1] [==>-----------------------------------------------] 6% est: 0s Error in FUN(X[[i]], ...) : object 'species' not found
    Can you help me please

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      Of course I can help you. When I copy your code I get the same error. Unfortunately you made a tiny mistake that is usually really hard to find. The error message tells you that it cannot find "species" in the iris data. Because in iris it is written with a capital-S. When you change species to Species it runs fine. Don't be discourage, these typos happen very often.

    • @mohammedadjeli6849
      @mohammedadjeli6849 Před 2 lety

      @@TheDataDigest thanks a lot for your help
      can you give me how to interpret this diagram (iris)

  • @rwaewae
    @rwaewae Před 2 lety +1

    Hi, can we plot summary statistics and correlation plot in single diagram.? thanx

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      Hi, I recommend looking into the patchwork package. In this video I combined many charts into one plot (czcams.com/video/FzntrJ8l5as/video.html).
      Also in the 30DayChartChallenge (twitter.com/DigestData) you find many examples of multiple charts in one plot.

    • @rwaewae
      @rwaewae Před 2 lety

      @@TheDataDigest THANX. I will try.

  • @scenarios911
    @scenarios911 Před rokem +1

    Hello, it's a nice video, can you please tell us how to plot few variables correlation species, actually i got the plot but that is mixed, among variables. I want few variables on one side and species another side. Is it possible? if yes, then kindly suggests us some information about it. Thank you.

    • @TheDataDigest
      @TheDataDigest  Před rokem

      Hi Muhammad Farooq, unfortunately I don't understand the question completely.
      Can you give me a time stamp to where in the video your question relates to?
      If you mean species from the iris dataset then this does not really have a correlation to variables, as it is a category with only 3 levels.
      However I post code below to show you how to only show correlation between a few selected variables (maybe that is what you asked about):
      corr_matrix

    • @scenarios911
      @scenarios911 Před rokem +1

      @@TheDataDigest Wow, that's worked sir, i am really grateful to you, thanks once again. Hats off to you..

    • @TheDataDigest
      @TheDataDigest  Před rokem

      @@scenarios911 Glad it worked and was what you were looking for. I think I should have included such in example in the video as another subscriber emailed me with a similar request.
      Anyway, thanks for leaving such a polite comment and letting me know my answer helped. It is always very rewarding to see the extra effort being useful to my subscribers.

  • @meliheser01
    @meliheser01 Před 2 lety

    If i want to create correlation between two variables. How should I do it?

    • @TheDataDigest
      @TheDataDigest  Před 2 lety +1

      Try: cor.test(x=data$variable1, y=data$variable2), or did you mean plotting them?

    • @meliheser01
      @meliheser01 Před 2 lety

      @@TheDataDigest i meant plotting them

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      @@meliheser01 I suggest looking into geom_point() for scatterplots [czcams.com/video/sk59wjdmrd8/video.html], you can also add geom_smooth(method='lm', formula= y~x))

  • @zahkiani2581
    @zahkiani2581 Před 7 měsíci

    Hello, can you help me in this soft ware? I want to drag a coreplot

    • @TheDataDigest
      @TheDataDigest  Před 7 měsíci

      Hi, do you need the R-code from the video or do you have a specific request to help with your data?
      You can send questions directly to: question[at]thedatadigest[dot]email

    • @zahkiani2581
      @zahkiani2581 Před 7 měsíci

      @@TheDataDigest thanks for your response, yes I want code for corep analysis and drawing the figures in R software, can i put my question to the email address that you sent?

  • @sheikhrashid3574
    @sheikhrashid3574 Před 2 lety +1

    could you please make a video on how to save output results in PDF file.

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      Excellent request Rashid and quite a coincidence because another guy just asked about options to save images as png file. My next video is already planned and will be published soon, but after that I will make a video on how to save charts as png or pdf.
      What you can do for now is simply create your chart and in RStudio select the Export option in your Plots panel. Save as PDF should be an option to chose. Use the preview button to check whether the height and width looks good and save as file.

    • @sheikhrashid3574
      @sheikhrashid3574 Před 2 lety

      @@TheDataDigest Thank you for your Response. Actually , i want to know how to save output results in PDF.

  • @el-houssainebahouar4269

    I thank you very much for this very rich tutorial, but when I tried to apply it to my data I receive this message which I do not understand because I am still a beginner if you can help me and thank you once again:
    GGally::ggpairs(log,columns = 1:13,ggplot2::aes(colours=Season))
    plot: [1,2] [=>---------------------------------------- -------------------------------------------------- -------------------------------------------------- -] 1% is:14s Error in cor.test.default(x, y, method = method, use = use):
    not enough finite observations

    • @TheDataDigest
      @TheDataDigest  Před 2 lety

      Sorry to hear that, but errors are common and should not discourage you. What I recommend is to post a reproducible example with a small dataset and R-code that produced the error at: www.stackoverflow.com
      Feel free to post the link here and I can answer on their website.