Creating map of Hanoi with OpenStreetMap and data frame

Hey,
I am trying to create a open street map of Hanoi and add data points from my data frame to it. But I'm always getting the same error no matter what I try. I found a similar post from someone with the same error and they solved by adding "+scale_fill_continuous(guide=FALSE)" , that doesn't help me however.

My code:
library(OpenStreetMap)
library(ggmap)
library(osmdata)
library(ggplot2)
library(tidyverse)
library(rgdal)

traps <- data.frame(name=c("Kim Ma","An Khanh","Gia Quat", "Ngoc Ha", "Cu Khoi N", "Cu Khoi S"),
longitude=c(105.818147,105.732711,105.879748,105.8265118,105.9067497,105.9067648),
latitude=c(21.031584,21.008828,21.055393,21.0399774,21.0083294,21.0081745))
location=c(106,20.5,105.6,21.1)

Fetch the map

Hanoi = get_map(location = location, source = "osm", zoom = 3)

Draw the map

Hanoimap = ggmap(Hanoi)

Add the points layer

Hanoimap = Hanoimap + geom_point(data=traps, aes(x = longitude, y = latitude), size = 5)

Add the labels

Hanoimap + geom_text(data=traps,mapping=aes(x = longitude+.001, y = latitude,label = name))

The error:
rror in grid.Call.graphics(C_raster, x$raster, x$x, x$y, x$width, x$height, : Empty raster

  1. grid.Call.graphics(C_raster, x$raster, x$x, x$y, x$width, x$height, resolveHJust(x$just, x$hjust), resolveVJust(x$just, x$vjust), x$interpolate)

  2. drawDetails.rastergrob(x, recording = FALSE)

  3. drawDetails(x, recording = FALSE)

  4. drawGrob(x)

  5. recordGraphics(drawGrob(x), list(x = x), getNamespace("grid"))

  6. grid.draw.grob(x$children[[i]], recording = FALSE)

  7. grid.draw(x$children[[i]], recording = FALSE)

  8. drawGTree(x)

  9. recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))

  10. grid.draw.gTree(x$children[[i]], recording = FALSE)

  11. grid.draw(x$children[[i]], recording = FALSE)

  12. drawGTree(x)

  13. recordGraphics(drawGTree(x), list(x = x), getNamespace("grid"))

  14. grid.draw.gTree(gtable)

  15. grid.draw(gtable)

  16. print.ggplot(x)

  17. (function (x, ...) UseMethod("print"))(x)

Thank you for your help!
/Janina

Try this (notice the boundingbox) :

library(ggmap)
#> Loading required package: ggplot2
#> Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
#> Please cite ggmap if you use it! See citation("ggmap") for details.
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright

traps <- data.frame(
  name=c("Kim Ma","An Khanh","Gia Quat", "Ngoc Ha", "Cu Khoi N", "Cu Khoi S"),
  longitude=c(105.818147,105.732711,105.879748,105.8265118,105.9067497,105.9067648),
  latitude=c(21.031584,21.008828,21.055393,21.0399774,21.0083294,21.0081745),
  stringsAsFactors=F)

#location <- c(106,20.5,105.6,21.1)  # if this is supposed to be bounding box
location <- c(105.6,20.9,106,21.1)  # than specify it like this
Hanoi <- get_map(location = location, source = "osm", zoom = 12)
#> Source : http://tile.stamen.com/terrain/12/3249/1802.png
#> Source : http://tile.stamen.com/terrain/12/3250/1802.png
#>  removed 14 similar lines HanOostdijk
#> Source : http://tile.stamen.com/terrain/12/3253/1804.png
#> Source : http://tile.stamen.com/terrain/12/3254/1804.png

Hanoimap <- ggmap(Hanoi)

Hanoimap <- Hanoimap + geom_point(data=traps, aes(x = longitude, y = latitude), size = 5)
Hanoimap <- Hanoimap + geom_text(data=traps,mapping=aes(x = longitude+.001, y = latitude-.001,label = name))

print(Hanoimap)


Created on 2021-07-21 by the reprex package (v2.0.0)

image

Thank you so much for the quick reply! This solved my problem. So I assume that not having defined my bounding box was the issue.

You did define it in location but the order of coordinates was not correct.
And I changed one of the coordinates to make the plot smaller.

Okay, so you have to define first the bottom left and then the top right corners for the location?

See

? get_map

Okay, got it. Left, bottom, right, top.