1

I am trouble wrapping my head around projection. My points for a place in Northern Europe ends up in Mid Africa.

My code goes as follow.

#Loading packages

library(OpenStreetMap)
library(rgdal)
library(ggplot2)

#defining world map
map <- openmap(c(70,-179), c(-70,179))
plot(map)

#Finding my work place in Northern Europe (Ørbækvej 100, Odense, Denmark from here: https://www.latlong.net/convert-address-to-lat-long.html)
subscr<-data.frame(lat=c(55.381640),
                   lon=c(10.433600))

#I am not sure what this does, but found on the web for a map in Germany: (https://gis.stackexchange.com/questions/209166/plotting-bubbles-on-top-of-openstreetmap-in-r) 
coordinates(subscr)<-~lat+lon
proj4string(subscr)<-CRS("+init=epsg:4326")
points(spTransform(subscr,osm()))
#as can be seen using this method the dot turns up in Eastern Africa


symbols(y = subscr$lon, x = subscr$lat, circles = 1, add = TRUE,
        inches = 0.0001, bg = "darkgreen")
#as can be seen using the method the dot turns up in Western/Mid Africa

Can anyone explain or even help me to get the dot placed in Denmark, Northern Europe?

Sander Ehmsen
  • 83
  • 1
  • 6
  • 1
    Regarding "I am not sure what this does" in your code: a map has a certain [projection](https://en.wikipedia.org/wiki/Map_projection). If you configure a wrong projection your coordinates will end up at the wrong position. – scai Oct 12 '18 at 13:32

2 Answers2

6

I do not know what kind of map you want, but for plotting lat-lon points, leaflet is my default weapon of choice..

library( leaflet )
library( magrittr )

subscr<-data.frame(lat=c(55.381640),
                   lon=c(10.433600))

leaflet() %>% addTiles() %>% 
  addCircleMarkers(data = subscr,
                   lat = ~lat, lng = ~lon,
                   color = "blue")

enter image description here

Wimpel
  • 26,031
  • 1
  • 20
  • 37
  • Thank you for your try. It could work, but I need the possibility to adjust size of the dot. Is that possible? And can I change the layout of the map in that leaflet-package? – Sander Ehmsen Oct 15 '18 at 05:58
  • @SanderEhmsen of course... you can change the `radius` of the marker, the coler, and much...much... more. See the leaflet helpfile, `?addControl`, or read the `addControl`-section of https://cran.r-project.org/web/packages/leaflet/leaflet.pdf – Wimpel Oct 15 '18 at 06:54
0

Are you bound to using open street maps? You might consider using the ggmap package which interacts pretty well with ggplot2. However, I sometimes have troubles with downloading an open street map with ggmap, but google-maps should work.

Consider the following example. Note that I removed unnecessary text in the map in the download command.

# download
map <- get_googlemap(center = "Europe", zoom = 3, 
                     style = paste0("feature:administrative.country|",
                                    "element:labels|visibility:off"),
                     filename = "Map",
                     language = "en-EN") # you might want to adjust the language settings

# see what you've got
ggmap(map)

# edit map
ggmap(map)+

  # do some scaling (make it smaller)
  scale_x_continuous(limits = c(-12, 42), expand = c(0, 0)) +
  scale_y_continuous(limits = c(35, 70), expand = c(0, 0))+

  # remove unwanted information
  theme(axis.title.x    = element_blank(),
        axis.title.y    = element_blank(),
        axis.line       = element_blank(),
        axis.ticks      = element_blank(),
        axis.text       = element_blank(),
        plot.title      = element_blank(),
        plot.background = element_blank())+

  # add whatever you like based on your coordinates using annotate()
  annotate("text", x = 10.433600, y = 55.381640, 
           label = "You are here",
           size = 2.4, color = "black", fontface = "bold",
           na.rm = TRUE, hjust = 0.5)

Does this solve your problem?

alex_555
  • 1,092
  • 1
  • 14
  • 27
  • 1
    Thank you for your effort. I used to use ggmap (or RgoogleMaps). But I keep getting an error and thus abandoned it: Error in download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : cannot open URL (*shortened by Sander*) In addition: Warning message: In download.file(url, destfile = tmp, quiet = !messaging, mode = "wb") : cannot open URL 'http://maps.googleapis.com/maps/api/staticmap?center=Europe&zoom=3&size=640x640&scale=2&maptype=terrain&language=en-EN&style=feature:administrative.country%7Celement:labels%7Cvisibility:off&sensor=false': HTTP status was '403 Forbidden' – Sander Ehmsen Oct 15 '18 at 06:02
  • Here's a conversation about this issue: https://stackoverflow.com/questions/19827598/error-in-get-map-using-ggmap-in-r. Interestingly, I recently upgraded from Windows 8 to Windows 10 and now I'm getting the same error. – alex_555 Oct 15 '18 at 07:14