0

I have several Rasterstacks (b1_mask, b2_mask,...) of several layers representing only a determined pixel-intensity range. (So point "A" in some layers is "NA" - in others a "value" of that certain range). My goal is to plot (boxplot) 100 random points of this raster stack - to see the variety of that random point in the different layers of my raster stack. I am working in R and my approach would be the following:

setting random points:

library(dismo)

set.seed(1)
random<-randomPoints(b1_mask, n=100)

the next step is difficult for me: As I have 6 spectral bands (b1_mask - b7_mask) my approach was to extract the point values of the 6 bands and save them into a dataframe from where they are plotted as a boxplot. I am not good in for loops so please help me with this part...

for(i in (1:nrow(random))){

  d<-data.frame(extract(b1_mask, random[i,]),
             extract(b2_mask, random[i,]),
             extract(b3_mask, random[i,]),
             extract(b4_mask, random[[i,]]),
             extract(b5_mask, random[[i,]]),
             extract(b7_mask, random[[i,]]),
  )
  par(mfrow=c(4,4)) #plot 16 figures in one plot


  boxplot(d, las=2, main="Reflectance of bands at Point"random[[i]], ylab="Reflectance" xlab ="band 1-7")
}
  • Just for my understanding: do you want the boxplot to include values from all spectral bands per randomly chosen cell or from just one spectral band per cell (i.e., 6 separate boxplots for 6 spectral bands per cell)? – fdetsch Jan 9 '15 at 17:50
  • The boxplot should contain the values from all spectral bands (6) per cell. So all in all I end up with 100 plots - each containing 6 bars (6 spectral bands) and its values... Do you have a hint? – kaltfront Jan 11 '15 at 15:51
1

In R you do not need to use loops for things like this. Please use a reproducible example like the one below to ask questions.

library(dismo)
set.seed(1)
s <- stack(system.file("external/rlogo.grd", package="raster"))
p <- randomPoints(s, 100)
d <- extract(s, p)
boxplot(d)

Or, what I think you want:

boxplot(t(d))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.