How to Create Correlation Plots in R

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ค. 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...

ความคิดเห็น • 69

  • @AlexisMelo
    @AlexisMelo 2 ปีที่แล้ว +2

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว

      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.

  • @sawersoft
    @sawersoft ปีที่แล้ว

    Excellent! Wonderfull explanation! Thank you very much!

  • @jeffgoblue
    @jeffgoblue ปีที่แล้ว +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!

  • @naumanejaz8861
    @naumanejaz8861 2 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว

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

  • @munkhdulamotgonbayar9923
    @munkhdulamotgonbayar9923 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

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

  • @stevennye5075
    @stevennye5075 ปีที่แล้ว

    excellent overview!

  •  2 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +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.

  • @mikkimooh
    @mikkimooh ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

      Glad you liked it and left such a nice comment.

    • @Hassan_MM.
      @Hassan_MM. ปีที่แล้ว

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

  • @TaraGhimite
    @TaraGhimite ปีที่แล้ว +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  ปีที่แล้ว

      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 :)

  • @tajamulislamshah8494
    @tajamulislamshah8494 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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 7 หลายเดือนก่อน

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

  • @nasimsadra1595
    @nasimsadra1595 ปีที่แล้ว

    this was amazing! thank you

  • @XXTargaryenXX
    @XXTargaryenXX 20 วันที่ผ่านมา

    Fantastic! It helped a lot

  • @umagenetics3975
    @umagenetics3975 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว +1

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

  • @liadirakusuma145
    @liadirakusuma145 ปีที่แล้ว +1

    it was very helpfull.. thank you so much

  • @pinakikkkm
    @pinakikkkm ปีที่แล้ว +1

    Helped a Lot. Thank you.

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

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

  • @sintubmb
    @sintubmb 2 ปีที่แล้ว +1

    Superb video

  • @rwaewae
    @rwaewae 2 ปีที่แล้ว +1

    Great, thanx

  • @pipertripp
    @pipertripp 5 หลายเดือนก่อน +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  5 หลายเดือนก่อน +1

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

  • @rifathossain9272
    @rifathossain9272 ปีที่แล้ว

    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))

  • @wildlifeireland9514
    @wildlifeireland9514 2 หลายเดือนก่อน +1

    Brilliant detail

    • @TheDataDigest
      @TheDataDigest  2 หลายเดือนก่อน +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 2 หลายเดือนก่อน

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

  • @Geology_monster
    @Geology_monster หลายเดือนก่อน

    bro i love u

  • @renanarruda9855
    @renanarruda9855 2 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +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 2 ปีที่แล้ว

      @@TheDataDigest Lot of thanks!

  • @rgonzalezz
    @rgonzalezz ปีที่แล้ว +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  ปีที่แล้ว

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

  • @korman9872
    @korman9872 ปีที่แล้ว +1

    Wow tx sir

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

      No problem. I am glad you watch my videos.

  • @findthetruth3021
    @findthetruth3021 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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 2 ปีที่แล้ว +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  2 ปีที่แล้ว +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.

  • @rwaewae
    @rwaewae 2 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +1

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

    • @rwaewae
      @rwaewae 2 ปีที่แล้ว

      @@TheDataDigest THANX. I will try.

  • @scenarios911
    @scenarios911 ปีที่แล้ว +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  ปีที่แล้ว

      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 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  ปีที่แล้ว

      @@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.

  • @ayesharukhsar5122
    @ayesharukhsar5122 5 หลายเดือนก่อน

    How I apply this script on my data set

  • @mohammedadjeli6849
    @mohammedadjeli6849 ปีที่แล้ว +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  ปีที่แล้ว

      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 ปีที่แล้ว

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

  • @meliheser01
    @meliheser01 2 ปีที่แล้ว

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว +1

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

    • @meliheser01
      @meliheser01 2 ปีที่แล้ว

      @@TheDataDigest i meant plotting them

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว

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

  • @sheikhrashid3574
    @sheikhrashid3574 2 ปีที่แล้ว +1

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

    • @TheDataDigest
      @TheDataDigest  2 ปีที่แล้ว

      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 2 ปีที่แล้ว

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

  • @zahkiani2581
    @zahkiani2581 7 หลายเดือนก่อน

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

    • @TheDataDigest
      @TheDataDigest  7 หลายเดือนก่อน

      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 7 หลายเดือนก่อน

      @@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?

  • @el-houssainebahouar4269
    @el-houssainebahouar4269 2 ปีที่แล้ว

    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  2 ปีที่แล้ว

      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.