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.
library(raster)
r <- raster(ncol = 10, nrow =10)
r[] <- sample(1:ncell(r))
plot(r, main = 'Raw 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(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))
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()
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_class | class_area | |
---|---|---|
0 | Unsuitable | 109275078 |
1 | Moderate | 151328120 |
2 | Suitable | 119265042 |
3 | Very suitable | 127890252 |