0

I'm looking for an R implementation of the ESRI 'Slice' tool, specifically I want to use the 'EQUAL_AREA' option.

I want to use an input raster, and reclassify raster values into 9 'bins', based on (approximately) the number of cells within each bin.

My raster has values between 0 and 50,000 that covers a very large geographical area. So for example values between 0 and 5000 might become '1', values between 5000 and 6000 might become '2' and so on. Depending on how many values/cells there are in each category.

Thanks.

Majid
  • 1,836
  • 9
  • 19
TheRealJimShady
  • 777
  • 3
  • 9
  • 24

2 Answers2

2

There is no such a package as far as I know, but you can use classInt and raster package to do what you are looking for! Although you need to come up with a reproducible example to get the best result, I think below script does the job:

# sample data
data(volcano)
volcanoR <- raster(volcano)

# required libraries 
library(classInt)
library(raster)


n = 9 # this is number of classes
zClass <- classIntervals(values(volcanoR), n=n, style="jenks")
# chosen style: one of "fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust",
# "bclust", "fisher", "jenks" or "dpih"


# classes for reclassification based on NBJ
df.rcl <- data.frame(zClass$brks[1:(length(zClass$brks)-1)], 
                     zClass$brks[2:(length(zClass$brks))],
                     seq(1,length(zClass$brks)-1,1))


rec.ras <- reclassify(volcanoR, df.rcl, include.lowest=TRUE)
plot(rec.ras, col=terrain.colors(n, alpha=1, rev=T), legend=F, main="NBJ")
legend("topleft", legend = c(seq(1,length(zClass$brks)-1,1)),
       fill = terrain.colors(n, alpha = 1, rev = T), cex=0.85, bty = "n")

enter image description here

Same approach for equal interval classes:

zClass <- classIntervals(values(volcanoR), n=n, style="equal")
# chosen style: one of "fixed", "sd", "equal", "pretty", "quantile", "kmeans", "hclust",
# "bclust", "fisher", "jenks" or "dpih"

# classes for reclassification based on EQUAL INTERVAL
df.rcl <- data.frame(zClass$brks[1:(length(zClass$brks)-1)], 
                     zClass$brks[2:(length(zClass$brks))],
                     seq(1,length(zClass$brks)-1,1))


rec.ras <- reclassify(volcanoR, df.rcl, include.lowest=TRUE)
plot(rec.ras, col=terrain.colors(n, alpha=1, rev=T), legend=F, main="Equal Interval")
legend("topleft", legend = c(seq(1,length(zClass$brks)-1,1)),
       fill = terrain.colors(n, alpha = 1, rev = T), cex=0.85, bty = "n")

enter image description here

Majid
  • 1,836
  • 9
  • 19
2
library(raster)
data(volcano)
v <- raster(volcano)

Use quantile with sampleRegular (for very large rasters)

s <- seq(0, 1, 1/9)
q <- quantile(sampleRegular(v,500000), s)
x <- cut(v, q)  # like reclassify

Check the results

plot(x)
table(values(x))
#  1   2   3   4   5   6   7   8   9 
#597 582 547 562 613 575 592 556 632 
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63