使用R

我正在尝试使用ggplot2映射北极/南北极地区的纬度/经度位置,并按类型为它们着色。

这是我正在使用的软件包:

library(ggplot2)
library(rgdal)
library(ggmap)
library(sp)
library(dplyr) 
library(ggspatial) #To use geom_sf to add shapefiles

以下是我的数据示例:

dat <- data.frame(
  "Lat" =  c(70.5,74.5,58.5,60.5),"Lon" = c(-21.5,19.0,-161.5,-147.5),"Type"=c("A","B","A","B")
)
dat

我为北极圈创建了一个shapefile,位于以下位置:https://www.arcgis.com/home/item.html?id=f710b74427a14a1d804e90fbf94baed4

ArcticCircle <- sf::st_read("C:/.../LCC_AC.shp")

我正在尝试使用ggplot2对此进行映射,但是我找不到找到用Lambert Conformal Conic投影添加底图的方法。

我知道您可以使用 coord_sf()来指定投影和边界,但是我找不到锥形投影的代码。

p <- ggplot()+
geom_point(data = dat,aes(x = Lon,y = Lat,colour = Type))+
geom_sf(data = ArcticCircle,linetype = "dashed",aes())+
xlab("Longitude")+
ylab("Latitude")+
p

我的地图边界最好是在大约45度纬度的北极圆周围的圆。如果无法绘制圆形边界,则围绕该纬度的矩形也可以使用。

我是R的新手,所以我们将不胜感激!

y5897411 回答:使用R

这可能会有所帮助:

国家边界数据

library("rnaturalearth")
library("rnaturalearthdata")
world <- ne_countries(scale = "medium",returnclass = "sf")

然后可以裁剪和绘制感兴趣的区域,如下所示:

world_cropped <- st_crop(world,xmin = -180.0,xmax = 180.0,ymin = 45.0,ymax = 90.0)
ggplot(data = world_cropped) + 
  geom_sf() + 
  geom_sf(data = ArcticCircle,linetype = "dashed",aes())+
  geom_sf(data = dat_sf,color = 'red') + 
  coord_sf(crs = 
             "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0") 

enter image description here

,

我在您的代码中发现了一些错误,首先您的dat数据帧包含字符串格式的x和y值,并且不是数字(这在绘制时无济于事!)。

第二,与其他GIS软件不同,R不能进行即时投影转换!因此,将LAT LONG和LAT LONG一起使用时,对您的shapefile无效,因为在不同的CRS中使用!这是ArcticCircle的CRS:

proj4string:    +proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs

所以,我所做的就是将您的LAT LONG点文件转换为上面显示的CRS,然后进行了ggplot,我将在下面将所有代码加在一起,并加上注释:

library(ggplot2)
library(rgdal)
library(ggmap)
library(sp)
library(dplyr) 
library(ggspatial) #To use geom_sf to add shapefiles

#### Breaking apart all the values
x = c(-21.5,19.0,-161.5,-147.5)
y = c(70.5,74.5,58.5,60.5)
Type =c("A","B","A","B")

### Creating spatial LAT LONG coordinates,which will be converted to Lambert Conformal Conic Projection below
dat <- data.frame(lon = x,lat = y)

#### Creating LAT LONG SpatialPoints
  coordinates(dat) = c("lon","lat")
proj4string(dat) <- CRS("+init=epsg:4326")

#### The coordinate reference system,that is used in your shapefile. Will use this when converting the spatial points
polar = "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0"

### Converting the LAT LONG to the polar CRS shown above
polar_dat = spTransform(dat,polar)
polar_dat = as.data.frame(polar_dat)

#### Adding the Type column back to the data frame,with the new polar coordinates
polar_dat = data.frame(polar_dat,Type)

#### Reading in the Circle Shapefile
ArcticCircle = st_read("P:\\SHP\\LCC_AC\\LCC_AC.shp")

### Putting it togather in ggplot
p <- ggplot()+
  geom_point(data = polar_dat,aes(x = lon,y = lat,colour = Type))+
  geom_sf(data = ArcticCircle,aes())+
  xlab("Longitude")+
  ylab("Latitude")

这是情节最后的样子:

Point and Shapefile with the same CRS

希望能有所帮助,让我知道是否有任何不清楚的地方!

编辑:带有底图的新代码(感谢Majid提供数据)

library(ggplot2)
library(rgdal)
library(ggmap)
library(sp)
library(dplyr) 
library(ggspatial)
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)

#### Breaking apart all the values
x = c(-21.5,lat = y)

#### Creating LAT LONG SpatialPoints
coordinates(dat) = c("lon",0"
b <- bbox(dat)

### Converting the LAT LONG to the polar CRS shown above
polar_dat = spTransform(dat,Type)

#### Reading in the Circle Shapefile
ArcticCircle = st_read("P:\\SHP\\LCC_AC\\LCC_AC.shp")

### Getting basemap shapefile
world <- ne_countries(scale = "medium",returnclass = "sf")
world_cropped <- st_crop(world,ymax = 90.0)

### Plotting it all togather
p = ggplot(data = world_cropped) + 
  geom_sf(colour = "#6380ad",fill = "#9cb3db") + 
  geom_sf(data = ArcticCircle,aes())+
  geom_point(data = polar_dat,colour = Type))+
  coord_sf(crs = 
             "+proj=lcc +lat_1=50 +lat_2=70 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0")

enter image description here

本文链接:https://www.f2er.com/3079346.html

大家都在问