Search code examples
rplotlyr-plotly

How to get correlation matrix as upper half of the matrix with splom using plotly in R


I used splom for pair plots. I would like to draw pair plots with correlation values like provided in R base pair plots. I didn't find the correlation display with pair plots using splom with plotly in R. I go though below links for plotting the pair plots in that they didn't mention pair plot with correlation values.

link that I followed for drawing pair plots.

Desired visualizations with spolm using plotly:

enter image description here

Is that desired visualization possible with splom using plotly in R?


Solution

  • I added a base example too, to make this answer more informative. One of the solution will be to use GGally::ggpairs and plotly::ggplotly functions. Of course the plot returned by plotly::ggplotly will be interactive.

    # Base plot
    
    panel.cor <- function(x, y) {
      usr <- par("usr")
      on.exit(par(usr))
      par(usr = c(0, 1, 0, 1))
      r <- round(cor(x, y), digits = 2)
      # possible to edit the size
      text(0.5, 0.5, r)
    }
    
    pairs(iris[, 1:4],
      upper.panel = panel.cor
    )
    

    ## GGally solution - plotly
    ## assume GGally and plotly are installed
    
    pm <- GGally::ggpairs(iris[, 1:4])
    #> Registered S3 method overwritten by 'GGally':
    #>   method from   
    #>   +.gg   ggplot2
    class(pm)
    #> [1] "gg"       "ggmatrix"
    plotly::ggplotly(pm)
    

    Created on 2021-07-09 by the reprex package (v2.0.0)