0

I have a SpatRaster created with the terra package in R, and I have two groupings of points within the larger SpatRaster, which I then separate into groups to observe the groupings with more detail. I use terra::plot to plot the larger SpatRaster and the individual groups, but I want to keep the gradient scale the same for all of the plots. The issue is that the range of values for each of the plots differs, and I haven't been able to figure out how to set the lower and upper limits for the color gradient of each terra::plot to be the same so the colors are consistent.

The ggplot2 package did not work for this data because it is SpatRaster data, thus scale_color_gradient does not work, I have tried a combination of the col argument in terra::plot, such as

plot(DTM_50570, main = "DTM", col = data.frame(from = 0, to = 2000, (terra.pal = terrain.colors(255))))

plot(DTM_50570, main = "DTM", col = data.frame(from = c(0, 1000), to = c(1000, 2000), col = terrain.colors(255)))

plot(DTM_50570, main = "DTM", col = data.frame(from = c(0:10:2550), to = c(0:10:2550), col = terrain.colors(255)))

to no avail.

The data that I am using is in this link: https://tinitaly.pi.ingv.it/Download_Area1_1.html, section W50570.

Here is the code I use for plotting and separating the groups:

library(terra)

DTM_50570_32632 <- rast(paste0("Downloads/w50570_s10/w50570_s10/w50570_s10.tif"))
DTM_50570 <- project(x = DTM_50570_32632, "EPSG:4326")

DTM_longitude <- c(11.80111, 11.80222, 11.80333, 11.95111, 11.95222, 11.95333)
DTM_latitude <-c(45.98333, 45.98777, 45.98555, 45.94111, 45.94888, 45.94333)

group_1_extent <- ext(11.80, 11.81, 45.98, 45.99)
DTM_group_1 <- crop(DTM_50570, group_1_extent)

group_2_extent <- ext(11.95, 11.96, 45.94, 45.95)
DTM_group_2 <- crop(DTM_50570, group_2_extent)

DTM_longitude_group_1 <- c(11.80111, 11.80222, 11.80333)
DTM_latitude_group_1 <-c(45.98333, 45.98777, 45.98555)

DTM_longitude_group_2 <- c(11.95111, 11.95222, 11.95333)
DTM_latitude_group_2 <-c(45.94111, 45.94888, 45.94333)

par(mfrow = c(1, 3))
plot(DTM_50570, main = "DTM")
points(DTM_longitude, DTM_latitude, pch = 20)

plot(DTM_group_1, main = "DTM group 1")
points(DTM_longitude_group_1, DTM_latitude_group_1, pch = 20)

plot(DTM_group_2, main = "DTM group 2")
points(DTM_longitude_group_2, DTM_latitude_group_2, pch = 20)

And the resulting plots (that all have different color gradient scales) DTM all plots

1
  • 1
    It may be that this can be resolved by looking at ?terra::plot range argument, as the colors above are the same, but given the slices of DTM1, DTM2, neither of which have the DTM 1500+ value peak...Well, what I would try as first measure, as overall it seems you want same numbers on each legend. Commented Apr 15 at 13:29

1 Answer 1

4

You can use argument "range". If the rasters have the same geometry, you can also use panel instead of plot.

library(terra)
f <- system.file("ex/elev.tif", package="terra")
r <- rast(f)
x <- c(r/2, r, r*2)
names(x) <- c("A", "B", "C")

plot(x, range=c(70, 1100))
# or plot(x, range=range(minmax(x)))

panel(x, nr=1)

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.