Reputation: 11
I'm working with a dataset that contains coordinates and fishing effort (HF).
Here is a sample of my dataset. The real dataset has 2985412 rows.
Year Month Type SI_LONG SI_LATI HF
2008 04 OTB_DEF_>=70_0 -4.88 46.84 0.617
2008 04 OTB_DEF_>=70_0 -4.91 46.83 0.4
2008 04 OTB_DEF_>=70_0 -4.96 46.85 1
2008 04 OTB_DEF_>=70_0 -5.01 46.84 1.02
2008 04 OTB_DEF_>=70_0 -5.01 46.92 1
2008 04 OTB_DEF_>=70_0 -4.95 46.98 1
2008 04 OTB_DEF_>=70_0 -4.92 46.96 1.02
2008 04 OTB_DEF_>=70_0 -4.97 46.93 1.02
2008 04 OTB_DEF_>=70_0 -4.96 46.97 0.167
2008 04 OTB_DEF_>=70_0 -5 46.94 0.833
As there are a lot of points, I would like to aggregate my data on a grid with a resolution of 0.05 so I can map my data.
The end result should be something that looks like this but with way less points and a pixel regrouping the mean of several points that are adjacent.
Expected visualization
Here is what I tried for now but it does not give me quite what I want. HF_filter is the name of the dataframe that I presented.
# Map and grid limits
grid_xmin <- -6
grid_xmax <- 0
grid_ymin <- 42
grid_ymax <- 48
grid_limit <- extent(c(grid_xmin,grid_xmax,grid_ymin,grid_ymax))
# Resolution of grid
grid <- raster(grid_limit)
res(grid) <- 0.05
crs(grid) <- "+proj=longlat +datum=WGS84"
# Create grid polygon
gridpolygon <- rasterToPolygons(grid)
gridpolygon$layer <- c(1:length(gridpolygon$layer))
gridpolygon_sf <- st_as_sf(gridpolygon)
#Transform to sf object
points_sf <- st_as_sf(HF_filter[,c("SI_LONG","SI_LATI","HF","LE_MET_level6","Year","Month")],
coords = c("SI_LONG","SI_LATI"),crs="+proj=longlat +datum=WGS84")
points_sf <- points_sf[st_intersects(points_sf,gridpolygon_sf) %>% lengths > 0,]
points_df <- st_join(points_sf,gridpolygon_sf) %>%
as.data.frame %>%
dplyr::select(-geometry)
## Aggregate at the cell level
polyg_sf <- inner_join(gridpolygon_sf,points_df)%>%
group_by(layer) %>%
dplyr::summarise(fishing_effort = mean(HF))
#Plot
ggplot() +
geom_sf(data=polyg_sf,aes(fill = fishing_effort,col=fishing_effort))+
#geom_sf(data=points_sf,size=0.1)+
geom_sf(data=mapBase)+
coord_sf(xlim = c(-6,0), ylim = c(43,48), expand = FALSE)+
scale_fill_distiller(palette = "Spectral")+
scale_color_distiller(palette = "Spectral")
It gives me something close to what I want except, all pixels are not the same size or some are on top of each other. I would like something a little more "organized".
Current result
What would you suggest?
Upvotes: 1
Views: 53