R - World Contour Map with Correlation between two data sets

1.1k views Asked by At

I have two data sets A and B and i wanna to find the correlation and plot the contour map.

A is just a simple vector with 230 stream flow data.

B is a complicated sea surface temperature(SST) data under a series date. On each date, the SST has a matrix of 360row *180columns of recorded temperatures.

The vector A (230 data) is :

Houlgrave_flow_1981_2000 = window(Houlgrave_flow_average, start = as.Date("1981-11-15"),end = as.Date("2000-12-15")) 
Houlgrave_SF_1981_2000 = coredata(Houlgrave_flow_1981_2000)

The dimension of matrix B is shown below and i only use from 1 to 230.

> dim(ssta_sst)
[1] 360 180 362

My idea for finding correlation is below.

z_correlation = cor(Houlgrave_SF_SST_1981_2000,ssta_sst[c(181:360, 1:180),,i])

Try, i=1. However, it doesn't work.The error message says:

"Error in cor(Houlgrave_SF_SST_1981_2000, ssta_sst[c(181:360, 1:180), ,  : 
  incompatible dimensions.".

Also, this is my contour map code,

require(maps)
par(ask=TRUE)
for (i in 1:230) {
    maps::map(database="world", fill=TRUE, col="light blue")
    maps::map.axes()
    contour(x=lon_sst, y=lat_sst, z=cor(Houlgrave_SF_1981_2000,ssta_sst[c(181:360, 1:180),,i]), zlim=c(-3,3), add=TRUE)
    title(paste("Year: ", year_sst[i], ", Month: ", month_sst[i]))
}

I think i just need to modify z under contour code. Is it necessary to redefine each A's data as a 360*180 data matrix?

1

There are 1 answers

1
Vincent Zoonekynd On BEST ANSWER

If I understand the problem correctly, you have a time series, i.e., a vector whose index can be interpreted as time, and a 3-dimensional array, whose indices can be interpreted as time and position.

# Sample data
n <- 230
m <- 100
dates <- seq.Date( from=Sys.Date(), length=n, by="day" )
flow <- rnorm(n)
names(flow) <- as.character(dates)
temperatures <- array( rlnorm(n*m*m), dim=c(n,m,m) )
dimnames( temperatures ) <- list(
  time = as.character( dates ),
  longitude = NULL,
  latitude = NULL
)

For each position, you can compute the correlation between your "flow" time series and the "temperature" time series (u, in the code below) for that position, using apply.

correlations <- apply( 
  temperatures, 
  2:3, 
  function (u) cor(u, flow) 
)
image(correlations)