Introduction

I will create a raster object using raster() function in raster package, reclassify it into four discrete classes and then calculate area under each class.

Creating a raster object

library(raster)
r <- raster(ncol = 10, nrow =10)
r[] <- sample(1:ncell(r))
plot(r, main = 'Raw Raster')

Reclasify the raster

My raster has values ranging from 1 to 100. So I will breake it into chunks of 1-25, 25-50, 50-75, and 75-100.

r_reclass <- reclassify(r, c(1, 25, 0,
                             25, 50, 1,
                             50, 75, 2,
                             75, 100, 3))
plot(r_reclass, main = 'Reclassified Raster')

Plot Reclassified raster

plot(r_reclass, main = 'Classified Habitat Suitability Map', legend = F,
     col = c('red', 'blue', 'green', 'gray'),
     )
legend('left', legend = c('Unsuitable', 'Moderate', 'Suitable', 'Very suitable'),
       fill = c('red', 'blue', 'green', 'gray'),
       border = FALSE,
       bty = 'n',
       xpd = T,horiz = F,
       inset = c(0.99, 1))

Area under each class

class_area <- tapply(area(r_reclass), r_reclass[], sum)
library(dplyr)
library(ggplot2)
class_df <- as.data.frame(class_area)
class_df |> mutate(class = c('Unsuitable',
                             'Moderate',
                             'Suitable',
                             'Very suitable')) |> ggplot() + geom_col(aes(x = class, y = class_area), fill = c('red', 'blue', 'green', 'gray')) + labs(x = 'Suitability Class', y = 'Class area in km^2') +
  theme_classic()

Tabular area

class_table <- class_df |> mutate(Suitability_class = c('Unsuitable', 'Moderate','Suitable',
'Very suitable')) |> relocate(Suitability_class, .before = class_area)
knitr::kable(class_table, caption = 'Suitability classes by area in km^2')
Suitability classes by area in km^2
Suitability_class class_area
0 Unsuitable 109275078
1 Moderate 151328120
2 Suitable 119265042
3 Very suitable 127890252

References

Hijmans, Robert J. 2021. Raster: Geographic Data Analysis and Modeling. https://rspatial.org/raster.
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. https://ggplot2.tidyverse.org.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2021. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2021. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.
Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2021. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.