admin管理员组

文章数量:1131675

I have a terra::rast() stack of continuous data, which I classify to n classes, convert to factors and plot on a discrete scale.

But when the first raster does not have all the factors present that some of the other rasters do (e.g. only 3 classification values instead of the whole set of 10, converted to factors), the plot goes a bit haywire.

I am looking for a way to inform the rast stack (or plot) that the levels of the whole stack are min - max of the the stack, but some layers may have less.

Interestingly, if the 1st layer has all classes/factors possible then the subsequent ones are ok and the plot is fine.

# dummy data
library(terra)
r1 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r1[] <- runif(ncell(r1), min = 1, max = 5)  # Random values between 1 and 5

r2 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r2[] <- runif(ncell(r2), min = 1, max = 5)

# Combine rasters into a stack
s <- c(r1/r1, r1/r2, r2/r1, r2/r2)
names(s) <- c("r1/r1", "r1/r2", "r2/r1", "r2/r2")

# Reclassify the raster stack
# Define reclassification matrix
m_rc <- matrix(c(0, 0.5, 1, 
                           0.5, 0.9, 2, 
                           0.9, 1.1, 3,
                           1.1, 2, 4,
                           2, max(global(s, max, na.rm=T)$max), 5), 
                         ncol = 3, byrow = TRUE)

# Apply reclassification
s_r <- classify(s, m_rc)

# Convert reclassified raster to factor for categorical plotting
s_r_f <- as.factor(s_r)

# Step 3: Plot using ggplot2 and tidyterra with custom legend labels
ggplot() +
  geom_spatraster(data = s_r_f) +
  facet_wrap(~lyr, nrow = 2) +  # Separate plots for each layer
  scale_fill_manual(
    values = c("blue","lightblue" , "white", "yellow", "red"),  # Assign custom colors
    na.value = "transparent",    # Transparent for NA values
    ) +
  labs(
    title = "Reclassified Raster Stack",
    labels = c("0 - 0.5","0.5 - 0.9","0.9 - 1.1","1.1 - 2","> 2"),
    fill = "Class"
  ) +
  theme_minimal()

I have a terra::rast() stack of continuous data, which I classify to n classes, convert to factors and plot on a discrete scale.

But when the first raster does not have all the factors present that some of the other rasters do (e.g. only 3 classification values instead of the whole set of 10, converted to factors), the plot goes a bit haywire.

I am looking for a way to inform the rast stack (or plot) that the levels of the whole stack are min - max of the the stack, but some layers may have less.

Interestingly, if the 1st layer has all classes/factors possible then the subsequent ones are ok and the plot is fine.

# dummy data
library(terra)
r1 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r1[] <- runif(ncell(r1), min = 1, max = 5)  # Random values between 1 and 5

r2 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r2[] <- runif(ncell(r2), min = 1, max = 5)

# Combine rasters into a stack
s <- c(r1/r1, r1/r2, r2/r1, r2/r2)
names(s) <- c("r1/r1", "r1/r2", "r2/r1", "r2/r2")

# Reclassify the raster stack
# Define reclassification matrix
m_rc <- matrix(c(0, 0.5, 1, 
                           0.5, 0.9, 2, 
                           0.9, 1.1, 3,
                           1.1, 2, 4,
                           2, max(global(s, max, na.rm=T)$max), 5), 
                         ncol = 3, byrow = TRUE)

# Apply reclassification
s_r <- classify(s, m_rc)

# Convert reclassified raster to factor for categorical plotting
s_r_f <- as.factor(s_r)

# Step 3: Plot using ggplot2 and tidyterra with custom legend labels
ggplot() +
  geom_spatraster(data = s_r_f) +
  facet_wrap(~lyr, nrow = 2) +  # Separate plots for each layer
  scale_fill_manual(
    values = c("blue","lightblue" , "white", "yellow", "red"),  # Assign custom colors
    na.value = "transparent",    # Transparent for NA values
    ) +
  labs(
    title = "Reclassified Raster Stack",
    labels = c("0 - 0.5","0.5 - 0.9","0.9 - 1.1","1.1 - 2","> 2"),
    fill = "Class"
  ) +
  theme_minimal()
Share Improve this question edited Jan 8 at 19:21 Robert Hijmans 46.6k4 gold badges59 silver badges72 bronze badges asked Jan 8 at 17:09 SamSam 1,46215 silver badges31 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

That should be improved a bit in the package, but right now you can do this:

# set the same levels to all layers
x <- categories(s_r_f, 0, levels(s_r_f)[[2]])

panel(x, all_levels=TRUE, col = c("blue","lightblue", "white", "yellow", "red"), main=1:4)


With the development version of terra (version 1.8-9) you can now do

panel(s_r_f, col = c("blue","lightblue", "white", "yellow", "red"))

And for use with tidyterra and ggplot you can first use combineLevels

x <- combineLevels(s_r_f)

library(tidyterra)
library(ggplot2)

ggplot() +
  geom_spatraster(data = x) +
  facet_wrap(~lyr, nrow = 2) +  # Separate plots for each layer
  scale_fill_manual(
    values = c("blue","lightblue" , "white", "yellow", "red"),  # Assign custom colors
    na.value = "transparent",    # Transparent for NA values
    ) +
  labs(
    title = "Reclassified Raster Stack",
    labels = c("0 - 0.5","0.5 - 0.9","0.9 - 1.1","1.1 - 2","> 2"),
    fill = "Class"
  ) +
  theme_minimal()

On top of Robert's solution, that is perfectly fine, see how this can be controlled with some options of ggplot (see also ggplot2 stacked bar not ordering by manually-defined factor order).

Basically, inside scale_fill_manual you can assign manually the color with a given value using a pair value-color in values and change the default oder using breaks. I also recoded the values on the legend, as I assumed that was your intention with this line:

    labels = c("0 - 0.5","0.5 - 0.9","0.9 - 1.1","1.1 - 2","> 2"),

See the full reprex:

# dummy data
library(terra)
#> terra 1.8.5
library(ggplot2)
library(tidyterra)
#> 
#> Attaching package: 'tidyterra'
#> The following object is masked from 'package:stats':
#> 
#>     filter

set.seed(1234)

r1 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r1[] <- runif(ncell(r1), min = 1, max = 5) # Random values between 1 and 5

r2 <- rast(nrows = 10, ncols = 10, xmin = 0, xmax = 10, ymin = 0, ymax = 10)
r2[] <- runif(ncell(r2), min = 1, max = 5)

# Combine rasters into a stack
s <- c(r1 / r1, r1 / r2, r2 / r1, r2 / r2)
names(s) <- c("r1/r1", "r1/r2", "r2/r1", "r2/r2")

# Reclassify the raster stack
# Define reclassification matrix
m_rc <- matrix(
  c(
    0, 0.5, 1,
    0.5, 0.9, 2,
    0.9, 1.1, 3,
    1.1, 2, 4,
    2, max(global(s, max, na.rm = T)$max), 5
  ),
  ncol = 3, byrow = TRUE
)

# Apply reclassification
s_r <- classify(s, m_rc)

# Convert reclassified raster to factor for categorical plotting
s_r_f <- as.factor(s_r)



# Step 3: Plot using ggplot2 and tidyterra with custom legend labels
ggplot() +
  geom_spatraster(data = s_r_f) +
  facet_wrap(~lyr, nrow = 2) + # Separate plots for each layer
  scale_fill_manual(
    # Step 1: Identify color with values by name
    values = c(
      "1" = "blue", "2" = "lightblue", "3" = "white",
      "4" = "yellow", "5" = "red"
    ), # Assign custom colors

    # Step 2: Order the values in legend
    breaks = c("1", "2", "3", "4", "5"),
    # Step 3 (extra) Rename values in legend
    labels = c("0 - 0.5", "0.5 - 0.9", "0.9 - 1.1", "1.1 - 2", "> 2"),
    na.value = "transparent", # Transparent for NA values
  ) +
  labs(
    title = "Reclassified Raster Stack",
    fill = "Class"
  ) +
  theme_minimal()

Plot Robert's answer for comparison (same plot):


# For comparison with Robert's answer
# set the same levels to all layers
x <- categories(s_r_f, layer = 0, value = levels(s_r_f)[[2]])

panel(x, all_levels = TRUE, col = c(
  "blue", "lightblue", "white",
  "yellow", "red"
), main = 1:4)

Created on 2025-01-09 with reprex v2.1.1

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.4.2 (2024-10-31)
#>  os       Ubuntu 20.04.6 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  C.UTF-8
#>  ctype    C.UTF-8
#>  tz       UTC
#>  date     2025-01-09
#>  pandoc   3.1.11 @ /usr/lib/rstudio-server/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version  date (UTC) lib source
#>  class         7.3-22   2023-05-03 [2] CRAN (R 4.4.1)
#>  classInt      0.4-10   2023-09-05 [1] RSPM (R 4.4.0)
#>  cli           3.6.3    2024-06-21 [1] RSPM (R 4.4.0)
#>  codetools     0.2-20   2024-03-31 [2] CRAN (R 4.4.1)
#>  colorspace    2.1-1    2024-07-26 [1] RSPM (R 4.4.0)
#>  curl          6.1.0    2025-01-06 [1] RSPM (R 4.4.0)
#>  data.table    1.16.4   2024-12-06 [1] RSPM (R 4.4.0)
#>  DBI           1.2.3    2024-06-02 [1] RSPM (R 4.4.0)
#>  digest        0.6.37   2024-08-19 [1] RSPM (R 4.4.0)
#>  dplyr         1.1.4    2023-11-17 [1] RSPM (R 4.4.0)
#>  e1071         1.7-16   2024-09-16 [1] RSPM (R 4.4.0)
#>  evaluate      1.0.1    2024-10-10 [1] RSPM (R 4.4.0)
#>  farver        2.1.2    2024-05-13 [1] RSPM (R 4.4.0)
#>  fastmap       1.2.0    2024-05-15 [1] RSPM (R 4.4.0)
#>  fs            1.6.5    2024-10-30 [1] RSPM (R 4.4.0)
#>  generics      0.1.3    2022-07-05 [1] RSPM (R 4.4.0)
#>  ggplot2     * 3.5.1    2024-04-23 [1] RSPM (R 4.4.0)
#>  glue          1.8.0    2024-09-30 [1] RSPM (R 4.4.0)
#>  gtable        0.3.6    2024-10-25 [1] RSPM (R 4.4.0)
#>  htmltools     0.5.8.1  2024-04-04 [1] RSPM (R 4.4.0)
#>  KernSmooth    2.23-24  2024-05-17 [2] CRAN (R 4.4.1)
#>  knitr         1.49     2024-11-08 [1] RSPM (R 4.4.0)
#>  lifecycle     1.0.4    2023-11-07 [1] RSPM (R 4.4.0)
#>  magrittr      2.0.3    2022-03-30 [1] RSPM (R 4.4.0)
#>  munsell       0.5.1    2024-04-01 [1] RSPM (R 4.4.0)
#>  pillar        1.10.1   2025-01-07 [1] RSPM (R 4.4.0)
#>  pkgconfig     2.0.3    2019-09-22 [1] RSPM (R 4.4.0)
#>  proxy         0.4-27   2022-06-09 [1] RSPM (R 4.4.0)
#>  purrr         1.0.2    2023-08-10 [1] RSPM (R 4.4.0)
#>  R6            2.5.1    2021-08-19 [1] RSPM (R 4.4.0)
#>  Rcpp          1.0.13-1 2024-11-02 [1] RSPM (R 4.4.0)
#>  reprex        2.1.1    2024-07-06 [1] RSPM (R 4.4.0)
#>  rlang         1.1.4    2024-06-04 [1] RSPM (R 4.4.0)
#>  rmarkdown     2.29     2024-11-04 [1] RSPM (R 4.4.0)
#>  rstudioapi    0.17.1   2024-10-22 [1] RSPM (R 4.4.0)
#>  scales        1.3.0    2023-11-28 [1] RSPM (R 4.4.0)
#>  sessioninfo   1.2.2    2021-12-06 [1] RSPM (R 4.4.0)
#>  sf            1.0-19   2024-11-05 [1] RSPM (R 4.4.0)
#>  terra       * 1.8-5    2024-12-12 [1] RSPM (R 4.4.0)
#>  tibble        3.2.1    2023-03-20 [1] RSPM (R 4.4.0)
#>  tidyr         1.3.1    2024-01-24 [1] RSPM (R 4.4.0)
#>  tidyselect    1.2.1    2024-03-11 [1] RSPM (R 4.4.0)
#>  tidyterra   * 0.6.1    2024-06-08 [1] RSPM (R 4.4.0)
#>  units         0.8-5    2023-11-28 [1] RSPM (R 4.4.0)
#>  vctrs         0.6.5    2023-12-01 [1] RSPM (R 4.4.0)
#>  withr         3.0.2    2024-10-28 [1] RSPM (R 4.4.0)
#>  xfun          0.50     2025-01-07 [1] RSPM (R 4.4.0)
#>  xml2          1.3.6    2023-12-04 [1] RSPM (R 4.4.0)
#>  yaml          2.3.10   2024-07-26 [1] RSPM (R 4.4.0)
#> 
#>  [1] /cloud/lib/x86_64-pc-linux-gnu-library/4.4
#>  [2] /opt/R/4.4.1/lib/R/library
#>  [3] /opt/R/4.4.2/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

本文标签: