I have a data frame (see below) with over 50 000 values, each associated to a position (lat, lon). I would like to calculate the average value for each cell of a 5° latitude x 5° longitude grid in order to create a heat map. The final goal is to plot this grid over a bathymetry map.
I looked at similar questions like this one Average values of a point dataset to a grid dataset. But I couldn't replicate these examples with my own data. Saddly, I am stuck at the first step which is creating the grid.
My data look like this:
library(sp)
library(proj4)
coordinates(data) <- c("lon", "lat")
proj4string(data) <- CRS("+init=epsg:4326") #defined CRS to WGS 84
df<- data.frame(data)
> head(df)
lon lat value
1 -48.1673562 57.71791 822.9
2 -48.7430053 57.83568 1302.3
3 -48.5662663 57.82087 1508.0
4 -48.3252052 58.29815 224.0
5 -47.1716772 58.42417 38.0
6 -46.4098311 58.67651 431.2
7 -45.8071218 58.70022 365.6
8 -45.5558936 58.46975 50.0
Ideally, I would like to plot the grid on a map from the marmap package using ggplot2 (see below):
library(marmap)
library(ggplot2)
atlantic <- getNOAA.bathy(-80, 40, 0, 90, resolution = 25, keep = TRUE)
atl.df <- fortify(atlantic)
map <- ggplot(atl.df, aes(x=x, y=y)) +
geom_raster(aes(fill=z), data=atl.df) +
geom_contour(aes(z=z),
breaks=0, #contour for continent
colour="black", size=1) +
scale_fill_gradientn(values = scales::rescale(c(-5000, 0, 1, 2400)),
colors = c("steelblue4", "#C7E0FF", "gray40", "white"))