title: Map of bivariate spatial correlation in R (bivariate LISA)
body: <p>I would like to create a map showing the bi-variate spatial correlation between two variables. This could be done either by doing a LISA map of bivariate Moran's I spatial correlation or using the L index proposed by <a href="https://www.researchgate.net/profile/SANG-IL_Lee/publication/220449126_Developing_a_Bivariate_Spatial_Association_Measure_An_Integration_of_Pearson&#39;s_r_and_Moran&#39;s_I/links/558d1a0008aee43bf6ae627f.pdf" rel="nofollow noreferrer">Lee (2001)</a>. </p>
  
  <p>The bi-variate Moran's I is not implemented in the <code>spdep</code> library, but the L index is, so here is what I've tried without success using the L index. A answer showing a solution based on Moran's I would also be very welcomed !</p>
  
  <p>As you can see from the reproducible example below, I've manged so far to calculate the local L indexes. <strong>What I would like to do</strong> is to estimate the pseudo p-values and <a href="https://www.google.co.uk/imgres?imgurl=https%3A%2F%2Fwww.researchgate.net%2Fprofile%2FSparkle_Malone%2Fpublication%2F223176540%2Ffigure%2Ffig6%2FAS%3A305134472056839%401449761135966%2FFig-6-Bivariate-LISA-based-spatial-clusters-showing-the-local-association-between.png&amp;imgrefurl=https%3A%2F%2Fwww.researchgate.net%2Ffigure%2F223176540_fig6_Fig-6-Bivariate-LISA-based-spatial-clusters-showing-the-local-association-between&amp;docid=ZViuC1ezG7GGUM&amp;tbnid=ajQuSn3iSGVw-M%3A&amp;vet=10ahUKEwi014eyk5XVAhVEJ1AKHULZA_4QMwjKASgUMBQ..i&amp;w=814&amp;h=592&amp;safe=active&amp;bih=537&amp;biw=1164&amp;q=%22bivariate%20Lisa%22%20cluster%20map&amp;ved=0ahUKEwi014eyk5XVAhVEJ1AKHULZA_4QMwjKASgUMBQ&amp;iact=mrc&amp;uact=8" rel="nofollow noreferrer">create a map of the results like those maps we use in LISA spatial clusters with high-high, high-low, ..., low-low</a>.</p>
  
  <p>In this example, the goal is to create a map with bi-variate Lisa association between black and white population. The map should be created in <code>ggplot2</code> , showing the clusters: </p>
  
  <ul>
  <li>High-presence of black and High-presence of white people</li>
  <li>High-presence of black and Low-presence of white people</li>
  <li>Low-presence of black and High-presence of white people</li>
  <li>Low-presence of black and Low-presence oh white people</li>
  </ul>
  
  <h3>Reproducible example</h3>
  
  <pre><code>library(UScensus2000tract)
  library(ggplot2)
  library(spdep)
  library(sf)
  
  # load data
    data("oregon.tract")
  
  # plot Census Tract map
    plot(oregon.tract)
  
  
  # Variables to use in the correlation: white and black population in each census track
    x &lt;- scale(oregon.tract$white)
    y &lt;- scale(oregon.tract$black)
  
  
  # create  Queen contiguity matrix and Spatial weights matrix
    nb &lt;- poly2nb(oregon.tract)
    lw &lt;- nb2listw(nb)
  
  
  # Lee index
    Lxy &lt;-lee(x, y, lw, length(x), zero.policy=TRUE)
  
    # Lee’s L statistic (Global)
      Lxy[1]
      #&gt; -0.1865688811
  
  
  # 10k permutations to estimate pseudo p-values
    LMCxy &lt;- lee.mc(x, y, nsim=10000, lw, zero.policy=TRUE, alternative="less")
  
  # quik plot of local L
    Lxy[[2]] %&gt;% density() %&gt;% plot() # Lee’s local L statistic  (Local)
    LMCxy[[7]] %&gt;% density() %&gt;% lines(col="red") # plot values simulated 10k times 
  
  
  # get confidence interval of 95% ( mean +- 2 standard deviations)
    two_sd_above &lt;- mean(LMCxy[[7]]) + 2 * sd(LMCxy[[7]])
    two_sd_below &lt;- mean(LMCxy[[7]]) - 2 * sd(LMCxy[[7]])
  
  
  # convert spatial object to sf class for easier/faster use
    oregon_sf &lt;- st_as_sf(oregon.tract)
  
  
  # add L index values to map object
    oregon_sf$Lindex &lt;- Lxy[[2]]
  
  # identify significant local results  
    oregon_sf$sig &lt;- if_else( oregon_sf$Lindex &lt; 2*two_sd_below, 1, if_else( oregon_sf$Lindex &gt; 2*two_sd_above, 1, 0))
  
  
  # Map of Local L index but only the significant results
    ggplot() + geom_sf(data=oregon_sf, aes(fill=ifelse( sig==T, Lindex, NA)), color=NA)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/qNgAw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qNgAw.png" alt="enter image description here"></a></p>