For loops + boxplot for raster?

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)
``````

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))){

)
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)? Jan 9, 2015 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? Jan 11, 2015 at 15:51

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))
``````