如何使用ggplot将使用坐标的位置点添加到地图?

问题描述 投票:0回答:1

我无法在 r 中向此地图添加位置点。该位置的坐标为:纬度 47.476,长 25.538。 我尝试了这个:+ geom_point(data = pr, aes(x=long, y= lat), size=5, color='red', pch= 19 ),但它不起作用。 请帮我。谢谢!

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))

r ggplot2 rnaturalearth
1个回答
0
投票

我认为您的情况比文档中的一些示例更复杂一点,因为您使用不同的坐标参考系统(CRS),EPSG:4326和EPSG:3035。

我的第一个建议是从你的单点

sf
制作一个
pr = st_sfc(st_point(c(25.538, 47.476), dim ="XY"), crs = 4326 )
对象;假设指定的坐标位于 EPSG:4326 中。
geom_sf()
将负责转变。

另一种方法是第一步转换 EPSG:3035 中的坐标 (5483178 2829622);并像您尝试过的那样使用

geom_point()

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))

指定替代方案:第一步转换 EPSG:3035 中的坐标(5483178 2829622);并使用

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))

创建于 2024 年 10 月 22 日,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.