0

I am trying to create a side-by-side of two spatial surfaces (each have the same grid locations). Following the example provided here, I was able to produce the plot below on a toy example. However, I can't seem to figure out how to change the facet labels such that they contain two words (e.g., "Predicted Surface"). I would also like the color scale to be continuous and on the right hand side. Any help would be much appreciated as well as other advice in making my plot prettier! (I don't have to use spplot, it just seemed easiest to me).

library(fields)
set.seed(123)
n=5000
print(n)
gridLocation<-cbind(runif(n,min = 0,max = 5),runif(n,min = 0,max = 5))
distMat<-as.matrix(rdist(gridLocation)) # Distance matrix
expCov<-function(distMat,phi){
  exp(-distMat/phi)
}
CovMat <- expCov(distMat,phi=1)
W1 <- as.numeric(t(chol(CovMat))%*%rnorm(n))
W2 <- W1+rnorm(n)

data <- data.frame(x=gridLocation[,1],y=gridLocation[,2], W1=W1, W2=W2)
coordinates(data) <- c("x", "y") # tells r to use x and y collumns as the lat/long 
spplot(data, c("W1","W2")) # easy way to plot side by side maps

enter image description here

1 Answer 1

1

Your code isn't reproducible (where does rdist come from?) but its not necessary, a simple example can be constructed much more easily:

> data = data.frame(x=runif(100), y=runif(100), W1=runif(100), W2=runif(100))
> coordinates(data) = ~x+y

Now I can plot it with W1 and W2 labels:

> spplot(data, names(data)) # easy way to plot side by side ma

There's no reason why you can't give column names two-word values, its perfectly valid R, although it elicits a warning:

> names(data)[1] = "First Item"
Warning message:
In checkNames(value) :
  attempt to set invalid names: this may lead to problems later on. See ?make.names

But you can still get at the data by name:

> head(data[["First Item"]])
[1] 0.26261320 0.03832961 0.30458142 0.67045738 0.82739282 0.70987561

Or even using $-extraction if you protect the name in backticks:

> head(data$`First Item`)
[1] 0.26261320 0.03832961 0.30458142 0.67045738 0.82739282 0.70987561

But spplot doesn't like it one little bit:

> spplot(data, names(data)) # easy way to plot side by side ma
Error in create.z(as(obj, "data.frame"), zcol) : 
  no support for variable of this type

Oh well. So then I read the documentation for spplot and discovered the names.attr parameter:

spplot(data, names(data), names.attr=c("First Thing","Second thing"))

enter image description here

Moral of the story: I should read the documentation first.

Note the sp package and friends are retiring soon, so you should really look into the sf package and its friends for work from now on. Any tutorials with classes like SpatialPoints are likely to start failing in new and mysterious ways from now on... Check out Geocomputation in R for the modern way. https://r.geocompx.org/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.