Dear Ivan
Many thanks.
Using my own dataset, my "s" is a layer shape file.
Somewhere I need to define : snew<-s$Unterregio
I tried it to do it before stack(), but it seems to be the wrong way. Do you
have any suggestion?
Code:
> #first import all files in a single folder as a list
> rastlist_pres <- list.files(path
="C:/Users/....._bee_presence", pattern='.tif$', all.files= T,
full.names= T)
> rastlist_RCP85P2 <- list.files(path
="C:/Users/...._bee_RCP85P2", pattern='.tif$', all.files= T,
full.names= T)
>
>
> #import all raster files in folder using lapply
> allrasters_pres <- lapply(rastlist_pres, raster)
> allrasters_RCP85P2 <- lapply(rastlist_RCP85P2, raster)
> sf <- read_sf("C:/Users/....._BiogeoRegion.shp")
> names(sf)
> names(sf)
[1] "RegionNumm" "RegionName" "Unterregio"
"Unterreg_1" "ObjNummer" "Version"
"Shape_Leng" "Shape_Area" "DERegionNa"
"FRRegionNa" "ITRegionNa" "DEBioBedeu"
"FRBioBedeu" "ITBioBedeu"
[15] "geometry"
> s<-sf$Unterregio
> r<-allrasters_pres[[1]]
>
>
> rs <- stack(r, s)
> names(rs) <- c('r', 's')
Error in `names<-`(`*tmp*`, value = c("r", "s")) :
incorrect number of layer names
-----Original Message-----
From: Ivan Krylov <ikrylov at disroot.org>
Sent: Thursday, August 22, 2024 4:50 PM
To: SIBYLLE ST?CKLI via R-help <r-help at r-project.org>
Cc: sibylle.stoeckli at gmx.ch
Subject: Re: [R] paired raster boxplots
? 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 names
The 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