# How to plot correlation coefficient vs distance between weather stations

I've looked but didn't find an answer.

I need to make a Correlation Coefficient VS Distance (in Km) plot between 9 weather stations. Something that would look similar to the following figure:

But because of the enormous distance between one station and the others, I need to make this plot with a logarithmic scale in the X axis, so as not to see the rest of the stations stuck together.

To do this, I have a time series of daily minimum temperature on 9 weather stations between the period 1980-2016. The .csv file with the data looks like this:

And the location of all the weather stations is in another .csv file that looks like this:

I know some R, Python and Mathematica so if you can help me in any of these languages that would be nice.

• Oh my. This question is a full-time job. :) You might start by looking into the various ways to estimate distance based from lat/long such as the Haversine formula
– Him
Commented May 7, 2020 at 5:35
• @Scott Nice. But also correlation between what and what else? Not clear to me.
– Carl
Commented May 7, 2020 at 6:58

Here's something to get you started:

First creating some random data that is similar in shape to yours

library(geosphere)
library(ggplot2)

stations <- data.frame(name=LETTERS[1:10], long=rgamma(10, shape=0.1), lat=rgamma(10, shape=0.1))
#name         long          lat
#1     A 1.259977e-07 1.131207e-04
#2     B 6.968209e-03 1.680159e-07
#3     C 2.602584e-13 3.535280e-06
#4     D 1.639016e-02 3.027593e-05
#5     E 2.948261e-06 1.046319e-02
#6     F 3.095077e-10 3.678569e-17
#7     G 3.302161e-01 2.251055e-03
#8     H 9.962861e-11 7.894267e-09
#9     I 1.424795e-04 2.680665e-02
#10    J 1.249873e-02 4.263518e-03

timeSeries <- data.frame(timepoints=1:100, matrix(data=rnorm(1000, mean=20, sd=5), ncol = 10))
timeSeries$$timepoints <- as.factor(timeSeries$$timepoints)

#timepoints       X1       X2       X3       X4       X5       X6       X7       X8
#1          1 17.85895 15.71448 17.13603 21.04133 24.62610 24.51893 22.92991 16.18887
#2          2 17.80604 12.56853 12.78618 15.53018 10.12556 10.25414 16.02324 23.72335
#3          3 19.13857 20.45890 16.47026 19.66117 18.93116 16.95251 23.68975 20.36496
#4          4 24.36578 10.83968 16.88001 18.87606 18.81933 12.95138 18.91245 22.14237
#5          5 18.25209 14.24753 21.08825 31.56626 10.47641 20.14895 18.61570 17.08810
#6          6 16.34040 27.44127 12.77782 28.44285 15.28028 10.53493 26.72544 20.47257


selecting the columns that you want to correlate:

temp <- timeSeries[,2:ncol(timeSeries)]

# matrix correlation of the temperature (assuming that's what you want)
coeffs <- cor(temp, temp)
# select the lower triangle and flatten
coeffs <- as.vector(coeffs[lower.tri(coeffs, diag = FALSE)])


Calculating the distances between stations. There are many other ways to estimate distances (e.g. Haversine as Scott points out in the comments).

# calculate distances between the stations with the geosphere package
dist <- geosphere::distm(as.matrix(stations[2:3]))
dist <- as.vector(dist[lower.tri(dist, diag = FALSE)])


finally make a data frame and plot with log transformation on one axis as requested

corr_dist <- data.frame(coeffs=coeffs, dist=dist)

p <- ggplot(data=corr_dist, aes(x=dist, y=coeffs)) +
geom_point() +
scale_x_continuous(trans='log') +
xlab("dist (log)") +
theme_minimal()


• Thanks you so much! You don't know how grateful I am for your help! Commented May 10, 2020 at 0:56