Stat_Density func in R filling in multiple grid squares instead of a single one

37 views Asked by At

I am trying to highlight single 1x1 degree grid squares on a map.

It works highlighting individual grid squares for the first 1 squares but after highlight the 4th square it begins to highlight multiple groups of squares and I am not sure why?


library(ggOceanMaps)
#devtools::install_github("MikkoVihtakari/ggOceanMapsData")
library(ggOceanMapsData)

dt <- data.frame(lon = c(35, 35, 60, 60), lat = c(-25, -25, -40, -40))

grid_2019_1 <- data.frame(lat=c(-28, -29), long=c(51, 52))
grid_2019_2 <- data.frame(lat=c(-28, -29), long=c(52, 53))
grid_2019_3 <- data.frame(lat=c(-28, -29), long=c(53, 54))
grid_2019_4 <- data.frame(lat=c(-30, -31), long=c(41, 42))
grid_2019_5 <- data.frame(lat=c(-30, -31), long=c(42, 43))

P4 = basemap(data = dt,bathymetry = T,
        lon.interval = 1,
        lat.interval = 1,
        bathy.style = "contour_blues",
        bathy.border.col = NA,
        bathy.size = 0.1,
        bathy.alpha = 1) 

P4.1 = P4 + stat_density2d(data = grid_2019_1, aes(x = long, y = lat, fill = ..density..),
                    geom = 'tile', contour = F) 
P4.2 = P4.1 + stat_density2d(data = grid_2019_2, aes(x = long, y = lat, fill = ..density..),
                           geom = 'tile', contour = F) 
P4.3 = P4.2 + stat_density2d(data = grid_2019_3, aes(x = long, y = lat, fill = ..density..),
                           geom = 'tile', contour = F) 

P4.4 = P4.3 + stat_density2d(data = grid_2019_4, aes(x = long, y = lat, fill = ..density..),
                           geom = 'tile', contour = F) 
P4.5 = P4.4 + stat_density2d(data = grid_2019_5, aes(x = long, y = lat, fill = ..density..),
                           geom = 'tile', contour = F) 
1

There are 1 answers

0
J_S_Moss On

Fixed using geom_tile


P4 = basemap(data = dt,bathymetry = T,
        lon.interval = 1,
        lat.interval = 1,
        bathy.style = "contour_blues",
        bathy.border.col = NA,
        bathy.size = 0.1,
        bathy.alpha = 1) 
grid_2019_1 <- data.frame(lat=c(-28.5), long=c(51.5))
grid_2019_8 <- data.frame(lat=c(-31.5), long=c(42.5))
P4.1 = P4 +  geom_tile(data = grid_2019_8, aes(x= long, y = lat, fill= 'red' )) 
P4.1
P4.8 = P4 +   geom_tile(data = grid_2019_8, aes(x= long, y = lat, fill= 'red' ))
P4.8