Extract values from geom_raster

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`) %>% 
  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() +
  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)

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.

I retraced my steps to the original raster and shapefile, and converted the raster to a stars object. Then I used the st_extract function to extract, in this case, the EFD value for each individual bee observation.


#Read stars raster then extract


ExtractB = st_extract(T1, Guade)

The object ExtractB will have an additional column named EFD_EVI_br_2001.tif with the extracted values. I added this column to the original shapefile named Guade. Finally, I converted the shapefile to a data frame.

#Add extracted EFD values to original shapefile Guade:

Guade$`EFD_EVI_br_2001.tif` = ExtractB$`EFD_EVI_br_2001.tif`

Guade_df<- Guade %>% 


I hope this can be of help for those wanting to extract values for points layered over a raster.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.