rggplot2rnaturalearth

How to add a location point using coordinates to a map using ggplot?


I can't add a location point to this map in r. The coordinates of the location are : 47.476 lat and 25.538 long. I tried this : + geom_point(data = pr, aes(x=long, y= lat), size=5, color='red', pch= 19 ), but it doesn't work. Please help me. Thanks !

library(rnaturalearth)
library(sf)
library(ggplot2)

world <- ne_countries(scale = 50, returnclass = 'sf')

ggplot(world) + 
  geom_sf(aes(fill = continent), color = 'black')+ 

  coord_sf(crs = st_crs(3035),
           xlim = c(2800000, 6200000), 
           ylim = c(1500000, 4000000)) +
  scale_fill_manual(values = c(NA, NA,NA,'grey100', 
                               NA, NA, NA, NA), guide = 'none',  na.value = 'white') +
  theme(panel.background = element_rect(),
        panel.grid.major = element_line(linewidth = 0.1))

RO <- world[world$name == "Romania", ]

ggplot() + 
  geom_sf(data = world, aes(fill =NULL), color = 'black') + 
  geom_sf(data = RO, fill = 'darkgrey', color = 'black') + 
  coord_sf(crs = st_crs(3035), xlim = c(2800000, 6200000), ylim = c(1500000, 4000000)) + 
  theme(panel.background = element_rect(), panel.grid.major = element_line(linewidth = 0.1))


Solution

  • I think that your situation is a little more complex than some examples in documentation because you are using different coordinates reference systemes (CRS) , EPSG:4326 and EPSG:3035.

    My first suggestion is to make a sf object from your single point pr = st_sfc(st_point(c(25.538, 47.476), dim ="XY"), crs = 4326 ) ; assuming the specified coordinates are in EPSG:4326. geom_sf() will take care of the transformation.

    An alternative could be to transfom the coordinates in EPSG:3035 in a first step (5483178 2829622); and using geom_point() like you tried.

    library(rnaturalearth)
    library(sf)
    library(ggplot2)
    
    
    world <- ne_countries(scale = 50, returnclass = 'sf')
    
    RO <- world[world$name == "Romania", ]
    
    pr = st_sfc(st_point(c(25.538, 47.476), dim ="XY"), crs = 4326 )
    
    ggplot() + 
      geom_sf(data = world, aes(fill =NULL), color = 'black') + 
      geom_sf(data = RO, fill = 'darkgrey', color = 'black') + 
      geom_sf(data = pr,col ="green")+
      coord_sf(crs = st_crs(3035), xlim = c(2800000, 6200000), ylim = c(1500000, 4000000)) + 
      theme(panel.background = element_rect(), panel.grid.major = element_line(linewidth = 0.1))
    

    The specified alternative: transfom the coordinates in EPSG:3035 in a first step (5483178 2829622); and using geom_point()

    
    library(tidyverse)
    pr_df = data.frame(long = 25.538, lat = 47.476) %>% 
      st_as_sf( coords = c("long","lat"), crs  = 4326) %>%  st_transform(3035) %>% 
      st_coordinates() %>% as_tibble() 
    
    ggplot() + 
      geom_sf(data = world, aes(fill =NULL), color = 'black') + 
      geom_sf(data = RO, fill = 'darkgrey', color = 'black') + 
      geom_point(data = pr_df,mapping = aes(x = X, y = Y),col ="green")+
      coord_sf(crs = st_crs(3035), xlim = c(2800000, 6200000), ylim = c(1500000, 4000000)) + 
      theme(panel.background = element_rect(), panel.grid.major = element_line(linewidth = 0.1))
    

    Created on 2024-10-22 with reprex v2.1.0