I would like to create a faceted map using my raster data using the tmap package. The end result will be multiple raster maps showing "values", with rows showing "class1" maps and columns showing "class2" maps as shown in the example figure below:
I have tried multiple approaches without success. I think the issue is building the raster brick layer in the correct format. Would appreciate any advice.
library(tmap)
library(dplyr)
library(magrittr)
library(raster)
r<-data.frame(lat=c(rep(12.25,5),rep(12.5,5)),
lon=c(rep(c(-72.25,-71.75,-71.25,-70.25,-69.25),2)),
value=runif(10));r
rx<-sp::SpatialPointsDataFrame(sp::SpatialPoints(coords=
(cbind(r$lon,r$lat))),data=r%>%dplyr::select(value))
rx@data
sp::gridded(rx)<-T
rx<-raster(rx);rx
r
tm_shape(rx)+tm_raster()
# This works and shows one of the raster maps I would like.
m1<-r%>%dplyr::mutate(class1="class1_A",class2="class2_A",value=value*0.5)
m2<-r%>%dplyr::mutate(class1="class1_A",class2="class2_B",value=value*3)
m3<-r%>%dplyr::mutate(class1="class1_B",class2="class2_B",value=value*10)
m<-bind_rows(m1,m2,m3);m
mx<-sp::SpatialPointsDataFrame(sp::SpatialPoints(coords=
(cbind(m$lon,m$lat))),data=m)
sp::gridded(mx)<-T
mx@data
# mx@data shows the data.frame with the lat/lon and the classes.
# I would like to plot this as facets
mxr<-brick(mx)
tm_shape(mxr)+tm_raster(col="value")+tm_facets(by="class1","class2")