Extract values from geom_raster

Hi,
I created a plot using geom_raster and geom_sf. The raster contains the EFT values for each pixel per year, and the shape file contains a list of species with coordinates. I would like to extract the values from the raster that corresponds to each species located in the shape file. The goal is to have a table with the list of species, coordinates, Year, and EFT value. Is this possible?


Guade<-st_read("~/iCloud Drive (Archive)/Documents/RStudio/FinalResearch/Diseno/Guadeloupe_Networks/Scripts/Guadeloupe.shp") %>% 
  mutate(Species = str_replace(Species, "_", " "))



Test<- Stacked_EFT %>% 
  select(x, y, `2001`) %>% 
ggplot()+
  geom_raster(aes(x = x, y = y, fill = `2001`))+
  scale_fill_continuous_sequential(name="EFT", palette="YlGnBu")+
   labs(title = "2001")+
  theme(plot.title = element_text(size=20, face= "bold", hjust = 0.5), legend.position = "bottom")+
    theme_void() +
  coord_equal()+
  geom_sf(data = Guade, aes(color = Species))

Example Data

Thank you

Data site is password protected.

1 Like

Thank you for noticing, I've edited the access settings again. Let me know if it still can't be accessed.

1 Like

Thanks, it's accessible, but I can't read the shapefile. It's extremely small, so it might have been corrupted? Is this Guadeloupe Island, Mexico in the Pacific? Can you point me to a shapefile?

1 Like

Hi, I edited the shapefile and data frame so they'll be larger. Hopefully the shapefile can be read now. The shapefile contains coordinates of bee species found in Guadeloupe Island in the Caribbean. Thank you.

Are these EFT data? Couldn't tell.

> Test$data$`2001`
 [1] 2 2 1 1 1 1 1 2 3 3 5 1 1 2 2 3 3 7 7 3 2 1 1 3 5 5 5 2 3 1 1 3 7 5 1 1 2 1 1 1 5 7 5 7 3
[46] 1 1 3 1 1
>

Yes, the values range from 1-8

Then Test$data$[year enclosed in back ticks] holds the EFT data.

1 Like

Yes, that is the data. I tried using ggbuild to try and see if I could export a table with the species names that are in the shapefile and the value of the pixel where the species is found (value of the raster).

DG <-ggplot_build(Test)
DG[[1]]

But I believe it gives a column with the color that represents the species, but it doesn't give a column with the value of the pixel the species is located at.