dggridR
dggridR copied to clipboard
dgearthgrid merge with binned data gives holes etc.
Instead of selecting just grid cells with data using dgcellstogrid(), I would like to display the full grid, including cells with NA.
I tried doing this by replacing dgcellstogrid
with dggearthgrid
and then merging using merge(all=TRUE)
but the result is a messed up grid.
Reproducible example using your vignette code:
library(dggridR)
library(dplyr)
dggs <- dgconstruct(spacing=1000, metric=FALSE, resround='down')
data(dgquakes)
dgquakes$cell <- dgGEO_to_SEQNUM(dggs,dgquakes$lon,dgquakes$lat)$seqnum
cellcenters <- dgSEQNUM_to_GEO(dggs,dgquakes$cell)
quakecounts <- dgquakes %>% group_by(cell) %>% summarise(count=n())
#** HERE replace dgcellstogrid() with dgearthgrid()**
# grid <- dgcellstogrid(dggs,quakecounts$cell,frame=TRUE,wrapcells=TRUE)
grid <- dgearthgrid(dggs, frame=TRUE, wrapcells=TRUE)
# merge using all=T to include NAs
grid <- merge(grid,quakecounts,by.x="cell",by.y="cell", all = TRUE)
#PLOT
countries <- map_data("world")
p <- ggplot() +
geom_polygon(data=countries, aes(x=long, y=lat, group=group), fill=NA, color="black") +
geom_polygon(data=grid, aes(x=long, y=lat, group=group, fill=count), alpha=0.4) +
geom_path (data=grid, aes(x=long, y=lat, group=group), alpha=0.4, color="white") +
geom_point (aes(x=cellcenters$lon_deg, y=cellcenters$lat_deg)) +
scale_fill_gradient(low="blue", high="red")
p