Dear Ivan
In the meantime I have found a much easier function. As before the code does not work with my own dataset, probably because of my multipolygonal shape file. There is a warning, that z coordinate are ignored. I provided the output of my shapefile. I changed from read_sf to vect() as I have read that this would work better. Probably this warming message drives the error when running e <- extract(r,v): “ cannot find an inherited method for function ‘extract’ for signature ‘’RasterLayer‘, “SpatVector”’ Working example # load libraries library(terra) library(ggplot2) # Load shp and raster from example files of terra (v <- vect(system.file("ex/lux.shp", package="terra"))) (r <- rast(system.file("ex/elev.tif", package="terra"))) plot(r); plot(v, add = TRUE); text(v, labels = "NAME_2", col=c("white")) # extract, include names from vector attributes e <- extract(r,v) e$NAME_2 <- v$NAME_2[e$ID] str(e) # plot ggplot(e, aes(x = NAME_2, y = elevation)) + geom_boxplot() + theme_minimal() + theme(axis.text.x = element_text(angle = 45)) Personal example not working # Load shp file > v <- vect("C:/Users/…._BiogeoRegion.shp") Warning message: [vect] Z coordinates ignored > str(v) S4 class 'SpatVector' [package "terra"] > names(v) [1] "RegionNumm" "RegionName" "Unterregio" "Unterreg_1" "ObjNummer" "Version" "Shape_Leng" "Shape_Area" "DERegionNa" "FRRegionNa" "ITRegionNa" [12] "DEBioBedeu" "FRBioBedeu" "ITBioBedeu" > v class : SpatVector geometry : polygons dimensions : 12, 14 (geometries, attributes) extent : 2485410, 2833842, 1075268, 1295934 (xmin, xmax, ymin, ymax) source : N2020_Revision_BiogeoRegion.shp coord. ref. : CH1903+ / LV95 (EPSG:2056) names : RegionNumm RegionName Unterregio Unterreg_1 ObjNummer Version Shape_Leng Shape_Area DERegionNa FRRegionNa (and 4 more) type : <int> <chr> <int> <chr> <chr> <chr> <num> <num> <chr> <chr> values : 1 R1 11 U11 1 2020/05/08 7.251e+05 4.171e+09 Jura Jura 2 R2 21 U21 2 2020/05/08 3.344e+05 1.112e+09 Mittelland Plateau 2 R2 22 U22 3 2020/05/08 5.397e+05 1.068e+09 Mittelland Plateau # lodad raster files #first import all files in a single folder as a list rastlist_pres <- list.files(path ="C:/Users/….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) r <- allrasters_pres[[1]] > r class : RasterLayer dimensions : 9200, 14400, 132480000 (nrow, ncol, ncell) resolution : 25, 25 (x, y) extent : 2480000, 2840000, 1070000, 1300000 (xmin, xmax, ymin, ymax) crs : +proj=somerc +lat_0=46.9524055555556 +lon_0=7.43958333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs source : Andrena.barbilabris_glo_ensemble.tif names : Andrena.barbilabris_glo_ensemble values : 0, 53 (min, max) > str(r) Formal class 'RasterLayer' [package "raster"] with 13 slots ..@ file :Formal class '.RasterFile' [package "raster"] with 13 slots .. .. ..@ name : chr "C:\\Users\\.......\\Andren"| __truncated__ .. .. ..@ datanotation: chr "INT2U" .. .. ..@ byteorder : chr "little" .. .. ..@ nodatavalue : num -Inf .. .. ..@ NAchanged : logi FALSE .. .. ..@ nbands : int 1 .. .. ..@ bandorder : chr "BIL" .. .. ..@ offset : int 0 .. .. ..@ toptobottom : logi TRUE .. .. ..@ blockrows : Named int 1 .. .. .. ..- attr(*, "names")= chr "rows" .. .. ..@ blockcols : Named int 14400 .. .. .. ..- attr(*, "names")= chr "cols" .. .. ..@ driver : chr "gdal" .. .. ..@ open : logi FALSE ..@ data :Formal class '.SingleLayerData' [package "raster"] with 13 slots .. .. ..@ values : logi(0) .. .. ..@ offset : num 0 .. .. ..@ gain : num 1 .. .. ..@ inmemory : logi FALSE .. .. ..@ fromdisk : logi TRUE .. .. ..@ isfactor : logi FALSE .. .. ..@ attributes: list() .. .. ..@ haveminmax: logi TRUE .. .. ..@ min : num 0 .. .. ..@ max : num 53 .. .. ..@ band : int 1 .. .. ..@ unit : chr "" .. .. ..@ names : chr "Andrena.barbilabris_glo_ensemble" ..@ legend :Formal class '.RasterLegend' [package "raster"] with 5 slots .. .. ..@ type : chr(0) .. .. ..@ values : logi(0) .. .. ..@ color : logi(0) .. .. ..@ names : logi(0) .. .. ..@ colortable: logi(0) ..@ title : chr(0) ..@ extent :Formal class 'Extent' [package "raster"] with 4 slots .. .. ..@ xmin: num 2480000 .. .. ..@ xmax: num 2840000 .. .. ..@ ymin: num 1070000 .. .. ..@ ymax: num 1300000 ..@ rotated : logi FALSE ..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots .. .. ..@ geotrans: num(0) .. .. ..@ transfun:function () ..@ ncols : int 14400 ..@ nrows : int 9200 ..@ crs :Formal class 'CRS' [package "sp"] with 1 slot .. .. ..@ projargs: chr "+proj=somerc +lat_0=46.9524055555556 +lon_0=7.43958333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs" .. .. ..$ comment: chr "PROJCRS[\"unknown\",\n BASEGEOGCRS[\"unknown\",\n DATUM[\"Unknown based on Bessel 1841 ellipsoid\",\n"| __truncated__ ..@ srs : chr "+proj=somerc +lat_0=46.9524055555556 +lon_0=7.43958333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +units=m +no_defs" ..@ history : list() ..@ z : list() > e <- extract(r,v) Error in (function (classes, fdef, mtable) : kann keine vererbte Methode finden für Funktion ‘extract’ für Signatur ‘"RasterLayer", "SpatVector"’ Kind regards Sibylle -----Original Message----- From: Ivan Krylov <ikry...@disroot.org> Sent: Tuesday, August 27, 2024 6:55 PM To: SIBYLLE STÖCKLI via R-help <r-help@r-project.org> Cc: sibylle.stoec...@gmx.ch Subject: Re: [R] boxplot of raster and shapefile В Mon, 26 Aug 2024 14:33:02 +0200 SIBYLLE STÖCKLI via R-help < <mailto:r-help@r-project.org> r-help@r-project.org> пишет: > > # Extract raster values within the shapefile extracted_values <- > > extract(raster_file, shape_file) > > # Assuming the shapefile has multiple polygons and you want to # > > create a boxplot for each data_list <- > > lapply(1:length(extracted_values), function(i) { > + data.frame(value = extracted_values[[i]], polygon = i) > + }) > > data <- do.call(rbind, data_list) > > names(data) > [1] "value" "polygon" > > # Create the boxplot > > bp<-ggplot(data, aes(x = factor(polygon), y = value)) + > + geom_boxplot() + > + labs(x = "Polygon", y = "Raster Values") + > + theme_minimal() > > bp > Error in UseMethod("depth") : > no applicable method for 'depth' applied to an object of class > "NULL" > In addition: Warning message: > Removed 452451 rows containing non-finite outside the scale range > (`stat_boxplot()`). Thank you for providing a runnable example! Could you please also show the output of str(extracted_values) and str(data)? -- Best regards, Ivan [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide https://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.