user1420372
user1420372

Reputation: 2187

How to align ggmap CRS of google map longitude latitude

Despite the many posts of CRS projections, etc I cannot stop my town from sinking.

If I go to Google Maps and enter -35.016, 117.878 the town of Albany is on dry land (as per following image): enter image description here

If I enter the lat/long into R and try to map using the simple features package and ggmap, the town is in the ocean:

library(tidyverse)
library(sf)
library(lwgeom) 
library(ggmap) 

lat <- c(-35.016)
lon <- c(117.878)
df <- tibble(lon,lat) %>% st_as_sf( coords = c("lon", "lat"), crs = 4326)

bbox_aus <- c(left = 113.338953078, bottom = -43.6345972634, right = 153.569469029, top = -10.6681857235)
ggmap_aus <- ggmap(get_stamenmap(bbox_aus, zoom = 5, maptype = "toner-background"))  

ggmap_aus + 
  geom_sf(data = df, colour = "red" , size = 3,  alpha = 0.5, inherit.aes = FALSE) +
 # coord_sf(datum = sf::st_crs(4326)) +
  labs(title = "Albany Sinking",
       x = NULL,
       y = NULL) +
   theme_bw()

enter image description here

Upvotes: 5

Views: 1405

Answers (1)

Eugene Chong
Eugene Chong

Reputation: 1741

It works if you use geom_point() with the lon and lat as x and y.

df <- tibble(lon,lat) %>% st_as_sf( coords = c("lon", "lat"), crs = 4326,
                                    remove = FALSE)

ggmap_aus + 
  geom_point(data = df, colour = "red", size = 3,  alpha = 0.5,
             aes(x = lon, y = lat)) +
  # coord_sf(datum = sf::st_crs(4326)) +
  labs(title = "Albany is saved",
       x = NULL,
       y = NULL) +
  theme_bw()

enter image description here

Based on this comment, using geom_point() with an x and y aesthetic aligns more closely with how ggmap produces a ggplot.

Unfortunately, I'm not sure how to make it work with geom_sf(), which plots using the geometry column. There's some discussion in that linked comment, but the solution seems to be to use inherit.aes = FALSE, which you've already tried.

Based on the warning Coordinate system already present. Adding new coordinate system, which will replace the existing one., I assume that the ggmap object has some coordinate system that is not 4326, but I couldn't find how to access it. I did try reprojecting df to EPSG: 3857, but that did not work.

Upvotes: 3

Related Questions