sibyiie@stoeckii m@iii@g oii gmx@ch
2024-Aug-26 12:33 UTC
[R] boxplot of raster and shapefile
Dear community This example code works library(raster) library(sp) library(rgdal) library(ggplot2) # Create some sample raster data raster_file <- raster(ncol=36, nrow=18) raster_file[] <- 1:ncell(raster_file) plot(raster_file) #Create some sample polygons cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20)) cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0)) shape_file <- SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1), Polygons(list(Polygon(cds2)), 2))) plot(shape_file) # 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) # Create the boxplot bp<-ggplot(data, aes(x = factor(polygon), y = value)) + geom_boxplot() + labs(x = "Polygon", y = "Raster Values") + theme_minimal() bp For my own dataset I encountered problems in reading in the polygons. The error message comes in the boxplot function Here may shape file> # load shape file including all layers and print layers > shape_file<-shapefile("C:/Users/.....BiogeoRegion.shp")Warning message: [vect] Z coordinates ignored> names(shape_file)[1] "RegionNumm" "RegionName" "Unterregio" "Unterreg_1" "ObjNummer" "Version" "Shape_Leng" "Shape_Area" "DERegionNa" "FRRegionNa" [11] "ITRegionNa" "DEBioBedeu" "FRBioBedeu" "ITBioBedeu"> str(shape_file)Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots ..@ data :'data.frame': 12 obs. of 14 variables: .. ..$ RegionNumm: int [1:12] 1 2 2 2 2 3 3 4 5 5 ... .. ..$ RegionName: chr [1:12] "R1" "R2" "R2" "R2" ... .. ..$ Unterregio: int [1:12] 11 21 22 23 24 31 32 41 51 52 ... .. ..$ Unterreg_1: chr [1:12] "U11" "U21" "U22" "U23" ... .. ..$ ObjNummer : chr [1:12] "1" "2" "3" "4" ... .. ..$ Version : chr [1:12] "2020/05/08" "2020/05/08" "2020/05/08" "2020/05/08" ... .. ..$ Shape_Leng: num [1:12] 725117 334364 539746 576810 555541 ... .. ..$ Shape_Area: num [1:12] 4.17e+09 1.11e+09 1.07e+09 4.64e+09 4.47e+09 ... .. ..$ DERegionNa: chr [1:12] "Jura" "Mittelland" "Mittelland" "Mittelland" ... .. ..$ FRRegionNa: chr [1:12] "Jura" "Plateau" "Plateau" "Plateau" ... .. ..$ ITRegionNa: chr [1:12] "Giura" "Altipiano" "Altipiano" "Altipiano" ... .. ..$ DEBioBedeu: chr [1:12] "Jura und Randen" "Genferseegebiet" "Hochrheingebiet" "Westliches Mittelland" ... .. ..$ FRBioBedeu: chr [1:12] "Jura et Randen" "Bassin l?manique" "Bassin rh?nan" "Plateau occidental" ... .. ..$ ITBioBedeu: chr [1:12] "Giura e Randen" "Regione del Lemano" "Regione dell?Alto Reno" "Altipiano occidentale" ... ..@ polygons :List of 12> # select a specific raster file from a list > raster_file<-allrasters_pres[[1]] > names(raster_file)[1] "Andrena.barbilabris_glo_ensemble"> > # Extract raster values within the shapefile > extracted_values <- extract(raster_file, shape_file) > > > > # Assuming the shapefile has multiple polygons and you want to create aboxplot 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()> bpError 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()`).>
? Mon, 26 Aug 2024 14:33:02 +0200 SIBYLLE ST?CKLI via R-help <r-help at 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