0

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:

enter image description here

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")
Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
zarrar
  • 47
  • 7

1 Answers1

-2

An answer using ggplot2 instead of tmap:

library(dplyr)
library(ggplot2)

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))

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)
m4 <- r %>% dplyr::mutate(class1 = "class1_B",
                          class2 = "class2_A",
                          value = value * 6)
m <- bind_rows(m1, m2, m3, m4)
head(m)
#>     lat    lon      value   class1   class2
#> 1 12.25 -72.25 0.07352787 class1_A class2_A
#> 2 12.25 -71.75 0.44638923 class1_A class2_A
#> 3 12.25 -71.25 0.20745736 class1_A class2_A
#> 4 12.25 -70.25 0.48200665 class1_A class2_A
#> 5 12.25 -69.25 0.06922203 class1_A class2_A
#> 6 12.50 -72.25 0.12036249 class1_A class2_A

# probably you should also add + coord_map()
ggplot(m, aes(x = lat, y = lon, fill = value)) + 
  geom_tile() +
  facet_grid(class1~class2) 

Created on 2019-01-31 by the reprex package (v0.2.1)

Jot eN
  • 6,120
  • 4
  • 40
  • 59
  • Thanks! This is great and will work for now. Eventually, I'd like to solve it using tmaps because I add other tmap layers which go under and over this and it would be good to have it in the same format. If I figure it out I'll post it here. – zarrar Jan 31 '19 at 14:05