doon75
doon75

Reputation: 1

how to show raster cell values with tmap?

The code below plots raster cell values. How can I do the same with package tmap?

library(terra)
pai_sim <- rast(ncols=6, nrows=6, 
                xmin=1, xmax=60, 
                ymin=1, ymax=60, 
                res=10) 
values(pai_sim) <- 1 
plot(pai_sim) 
text(pai_sim)

The code below results in errors (incorrect number of dimensions and can´t find object). I have searched using different terms e.g. tmap tm_raster show cell values, but have not found a solution. This is a teaching example so I want to avoid additional steps e.g to not convert to polygon on the fly if at all possible.

library(tmap)
tm_shape(pai_sim) +
tm_raster() + 
  tm_text("lyr.1")

tm_shape(pai_sim) +
tm_raster() + 
  tm_text(lyr.1)

Upvotes: 0

Views: 167

Answers (1)

Rion Lerm
Rion Lerm

Reputation: 29

library(terra)

pai_sim <- rast(ncols=6, nrows=6, 
                xmin=1, xmax=60, 
                ymin=1, ymax=60, 
                res=10) 
values(pai_sim) <- 1

library(tmap)

tm_shape(pai_sim) +
  tm_raster(legend.show = FALSE) +
  tm_layout(
    main.title = "lyr.1")

This removes the legend because the title was a duplicate. To keep the legend, remove the relevant code but ensure the title is different.

Upvotes: 0

Related Questions