how to handle blank fields in correlation plot - R

283 Views Asked by At

I am plotting a correlation plot using ggpairs - the problem I have is that when I include specific columns in the plot some field of the plot gets blank and I have not figured out what could be the reason. There are no NaNs or inf in the columns.

columns that I plot looks like this:

col37,col38,col42,col43,col44,col45,col47
6.40514146313634,5.90086680798075,2.40143697262243,6.57925638237594,0.254345787558367,5.68650052718322,3.021007575319
6.61960864352805,6.06608919045777,2.32386365585468,6.6724253419715,0.0402490401830855,2.55113762307289,3.39880204315015
6.55018833807972,6.36998535486677,0.900691491527457,6.6724253419715,2.31307314716301,6.6724253419715,3.50450447520331
6.60765194902926,6.42574368161488,0.844839684646299,6.37331135222887,0.600591443185196,6.6724253419715,3.84359032949021
5.90677415063159,5.79356591670565,0.774971771845228,6.6724253419715,1.54849183305736,6.6724253419715,2.67202156794908
6.65821148275179,6.15987133677839,0.094806602528702,1.76755391399963,0.0683891386843734,1.49560298058251,2.22285119652822
6.63420601974101,6.53397857200235,0.636233547363233,3.19128283104183,0,0,2.14895522759519
6.62205181945638,6.44708322620965,0.146518637521186,0.583682931034991,0.325201468092632,1.81762325751143,2.45724405939953
6.65821148275179,6.65821148275179,0.702284302480206,2.53841991478413,0.57068661530475,2.53841991478413,2.09953567355092
6.65821148275179,6.65821148275179,0.479198293320067,1.98935275580049,0.365628333350581,1.69797146255034,2.27301849440641
6.64965835237358,6.45354706238544,0.802783546021951,6.20985855186186,0.280186677930464,4.15650448567999,3.82719727249051
6.65821148275179,6.65821148275179,0.241252680228721,0.733176504553092,0.480814393131308,1.75551599053798,2.14684138832927
6.55902481476715,6.49311806356625,0.893445338411045,6.65375240888889,0.318419184390704,4.71424551766612,3.02255201935584
6.58496250072116,6.6192324028059,0.490631582147239,5.68650052718322,0.936453196848269,6.6724253419715,3.01937478203057
6.44817065751228,5.94928917966647,1.25155854561321,6.6724253419715,0.0487536594923067,0.575594318186961,2.58172204844072
6.64454302389429,6.21130892220743,0.297632475622459,2.20387233336565,0.277617715728049,3.90024221959915,3.2894750894239
6.65633076935693,6.4350670702381,1.50298952586988,6.6724253419715,0.374355357091855,4.71424551766612,2.94069361473381
6.64492445716222,6.48012945803804,1.45257205995061,6.6724253419715,1.02012669443575,6.6724253419715,2.5911389992217
6.63420601974101,6.62611460738746,0,0,0.90314096785153,6.6724253419715,2.09186139649834
6.62935662007961,6.62935662007961,0,0,2.57733972661197,6.6724253419715,3.2667865406949
6.63420601974101,6.63420601974101,3.39891020413755,6.6724253419715,1.57916886795736,6.6724253419715,2.4272638016121
6.64225230407214,6.58913093771786,0.195814440357094,2.72489276186563,0.260576688767669,3.39620085726599,2.8824401101238
6.65821148275179,6.65821148275179,0.156327158519214,2.7951802081115,0.628277068994019,5.11547721741994,3.05889368905357
6.59432460392485,6.56605403817109,0.687443839985558,6.6724253419715,1.23360677369035,6.6724253419715,3.58277855405078
6.65821148275179,6.65821148275179,0.533149899924447,3.47248777146274,0.337880513861858,3.47248777146274,3.58496250072116
6.65542617709055,6.37074401083041,0.284477058341618,3.23521646169403,0.53156903272269,6.50426726670337,2.61577989054696
6.57156916843504,6.60780755326468,0,0,0.837723994225965,6.6724253419715,2.60348077443649
6.65821148275179,6.49785183695112,0.108209949798639,1.65486451395131,0.0960976235715174,1.75950816281729,2.38958161402448
6.62611460738746,6.64225230407214,0.256776001288415,3.94753258010586,1.63450298074363,6.6724253419715,3.58496250072116
6.24884426701125,6.20794565023018,1.30083740579799,6.6724253419715,0.867366693019027,6.6724253419715,2.55197539856245
6.64797229824696,6.57418802912698,2.92365651455329,6.6724253419715,2.47100342817163,6.6724253419715,2.60069467560733
6.65556384934053,6.57938578314372,0.263752333824358,2.7951802081115,0.025428142624833,2.59693514238723,1.91538046654444
6.57238443464169,6.49452228310566,0.918543414923869,5.27818940908626,0.382821670339945,4.71424551766612,2.98440938755802
6.57778846783342,6.18794114472103,0.780513519756205,5.2807707701306,0.136620236974111,1.69797146255034,2.42251793178732
6.55232425568694,6.19336740061352,0.328250112301077,6.09451759878429,0.925109740290486,6.6724253419715,2.5224713830764
6.5274770060604,6.36194377373524,0.168911127023766,2.24511249783653,0.646308065975975,6.6724253419715,2.42845878099781
6.62205181945638,6.58496250072116,0.265286511341407,2.8703647195834,2.75934692704241,6.26209484537018,2.69821847822441
6.60141302150175,6.39279005015101,0.546898651955453,6.6724253419715,1.18971404242687,6.6724253419715,3.86506425690769
6.65821148275179,6.65821148275179,0.129013212552379,1.55694812455156,0.0839468505594894,1.61410884638067,3.05889368905357
6.3242783995737,6.36765214984882,1.75794278935807,6.6724253419715,0.415080060361025,5.68650052718322,2.70696670635344
6.65717603087293,6.52492491793532,0.30570011523655,6.6724253419715,0.657103091359165,6.09451759878429,2.46184361058457
6.61726781471599,6.50088516798363,0.654781108349025,4.18197262920684,0.36427068499319,3.13245029602365,2.22142829633298
6.63571812033105,6.5127404628035,1.0880878846382,5.26983341625377,1.34101872297681,6.6724253419715,3.56024286651279
6.63420601974101,6.52530329005344,0.199256980711198,3.13245029602365,0.102530236493767,2.02641518671481,2.70479293648992
6.65821148275179,6.36923380966572,0.201073906489019,1,1.63462883370775,6.6724253419715,3.66789212523023
6.63016598618268,6.63016598618268,0.420685965329095,4.71424551766612,0.408521878591029,5.11547721741994,3.47799676015376
6.63420601974101,6.45395648857115,0.0156715323547459,0.279748279395064,0.236931033841168,3.03813512888677,2.23815497078329
6.65184898052826,6.58996318177952,1.04225162630303,6.6724253419715,1.47792907179468,6.6724253419715,3.50890849886721
6.65002595701493,6.64797229824696,0.298708328963986,5.11547721741994,0.14664966963668,3.94753258010586,2.92247732568701
6.63571812033105,6.56605403817109,0.284967150053465,2.83212282720655,1.32969475461736,6.6724253419715,3.10396023678164
6.65616945121358,6.6168113667605,0.234431371389141,3.94753258010586,0.408084703924947,4.71424551766612,2.74028579490245
6.60979435370109,6.11547721741994,0.222299531744348,1.31341659503185,0.98620007409537,3.34792330342031,2.83129297671436
6.57374952443544,6.02356706084555,0.830611763503904,6.6724253419715,0.844479466461899,5.81729315911372,2.78927175536496
6.21593739919915,6.63420601974101,1.40704797696834,5.11547721741994,0,0,0.208399149256948
6.63606684166916,6.48319566333213,0.814125850912173,5.11547721741994,1.0195137810662,6.6724253419715,2.54799915371001
6.56948962785184,6.00839487047585,0.299652848329962,4.71424551766612,0.879234967807922,6.66786134902984,3.09234219401437
6.65821148275179,6.64385618977472,0.0370957737684139,1.02566488339335,1.89723134788152,6.6724253419715,2.6092391920278
6.37496319306959,5.86657399434923,1.36161651362136,6.6724253419715,1.13184637998339,6.6724253419715,2.91476070517099
6.61825891119628,6.12857215462909,0.51941348421524,6.41221698021647,1.39382410416203,6.6724253419715,2.53352460403978
6.64830020584227,5.92316941476625,0.688703740567359,5.73142852933948,1.64447916048337,6.6724253419715,3.24932504549517
5.71535210357979,5.70359200280577,0.860923594793391,6.6724253419715,0.889726944641535,6.6724253419715,2.92045660577739
6.61989629083054,6.44099126951445,1.20668576929653,5.92319240793762,0.712361857546758,6.6724253419715,3.39469176625984
6.65821148275179,6.60979435370109,0.0588473252076879,0.465101337329658,0,0,2.94468964044364
6.64024493622235,6.64024493622235,1.87101355927992,6.6724253419715,0,0,3.52576118672814
6.64024493622235,6.49452228310566,0.196926305287155,2.12394471395803,1.2666376399255,5.11547721741994,2.84105370629476
5.54225804976692,5.62058641045188,2.52233800773279,6.6724253419715,1.5180890967249,6.6724253419715,3.10918100028486
6.45924171121408,6.0299746826698,0.828757364900789,6.6724253419715,1.38666598669624,6.6724253419715,2.7779591996624
6.55969574211073,6.55969574211073,0.128343986443969,2.05528243550119,0.00879206952142764,0.191126717822359,2.80921047863609
6.58496250072116,6.62205181945638,0.0530011501418263,0.592194069952232,1.16673926612788,4.40599235967584,3.54202870471581
6.21593739919915,6.60979435370109,0.197273746433449,1.8802607557259,0,0,4.28939584090278
6.63571812033105,6.54689445988764,2.01095434214408,6.6724253419715,0,0,4.05543520672396
6.65821148275179,6.33985000288462,0.961615356248341,5.68650052718322,0.151860078130196,3.23521646169403,3.47111342368493
6.53793560502266,6.18191722113566,0.22857355619096,4.15650448567999,1.37409833905226,6.6724253419715,2.56217009187165
6.5762920314952,6.23950348483245,3.24724397073367,6.6724253419715,0.44097704692723,6.6724253419715,3.07329145835558
6.37503943134692,6.58496250072116,1.75714010730396,6.6724253419715,0.586073961722509,4.55117418726487,2.23982701467247
6.65105169117893,6.64385618977472,0.427024699435811,4.40599235967584,0,0,2.52565130477974
6.65821148275179,6.5077946401987,0.151791425442897,3.00735077738773,1.16938236408651,6.6724253419715,3.55232754028362
6.65574660082487,6.48019825477254,0.0144203523998568,0.381167298262038,1.12719770349153,6.6724253419715,3.40865858671614
6.31469652565629,5.90689059560852,0.231360159795531,2.84789440966024,1.25874190385391,6.6724253419715,2.45028502104998
6.60317399022097,6.40842467620719,0.526534927326013,6.6724253419715,0.390804310993818,6.6724253419715,3.36023535103186
6.44535978192288,6.37105057736694,1.47743351582312,6.6724253419715,0.743898499175355,6.6724253419715,3.29132552848879
6.58496250072116,6.36741475124683,0.258790720577433,4.23446525363702,0.457270211298669,6.6724253419715,2.69044860021908
6.64385618977472,6.64385618977472,0.578756537935093,5.68650052718322,3.12383451079396,6.6724253419715,2.74469486694268
6.36923380966572,6.3497112514009,0.662140896427838,6.6724253419715,0.377926023819276,5.11547721741994,2.35510921773173
6.57098007841195,6.18505895029522,0.865918995809545,6.6724253419715,0.730806578762784,6.6724253419715,3.13141278958914
6.26052755022322,6.18382332293498,0.516421163295025,5.11547721741994,0.951881806054704,6.6724253419715,2.96434425966778
6.64745842645492,6.41362792902417,0.454689347231344,4.90000421002328,1.50441475787984,6.6724253419715,2.9657521782857
6.63161856279228,5.95351840527257,1.1808690463042,6.6724253419715,1.61766367629176,6.6724253419715,3.09079620552381
6.64797229824696,6.63765992381082,0.574460956795509,5.11547721741994,1.74220793892882,5.68650052718322,2.74922901383204
6.56985560833095,6.28540221886225,0.0569442136340646,0.4451852254511,0,0,2.99965646028153
6.64625867999003,6.24792751344359,0.574515962771207,2.38082178394093,0,0,2.87540562884866
6.65571791462358,6.63645601246295,0.332878058177691,6.65122352005175,0.511324454910838,6.6724253419715,3.11494679943014
6.11547721741994,6.5077946401987,0.0183978895063423,0.103129991478833,0.0105083244925423,0.104013778670066,2.75566168787611
6.63765992381082,5.99677609912571,0.333973317811634,2.14526294720804,0,0,3.02974734339405
6.36923380966572,6.13271392234524,2.25294428684232,6.6724253419715,0.100014348491285,0.921288023985959,3.23828379410634
6.64225230407214,6.06847973788277,0.0520185528507252,0.364851742110627,0,0,2.51871614538051
6.28550975026325,5.74694389806387,0.870047039623661,6.6724253419715,0.913230923750931,6.6724253419715,2.98676343599099
5.96962635095648,5.75742969672592,0.315137465932566,3.34792330342031,0.206973594033132,3.34792330342031,2.85766409341495
6.55969574211073,6.53397857200235,0.0273929084992974,0.276027244278754,0,0,2.95109039951905
6.48784003382305,6.36194377373524,0.126600516979932,0.65731334494584,0,0,1.96193195916648

I used the code below for plotting:

library(GGally)
library(ggplot2)
library(RColorBrewer)


cor_fun <- function(data, mapping, method="spearman", ndp=2, sz=5, stars=TRUE){ #ndp is to adjust the number of decimals
  
  x <- eval_data_col(data, mapping$x)
  y <- eval_data_col(data, mapping$y)
  
  corr <- cor.test(x, y, method=method)
  est <- corr$estimate
  lb.size <- sz 
  
  if(stars){
    stars <- c("***", "**", "*", "")[findInterval(corr$p.value, c(0, 0.001, 0.01, 0.05, 1))]
    lbl <- paste0(round(est, ndp), stars)
  }else{
    lbl <- round(est, ndp)
  }
  
  ggplot(data=data, mapping=mapping) +
    annotate("text", x=mean(x, na.rm=TRUE), y=mean(y, na.rm=TRUE), label=lbl, size=lb.size)+
    theme(panel.grid = element_blank(), panel.background=element_rect(fill="snow1")) 
  
}

colfunc<-colorRampPalette(c("#003366","#3399FF","cyan","yellow","red","#990000"))

my_fn <- function(data, mapping){
  p <- ggplot(data = data, mapping = mapping) + 
    stat_density2d(aes(fill=..density..), geom="tile", contour = FALSE) +
    scale_fill_gradientn(colours = colfunc(100)) + theme_classic()
}


ggpairs(out, columns = c(37,38,42,43,44,45,47),
        lower=list(continuous=my_fn),
        diag=list(continuous=wrap("densityDiag", fill="#006666")),
        upper=list(continuous=cor_fun)) + theme(panel.background=element_rect(fill="white")) +
  theme(axis.text.x = element_text(angle = 0, vjust = 1, color = "black")) + 
  theme(axis.text.y = element_text(angle = 0, vjust = 1 , color = "black"))

and my plot looks like this: enter image description here

sessioninfo()

R version 3.6.0 (2019-04-26)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS  10.15.6

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] data.table_1.13.0     gtools_3.8.2          forcats_0.5.0         dplyr_1.0.2          
 [5] purrr_0.3.4           readr_1.3.1           tidyr_1.1.2           tibble_3.0.3         
 [9] tidyverse_1.3.0       corrplot_0.84         GGally_2.0.0          ggplot2_3.3.2        
[13] psych_2.0.12          stringr_1.4.0         ctv_0.8-5             mnormt_2.0.2         
[17] GPArotation_2014.11-1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         lubridate_1.7.9    lattice_0.20-41    prettyunits_1.1.1  digest_0.6.25     
 [6] assertthat_0.2.1   utf8_1.1.4         R6_2.4.1           cellranger_1.1.0   plyr_1.8.6        
[11] backports_1.1.9    reprex_0.3.0       httr_1.4.2         pillar_1.4.6       rlang_0.4.7       
[16] progress_1.2.2     readxl_1.3.1       rstudioapi_0.11    blob_1.2.1         labeling_0.3      
[21] munsell_0.5.0      broom_0.7.0        compiler_3.6.0     modelr_0.1.8       pkgconfig_2.0.3   
[26] tmvnsim_1.0-2      tidyselect_1.1.0   reshape_0.8.8      fansi_0.4.1        crayon_1.3.4      
[31] dbplyr_1.4.4       withr_2.2.0        MASS_7.3-53        grid_3.6.0         nlme_3.1-151      
[36] jsonlite_1.7.1     gtable_0.3.0       lifecycle_0.2.0    DBI_1.1.0          magrittr_1.5      
[41] scales_1.1.1       cli_2.0.2          stringi_1.5.3      farver_2.0.3       fs_1.5.0          
[46] xml2_1.3.2         ellipsis_0.3.1     generics_0.0.2     vctrs_0.3.4        RColorBrewer_1.1-2
[51] tools_3.6.0        glue_1.4.2         hms_0.5.3          parallel_3.6.0     colorspace_1.4-1  
[56] rvest_0.3.6        haven_2.3.1 

Any help is appreciated - thank you.

0

There are 0 best solutions below