Dear community I have two raster files (here r and r2: y-axis) and an equal x-axis (here s, 4 classes). Instead of plotting two boxplots I would like to plot paired boxplots: for each class the boxplots from r and r2 paired). I tried to adapt the code, but I am struggling around with the error:> names(rs) <- c('r', 's', 'r2')Error in `names<-`(`*tmp*`, value = c("r", "s", "r2")) : incorrect number of layer names Additionally I am not sure how to adapt the function boxplot() with two rasters, r and r2 and not only r. Kind regards Sibylle Working example: library(raster) r <- raster(nc=10, nr=5) r[] <- runif(ncell(r), min=10, max=20) * 2 r2 <- raster(nc=10, nr=5) r2[] <- runif(ncell(r2), min=10, max=20) * 2 s <- setValues(r, sample(c(1:4), replace = T, size=50)) rr2s <- stack(r, r2,s) names(rs) <- c('r', 's', 'r2') d <- as.data.frame(rr2s) boxplot(r~s, data= d) [[alternative HTML version deleted]]
? Thu, 22 Aug 2024 08:46:03 +0200 SIBYLLE ST?CKLI via R-help <r-help at r-project.org> ?????:> rr2s <- stack(r, r2,s)> > names(rs) <- c('r', 's', 'r2') > > Error in `names<-`(`*tmp*`, value = c("r", "s", "r2")) : > > incorrect number of layer namesThe error must be happening because the variable named 'rs' in your workspace originates from some other code you have previously run. The code "works" if you replace names(rs) by names(rr2s), but the plot isn't very useful because d$s are real numbers in this example and they don't form groups for d$r. Are you interested in a boxplot where the boxes are grouped by two, one for 'r' and one for 'r2'? I'm sure they are not impossible to produce manually using the boxplot function, but the 'lattice' or 'ggplot2' packages would make them much easier. You will need to reshape your data into long format, with the "value" column for the r/r2 value, the "kind" column saying "r" or "r2", and the "s" column: https://stackoverflow.com/q/20172560 -- Best regards, Ivan